Error: ByRef argument type mismatch

Posted on 2001-07-21
Last Modified: 2008-03-17
Hello all,
I am getting the message in the IDE "ByRef argument type mismatch"  In Module1 I am calling a function in Module2 as shown.  I hope I have included enough code.  



   Dim Mask$,dblVar#
   Result$ = Ptformat(Mask,dblVar)


Public Function PTFormat(ByVal strMask, ParamArray   FParams() As Variant) As String
   iMaxNum = UBound(FParams)
   ReDim vDataParams(iMaxNum)
   For i = 0 To iMaxNum
       vDataParams(i) = FParams(i)

strResult = sProcessFormat(strMask, vDataParams())

End Function

Function sProcessFormat(strWorking As String, vDataParams() As Variant) As String


I get the error when I call sProcessFormat in Module2.  It is complaining about the vDataParams parameter.

The code in Module2 works in other projects.  I have one project in which I cannot make it work (although it worked at one time in the project in which it is failing now).

I suspect something got set up wrong somewhere.  Any ideas?
Question by:GeneM
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 6304745

If you pass a parameter to a function or sub procedure by reference, the type of the actual parameter passed and the corresponding function argument must match. Otherwise, you will get a "ByRef argument type mismatch" error.

Expert Comment

ID: 6304746

The reason types have to match with ByRef parameters is that the called procedure is working on the original outside variable through a reference pointer. In the following step-by-step example, if the procedure thinks it is modifying a variant but the outside variable is really a control, the data will probably be ruined.

If something is passed by value, Visual Basic can do automatic type conversion. When you pass by value, the inside procedure is working on a copy and can therefore modify it in any way, such as converting the passed object to a temporary Variant and working on that.

This applies to simple built-in types as well as objects. Problems like this are easier to understand and debug if you set Option Explicit and declare every variable type explicitly.

Expert Comment

ID: 6304750
Change this

Function sProcessFormat(strWorking As String, vDataParams() As Variant) As String


Function sProcessFormat(strWorking As String, vDataParams As Variant) As String

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Accepted Solution

Dave_Greene earned 100 total points
ID: 6304751
Does this work?

Public Function PTFormat(ByVal strMask, ParamArray   FParams() As Variant) As String

...  I don't think so

Author Comment

ID: 6304929
Loranger & Dave,
Thank you both for responding.

Loranger, I have Option Explicit turned on and have all data types explicitly defined.  I just left some of the definitions out of the sample code.

Dave, I tried your first suggestion and it doesn't help.  As to your second question (Does this work?) the answer is yes.  The Keyword ParamArray is used to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments.

Unless I am really cracking up :-), the code in Module2 works correctly in other projects.  Thus, I think it has something to do with my current project.

Expert Comment

ID: 6304987
No, actually you don't have *all* your variables defined. The header for PTFormat has an implicit Variant for it's first parameter (strMask).

Public Function PTFormat(ByVal strMask, ParamArray FParams() As Variant) As String

You are then passing this variant to the sProcessFormat function:

strResult = sProcessFormat(strMask, vDataParams())

Which has a ByRef string!:

Function sProcessFormat(strWorking As String, vDataParams() As Variant) As String

Either change PTFormat's header to this:

Public Function PTFormat(ByVal strMask As String, ParamArray FParams() As Variant) As String

Or change sProcessFormat's header to this:

Function sProcessFormat(ByVal strWorking As String, vDataParams() As Variant) As String


Author Comment

ID: 6305029
Thanks to all who responded.  I have found my problem.

I am updating a very large program that was written by someone else.  It contains about 50 BAS modules, each with many subs in them.

In one of the 50 BAS modules, I found a IsNumeric function!  The compiler was using that function instead of the VB IsNumeric function.  

Again, thank you all for responding.  This problem was driving me crazy.  I thought sure it had something to do with my current project options.  Of course, it was related to my current project, as that is the only project in which I include the BAS module with the IsNumeric function.

I am giving the points to Dave just because he was early to post an answer, and gave me something to investigate!

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now