Solved

Error: ByRef argument type mismatch

Posted on 2001-07-21
7
1,313 Views
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.  

-------------------------------

Module1.bas

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

-------------------------------
Module2.bas

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)
   Next

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?
0
Comment
Question by:GeneM
[X]
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
7 Comments
 

Expert Comment

by:LorangerG
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.
0
 

Expert Comment

by:LorangerG
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.
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6304750
Change this

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

To

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

Cheers
0
Technology Partners: 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!

 
LVL 8

Accepted Solution

by:
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
0
 
LVL 3

Author Comment

by:GeneM
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.
0
 
LVL 5

Expert Comment

by:KDivad
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

KDL
0
 
LVL 3

Author Comment

by:GeneM
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!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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