re-using variable throughout code

  • Thread starter Thread starter gator
  • Start date Start date
G

gator

On Form_Load I open a table and grab a value to be inserted in a field on my
Form.
I might use this value on another part of the form. How do I store this
value so I can use it throughout my code?
 
Store it in a hidden control on the form (text box with visible property =
false).
Remember to initialize the control's value when the form is loaded.
Sometimes you can also use the tag property of an existing control to store
a value.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
On Form_Load I open a table and grab a value to be inserted in a field on my
Form.
I might use this value on another part of the form. How do I store this
value so I can use it throughout my code?

How do you 'grab' the value?
What do you mean by "open a table"? Why open a table rather than use
DLookUp to read a value?

A generic way to store and use a variable anywhere in code would be to
declare a variable in the Declarations section of the Form's code.
You could then refer to it whenever you need it.

Option Compare Database
Option Explicit
Dim MyVariable as String

You can then give it a value in the Form's Open or Load event or
Wherever (depends upon where and why you need this variable).

It can be read in any event and assigned to an unbound control:
Me.ControlName = MyVariable
 
gator said:
On Form_Load I open a table and grab a value to be inserted in a field on my
Form.
I might use this value on another part of the form. How do I store this
value so I can use it throughout my code?


If you only want to use it throughout the form, put it in
the declarations section (above all procedures) of the
form's module.

If you require it to be available across multiple form's
and/or reports, then you could make it a global variable by
declaring it as Public a standard module's declaration
section. However, global variables are in general a poor
practice and are reset on any unhandled error in any code
anywhere.

Better than a global variable is using a text box on an
always open (can be invisible) form.
 
Back
Top