ByRef argument type mismatch

S

Sunny

Following is my function in general module.

Function GetMonth(dDate As Date, cType As String) As Date
GetMonth = dDate - Day(dDate) + 1
If cType = "TYPE1" And Day(dDate) >= 25 Then
GetMonth = DateAdd("m", 1, GetMonth)
End If
If cType = "TYPE2" And Day(dDate) < 20 Then
GetMonth = DateAdd("m", -1, GetMonth)
End If
End Function

Now when I run this function in immidiate window
?GetMonth(date(),"TYPE1") 'returns 3/1/2004, which is expected.

but in immidiate window when I try following:
dt = date()
st = "TYPE1"
?GetMonth(dt,st) 'Which gives me error ByRef argument type mismatch

Can't I pass variable to the function? Or why this happens?
 
W

Wayne Morgan

The immediate window is just a "one liner". You can sometimes combine
multiple statements on one line using semicolons as dividers. However, I
tried that with what you have here and couldn't get it to work. If someone
else has a suggestion, hopefully they'll jump in.
 
V

Van T. Dinh

The problem is that in the Immediate window, both dt and st are variants and
therefore don't exactly match the types of the function arguments. In the
Immediate window, you can use the type-declaration character to explicitly
declare the type. Try:

str3$ = "TYPE1"

?GetMonth(Date(), st3)
01/03/2004

Unfortunately, there is no type-declaration character for DateTime (AFAIK,
anyway). Hence, the above example only uses variable for the String
argument.

****
Since you don't change these input values (and you don't want to have
side-effects, anyway), you should use ByVal like:

Function GetMonth(ByVal dDate As Date, _
ByVal cType As String) As Date

This declaration should work fine as per your test.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top