global variables problems

G

Guest

I am having problems with Global/Public variables in Access 2003.

I define them in a module. I initialize the varibles when the database
startups. However, when I open a form the global variables get reset to empty
strings or 0 depending on the type.Also, within the code of a subform, I can
set a variable to a value, but when I check the value in another subroutine,
the variable has been reset. This happens on several machines, not just my
development system.

Does anyone else have these problems? If so what is the solution?

(I'm a very experienced programmer and have been using public variables for
years. I can't believe that I suddenly forgot how to use public variables.)

BobG
 
D

Douglas J. Steele

Errors can cause global variables to get reset. That's one of the reasons
why using global variables are generally discouraged.

One solution is to have an open form (it can be hidden), and store the
values as controls on that form. This has the added advantage that they can
be used in queries (you'd need to write a function to return the value of a
global variable to use them in queries).

Another approach is to use a class, and have the values returned as
properties of the class.
 
D

Dirk Goldgar

BobG said:
I am having problems with Global/Public variables in Access 2003.

I define them in a module. I initialize the varibles when the database
startups. However, when I open a form the global variables get reset
to empty strings or 0 depending on the type.Also, within the code of
a subform, I can set a variable to a value, but when I check the
value in another subroutine, the variable has been reset. This
happens on several machines, not just my development system.

Does anyone else have these problems? If so what is the solution?

(I'm a very experienced programmer and have been using public
variables for years. I can't believe that I suddenly forgot how to
use public variables.)

I agree with Doug that global variables can be problematic, but what you
describe doesn't sound quite like the usual issues that occur with them.
Could it be that you defined these variables *both* in the standard
module and in the form's module? That would create two separate and
unrelated sets of variables, so the local ones in the form wouldn't have
the values you previously assigned to the ones defined in the standard
module.
 

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