Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > ByRef Argument Type Mismatch < Abort Loop | Sex & Gender >
ByRef Argument Type Mismatch
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   17 months ago

ByRef Argument Type Mismatch Error in MS Access


 S  M  L  XL  FS  |  Slo  Reg  Fast  2x  |  Bookmark Join Now

In this Microsoft Access tutorial, I will show you how to resolve a ByRef argument type mismatch error in VBA. Learn the essential concepts of ByRef vs. ByVal, type mismatches, and the importance of declaring variables correctly. Perfect for advanced developers dealing with frustrating Type Mismatch errors.

Christine from Kirkwood, Missouri (a Platinum Member) asks: I need your help with something I've been struggling with for three or four days now. I wrote a function and have declared all my variables correctly, but I'm getting a ByRef argument type mismatch error. I can't figure out what's wrong. Help me, Obi-Wan, you're my only hope.

Members

There is no extended cut, but here is the database download:

Silver Members and up get access to view Extended Cut videos, when available. Gold Members can download the files from class plus get access to the Code Vault. If you're not a member, Join Today!

Prerequisites

Links

Recommended Courses

Learn More

FREE Access Beginner Level 1
FREE Access Quick Start in 30 Minutes
Access Level 2 for just $1

Free Templates

TechHelp Free Templates
Blank Template
Contact Management
Order Entry & Invoicing
More Access Templates

Resources

Diamond Sponsors - Information on our Sponsors
Mailing List - Get emails when new videos released
Consulting - Need help with your database
Tip Jar - Your tips are graciously accepted
Merch Store - Get your swag here!

Questions?

Please feel free to post your questions or comments below or post them in the Forums.

KeywordsByRef Argument Type Mismatch in Microsoft Access

TechHelp Access, ByRef argument type mismatch, ByRef error fix, resolving ByRef error, VBA type mismatch, type mismatch VBA, VBA argument type mismatch, ByRef vs ByVal, declaring variables in VBA, debugging ByRef mismatch, VBA advanced tutorial, Microsoft Access error handling, option explicit VBA, VBA function error, troubleshooting VBA errors, understanding VBA errors, advanced VBA lesson

 

 

 

Comments for ByRef Argument Type Mismatch
 
Age Subject From
17 monthsTime machineThomas Gonder

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to ByRef Argument Type Mismatch
Get notifications when this page is updated
 
Transcript Today's one for the advanced developers. We're going to talk about the ByRef argument type mismatch error. Chances are you're only watching this video if you're a regular follower of mine or you got this error message and you're Googling it and trying to figure out how to fix it. So let's talk about what it is and how you can get rid of it.

First up, this is an advanced developer lesson. So if you want to be a developer and you're not there yet, but you want to be, you want to sit at the table with the cool kids, you can start here. Go watch this Intro to VBA lesson. VBA isn't scary. This will teach you everything you need to know to get started in about 20 minutes.

Now since this error message literally has the words type mismatch in it, if you don't know what a type mismatch is, go watch this video. It's like if you got a long integer and you're trying to stuff a string into it. That's the wrong type. Then you'll get this message. All right? So go watch this video.

And the error message literally has by ref in it. So if you don't know the difference between by ref and by val, go watch this video. I'll put a link to it down below since I don't have a cool screen for it yet. Okay? This isn't one of my more popular videos, but this explains what by ref means. So go watch this if you're not sure.

And finally, we're going to use my little status box sub to display stuff in a little status box. That's all that is. So these are all free videos. They're on my website. They're on my YouTube channel. Go watch them and come on back.

Today's question comes from Christine in Kirkwood, Missouri, one of my Platinum members. Christine says, I need your help with something I've been struggling with for three or four days now. I wrote a function and I've declared all my variables correctly and I'm getting a by ref argument type mismatch error. I can't figure out what's wrong. Help me, Obi-Wan. You're my only hope.

I love the Star Wars reference. Of course, most people know I'm a Trekkie, so I'm a huge Star Trek fan. But I love Star Wars too, the original ones. The new ones, not so much. But that's a topic for a totally different video.

So let's talk about this error message.

All right, here I am in my TechHelp free template. You can download a free copy of this off my website if you want to. And in here I got this little Hello World button. You click on it, it says Hello World. It uses my little status function that I wrote. Right-click, build event, take a peek. Bring the code window down here.

Right, it just goes status, Hello World, and status is up top here. I'm going to move this down. Actually, I'm just going to delete all the stuff in here we don't need so we can get rid of the clutter.

Okay, so status hello world sends it up to here, right, s as a string. Now the obvious way you'll get a type mismatch error is if status is expecting a long. If status wants a long and you try to send it a string, what happens? You get the basic type mismatch error. It's not a by ref type mismatch, but it's just a regular old type mismatch. You can't do that. Okay?

