Problem with an "optional" parameter...

B

Brad Pears

I have a function that includes an "optional" boolean parameter as the 2nd
(and last) parameter.

In one particular location, I am executing the function and have passed it a
"false" value. In every other instance where this function is executed - I
am not even passing the optional parameter because I want the value to be
"true".

I coded the function as follows...

Function TestFunc(parm1 as string, optional parm2 as boolean = true)
....
if parm2 then
some code
else
some code
endif
....
end function

What is happening is that even when I pass a "false" value to this optional
parameter, it is using the "true" value. I anticipated that the "true" value
would only be used when no "optional" parameter was passed and that when a
value of "false" is passed, it would use that. Is this not the case?

Am I going to have to modify ever line of code where this function is
executed (tons of places) to pass a value of either "true" or "false" to
resolve it? (thus removing the "optional" parameter and replacing with a
requried parameter)

Thanks,

Brad
 
D

Douglas J. Steele

What you're describing is something I do all the time without any problems.

Post a sample of how you're calling the function.
 
K

Ken Snell [MVP]

Your expectation is correct... if you pass parm2 a value of False, it will
use that instead of its default value of True. Thus, I suggest that you look
more carefully at your code to confirm that you indeed are passing a False
value when you think you are, and that the function isn't changing the value
of parm2 before you test it.
 
A

Allen Browne

The skeleton function you supplied is fine and should work, so perhaps it is
the way it is being called.

Exactly what value are you supplying for the 2nd parameter?
Is it False (*without* the quotes)?
Is it in a query or in other VBA code?
Is it a Variant or other kind of variable or constant?
 

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