Questions on using global variables

G

Gary Schuldt

I haven't (successfully) used global variables yet, so I have a couple of
questions. The general context here is a multi-user environment with user
profiles (quite rudimentary at this point in the development).

1. Declaring

I assume I at least declare Public pubVar As . . . outside of any particular
module in a Modules object (container).

Q1: Do I also declare it using the same syntax in every module in which I
want to refer to it?

2. Profile parameters

I have a few users already. I'd like to put their profile parameters
(fields) into a tblUsers.

When a user logs on, I could store their UserID in a Public pubIntUserID.
Then, whenever I wanted to use a particular User.field value, I could look
it up in the tblUsers.

Or, I could copy ALL the parameters for that UserID into Public variables
and use them that way in the modules that need them.

Q2: Any comments on which is preferable from a best practice point of view?

Q3: Any other things to watch out for in this approach?

Thanks in advance.

Gary
 
K

Ken Snell

Answers/comments inline....

--
Ken Snell
<MS ACCESS MVP>

Gary Schuldt said:
I haven't (successfully) used global variables yet, so I have a couple of
questions. The general context here is a multi-user environment with user
profiles (quite rudimentary at this point in the development).

1. Declaring

I assume I at least declare Public pubVar As . . . outside of any particular
module in a Modules object (container).

Q1: Do I also declare it using the same syntax in every module in which I
want to refer to it?

No, you would declare it only once in the public module. If you declare it
again inside another module, it'll become a local variable within that
module with different values.

2. Profile parameters

I have a few users already. I'd like to put their profile parameters
(fields) into a tblUsers.

When a user logs on, I could store their UserID in a Public pubIntUserID.
Then, whenever I wanted to use a particular User.field value, I could look
it up in the tblUsers.

Or, I could copy ALL the parameters for that UserID into Public variables
and use them that way in the modules that need them.

Q2: Any comments on which is preferable from a best practice point of
view?

DLookup function to get the values from a table would be preferable in my
opinion. Global variables will lose their values if there is an unhandled
error while the code runs. As such, I find it preferable to avoid their use
and instead use hidden textboxes on forms or reports, or use tables, for
storing long-term variables such as you envision. They are not prone to
"loss" in these situations.

Q3: Any other things to watch out for in this approach?

See above. I usually use global variables in ACCESS strictly within a form
module and usually for short-term things, after I have thoroughly
reviewed/tested my code to be sure that all possible errors will be handled
by error-handling code. Also, if someone else will make changes to your code
(or even you for example) and you/that person don't realize that a variable
name is already being used as a global variable, then you could get
confusion if the variable is reused in another module. If you do use global
variables, name them in nonobvious ways (for example, I often put glb or x
or some other prefix in front of my "normal" variable naming convention:
strPersonName becomes glbstrPersonNamex or such) so that it's less likely
this would occur.

Note also that global constants do not suffer this "loss" behavior with
errors ... I use them frequently in my code.
 
G

Gary Schuldt

Ken,

thanks for the useful insight into global variables. I think your message
answers all the questions I have for now!

Thanks.

Gary
 
E

Eric Cárdenas [MSFT]

I haven't (successfully) used global variables yet, so I have a couple of
questions. The general context here is a multi-user environment with user
profiles (quite rudimentary at this point in the development).

1. Declaring

I assume I at least declare Public pubVar As . . . outside of any particular
module in a Modules object (container).

Q1: Do I also declare it using the same syntax in every module in which I
want to refer to it?

2. Profile parameters

I have a few users already. I'd like to put their profile parameters
(fields) into a tblUsers.

When a user logs on, I could store their UserID in a Public pubIntUserID.
Then, whenever I wanted to use a particular User.field value, I could look
it up in the tblUsers.

Or, I could copy ALL the parameters for that UserID into Public variables
and use them that way in the modules that need them.

Q2: Any comments on which is preferable from a best practice point of view?

Q3: Any other things to watch out for in this approach?

Thanks in advance.

Gary
--------------------
Hi Gary,

You need only define the global variable and assign a value once. Typically
you would have an autoexec macro with a Runcode action to initialise the
global variable. From then on, the global variable is available in any or
all modules.

Hope this helps,
 
G

Gary Schuldt

Thanks, Eric, that helps.

Gary

Eric Cárdenas said:
--------------------
Hi Gary,

You need only define the global variable and assign a value once. Typically
you would have an autoexec macro with a Runcode action to initialise the
global variable. From then on, the global variable is available in any or
all modules.

Hope this helps,

--
Eric Cárdenas
Senior support professional

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Top