All right. Let's bring this over here. Close that. Now, interestingly enough, if you just get rid of that declaration right there, if you get rid of that type and just say S, that means it's accepting a variable S, but if you don't give it a type, it's going to make it a variant. Variant means it can pretty much hold anything. Pretty much, not objects, but it can hold longs, dates, strings, pretty much whatever you give it within reason. And now if I hit the button, it works. Because this is a string, and you can stuff a string into a variant without any problems.

Now let's say down here in the Hello World button, you declared a variable. Let's say dim my name as a string. And I'm going to say, my name equals Richard. And then I'm going to status my name. Save that. And now run it. And it works just fine. Because my name is a string. I'm putting Richard into that string. And then I'm sending the string up here which is receiving a variant and that's okay. You can stuff a string into a variant.

What happens if you try to go the other way? What happens if we don't declare this as a string? All right, if I don't put a type on there it's going to be what? A variant. And what if up here I...

Well, now watch what happens. There's your by ref argument type mismatch, okay? Because you can't stuff a variant into a string. Now by ref is because by default when you pass values back and forth, they're passed by ref, all right, by reference, as opposed to by val, which is where you send just the value, which prevents the original variable from having its value changed. Okay, that's not really important at this point but it's important to know it long term.

All right, but the problem here is that you're trying to stuff the wrong kinds of value into a string. Okay I see this a lot from people who don't have option explicit in all of their code and you should. I got a whole separate video on option explicit and why you should use it. Now it's the default setting now I believe in new databases in 2024 now. It wasn't always the case in older versions of access. You had to add option explicit. Option explicit forces you to have to define all of your variables and if you don't, you can do something like this.

I'm just using my name. I haven't declared it. So if it's not declared as a type, what is it? It's a variant. And now if I try sending it to the function, same problem. Because my name is a variant here, and then repeat it with me, you can't stuff a variant into a string.

So it's very important to always have option explicit at the top of all of your code. All of your code modules, your module modules, your form modules, your report modules, and if you have that setting on that I talk about in the other video, then it'll be there for you all the time whether you want it or not and you should want it. Option explicit, if it does one thing, the good thing about it is it prevents misspellings. If you get long or stupid variable names, you know, like P3 1X427. You type it in wrong. If you don't have option explicit, you won't know what the problem is and your code just doesn't work.

Now, here's where Christine had a problem and she sent me a copy of her code. I usually don't accept code from people, folks, but I went back and forth and emailed with her a couple of times, which I will sometimes do with my Platinum members. And I finally broke down, I'm like, all right, send me your code, I'll take a peek at it. So it wasn't very complicated, but I spotted the problem right away.

Let's say you've got an intermediary function in here. Let's say you've got your own function in here that sends an email. Let's call it sendEmail. This is kind of basically what she was doing. And you've got three parameters, email, subject, body, that it's taking in. And you could probably spot the problem right away.

End sub. Oh, I forgot the word sub here. Private sub send email. And you can see right here, this was Christine's problem. She didn't have declarations in here for the types. So these are all variants. And so this is what actually process sending the email. And I'll just pretend we're doing that but I'm going to say status email. Okay, let's pretend that the status is actually sending the email. So she made her own function where she does this.

She adds some extra stuff in here, attachments and blah, blah, blah and then sends it to the actual function that sends the email. But it's this intermediary that was causing the problem because now if I come down here my hello world click button, I go send email to me, amerkronageemail.com. Yes, that's my email address. Don't send me all kinds of crazy stuff. Hi there is the subject and the body is live long and prosper.

Okay, so send email is going to send these bits of data to the send email function. It's going to receive them as variants. Then you're going to try to send them to this guy which wants a string. An email is not a string, it's a variant. So you're going to try and stuff a variant into a string.

Debug compile, there's your error message. See, by ref argument type mismatch. Okay. How do you fix it? Well, you put as string in here. In all of them. As string, as string, as string. Interesting point I want to show you though, watch this. Even if you leave those as variants, if you change this to sending to and with little concatenation there, look at that. Now it works.

Why is that? Well, because as soon as you concatenate that string value onto the variant, it casts the variant, it converts it basically, into a string variable type. So now this becomes a string which can now be stuffed into status. It's weird folks, yeah I know, it takes years to get used to this stuff. So you can basically get around all these problems by making sure you've got all of your arguments As a string, as a string. Just like that.

And all your variables have to be declared that way too. Another problem I see people do is they'll go, Dim, first name, last name, address, whatever, blah, blah, blah, as string. This is a common problem. I see this all the time. I think I even mentioned this in the video that I have for Dim and for variables. I know I mentioned it in one of my developer classes. What's the problem here? Well, here, everybody see it?

All right, pause the video, figure it out if you don't know what the problem with this one is. Problem here is only address is declared as a string. First name and last name are variants. Because you've got to put as string. I know, I know, I know it's a lot of repeat typing, but that's just how visual basic is. Some other languages like C don't make you do that. You can declare a bunch of integers and make them ints, and stuff like that.

