Passing variable

  • Thread starter Thread starter Rohit Thomas
  • Start date Start date
R

Rohit Thomas

Hello,

I have the following variable (Static PassShow as Integer)
in a private sub of a form that is set to 1 once a certain
condition becomes true. I would like to pass this variable
to another private sub. I am new to VB and am not sure how
to pass variables between private subs. Any help would be
appreciated.

Thanks,
Rohit
 
Passing a variable is done when you call the next sub or function. You use
parameters to do this. Another option is to increase the "scope" of the
variable. To increase the scope to be available anywhere in the form you
would declare it in the form's Declarations section (where the Option
Compare Database and, hopefully, Option Explicit are). You can also declare
it in the Declarations section of a standard module with the Public keyword
to make it available anywhere in the database.

When passing the value using parameters there are 2 ways, ByVal (by value)
and ByRef (by reference). VB uses ByRef by default. When the value is passed
ByRef, the address to the memory location of the variable is passed. What
this allows you to do is to change the value in the called procedure and
that change will be visible in the calling procedure when you return. If you
pass ByVal, just the value is passed to a new variable in a new memory
location.

Example:
Use the following to call MySub and pass the variable MyVariable:
MySub (MyVariable)

The called routine (MySub) would look something like this:
Private Sub MySub(VariableName As String)
'Do stuff here. You would use VariableName to refer to the passed value
'This is ByRef so if you change the value of VariableName then
'the value of MyVariable in the calling procedure will change also.
End Sub

The passed variables are used by the ones defined in the Sub statement in
left to right order. There are other options such as Optional parameters
that require a little different handling, but the basic results are the
same.

It is usually recommended to not change the built-in parameters that you
will see in some routines, such as the Cancel parameter in a form's Open
event.
 
Wayne,

Thanks for the explanation. I now have a better
understanding and was able to use it find the solution.

Rohit
 
Back
Top