Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Error: ByRef argument type mismatch

Posted on 2001-07-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Accepted Solution

Dave_Greene earned 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

618 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