But with visual basic, you gotta have the type with every variable. I know, it's a pain. So that's that, Christine. Sorry you had to pull your hair out for three or four days, but that happens sometimes. I have very little hair left. Actually, I've got a pretty good head of hair. I can't complain. For being 51 years old, I've got most of my hair still. But I've had many, many nights of trying to figure out why something doesn't work, why something doesn't work.

And honestly, that struggle will build you into a better developer, because once you make this mistake, you won't make it again. And that's why I make these videos to try to help some of you who have encountered this error message and you may be now you'll find it you'll find my little video and you'll like it and you'll learn something.

Speaking of learning something if you want to learn more stuff like this I got lots I got tons and tons of developer lessons on my website come and check them out there's the link or join and become a member of my website and you get all kinds of free cool stuff you get extended cut videos. You get a free class every month. You get lots and lots of cool stuff.

But that is going to be your TechHelp video for today. It's Friday so have a good weekend. Friday what? 28th of June 2024. I hope you learned something. Live long and prosper my friends. See you next week.

TOPICS:
ByRef argument type mismatch error explanation
Difference between ByRef and ByVal
Introduction to Type Mismatch error
Using the status box subroutine
Declaring variables in VBA
Handling variant data type in VBA
Option Explicit and its importance
Common mistakes with variable declaration
Fixing ByRef argument type mismatch error
Concatenation and type casting in VBA
Debugging ByRef argument type mismatch
Using intermediary functions in VBA

COMMERCIAL:
In today's video from Access Learning Zone, we tackle the ByRef argument type mismatch error. If you're stuck with this error, don't worry. First, I guide you through understanding type mismatches. Then, we delve into by ref versus by val, and finally, we dive into a practical example with a status box sub. Christine from Missouri sent me her code, and we troubleshoot it together, highlighting common mistakes and solutions that you'll find helpful. You'll find the complete video on my YouTube channel and on my website at the link shown. Live long and prosper my friends.
Quiz Q1. What is the primary cause of the "by ref argument type mismatch" error in VBA?
A. Incorrectly named variables
B. Undefined variables in the code
C. Passing an incorrect data type to a function that expects a different data type
D. Using too many subroutines in the code

Q2. What does "ByRef" mean in VBA?
A. The original variable's value can be changed
B. The value is passed without changing the original variable
C. It is a variant type that can hold any value
D. It is used to declare string variables only

Q3. What can you do to avoid the "by ref argument type mismatch" error?
A. Use of global variables
B. Explicitly define the data types of all passed arguments
C. Avoid using functions or subroutines
D. Declare variables without specifying their type

Q4. What happens if you do not put a type on a variable in VBA?
A. It becomes a string by default
B. It becomes an integer by default
C. It becomes a variant by default
D. It causes a compilation error

Q5. What is the purpose of using "Option Explicit" at the top of VBA code?
A. To prevent the use of global variables
B. To automatically declare all variables as strings
C. To ensure all variables are declared before use
D. To increase the execution speed of the code

Q6. How does concatenating a string to a variant type affect the variant in VBA?
A. It converts the variant to an integer
B. It leaves the variant unchanged
C. It converts the variant to a string
D. It causes a type mismatch error

Q7. Why is it important to declare data types for all subroutine parameters?
A. To make the code run faster
B. To avoid runtime errors related to type mismatches
C. To allow the subroutine to accept any type of data
D. To reduce the amount of code needed

Q8. In the example in the video, what mistake was identified in the intermediary function?
A. The function was missing the "Private" keyword
B. The function was not declared correctly
C. The parameters did not have specified data types
D. The parameters were declared globally

Q9. What can happen if you declare multiple variables in a single "Dim" statement without specifying their types individually?
A. All variables will automatically be integers
B. Only the last variable will have the specified type, others will be variants
C. It will cause a compilation error
D. All variables will be strings

Q10. Why does the presenter suggest that struggling with error messages can be beneficial for developers?
A. It helps developers learn to ignore errors
B. It ensures developers will not make the same mistake again
C. It slows down the learning process intentionally
D. It helps developers learn to write less code

Answers: 1-C; 2-A; 3-B; 4-C; 5-C; 6-C; 7-B; 8-C; 9-B; 10-B

DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks.
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 11/17/2025 7:29:09 AM. PLT: 1s
Keywords: TechHelp Access, ByRef argument type mismatch, ByRef error fix, resolving ByRef error, VBA type mismatch, type mismatch VBA, VBA argument type mismatch, ByRef vs ByVal, declaring variables in VBA, debugging ByRef mismatch, VBA advanced tutorial, Microsoft  PermaLink  ByRef Argument Type Mismatch in Microsoft Access