passing arguments byref in VBA

  • Thread starter Thread starter Jim Dalrymple
  • Start date Start date
J

Jim Dalrymple

I am passing a date variable(code is in a form) to a
public function of a class module. When I step through
the code in the class, the argument is correctly updated
to the new value. However, when control goes back to the
calling Form-based Private function, the date variable is
not updated.

I've checked and re-checked the code. I have another proc
that does the exact same thing and it works. What could
be happening here?

Thanks for the help!
 
I've checked and re-checked the code. I have another proc
that does the exact same thing and it works. What could
be happening here?

Care to post the code? It's a bit hard to tell what might be wrong
with it without seeing it!
 
Jim Dalrymple said:
I am passing a date variable(code is in a form) to a
public function of a class module. When I step through
the code in the class, the argument is correctly updated
to the new value. However, when control goes back to the
calling Form-based Private function, the date variable is
not updated.

I've checked and re-checked the code. I have another proc
that does the exact same thing and it works. What could
be happening here?

Thanks for the help!


I believe that if the called code fails before it exits normally, the
original value of the byref actual parameter is reinstated! (It's a bit as
if the byref value changes are wrapped in a memory transaction.) So make
sure that the called code is exitting normally, not being killed with an END
statement or the debugger etc.

Also, try adding the ByRef prefix to the formal parameter declaration -
though I'm sure that is the default, in any case.

Are you sure that the actual parameter is an updatable item (eg. a
variable)? If it is not (eg. a CONST), the called code still runs fine, &
there is no indication that updating the formal parameter did NOT update the
actual parameter.

HTH,
TC
 
The compiler was not giving me an error during design.

This doesn't work:

Shipment.Import (dteLastImported)

This works:

Shipment.Import dteLastImported

There is no compiler error here because it's perfectly valid syntax. The
expression in the parentheses is evaluated before being passed to the sub.
The value passed happens to be the same as the original value, but when it
gets changed on returning from the sub there is no way for it to find its
way back to the variable. It is syntactically the same as

Shipment.Import (dteLastImported + 7)

or

Shipment.Import #2002-09-30#

The parentheses trick can be quite useful to protect a variable from being
altered by a ByRef function.

All the best


Tim F
 
Thanks for the reply. See the other response to my
question. I was using parenthesis around the variable,
which was protecting it from being updated. I didn't know
that VBA worked that way.
 
Back
Top