PDA

View Full Version : Access VBA Global Variables



lornemcauley
03-12-2009, 06:59 AM
Hi everyone,

I have an application that has a simple login. At login, the userid and name are stored inside global variables that are declared inside a module. Then on the main form it says "Hello (username)". Pretty simple.

So here's what happens:

1. Off the main form I have several buttons that open other forms. Here is example code of opening another form:

DoCmd.Close
DoCmd.OpenForm "Access_Control", acNormal, "", "", , acNormal

2. On one of these other forms, there is some simple data entry that is validated. User is notified of bad data using messageboxes.

3. If a message box pops up, my global variables are reset! I do not programatically change my global variables except for when they are set when the user completes the login screen.

I've added watches to my globals and it always happens on these damn messageboxes. So what's happening is a user will login, see the home screen with their name on it, go fill out a form, come back to the main screen and their name field is blank.

Very frustrating. Is it due to the way I open / close windows? Are the globals being reset during a split second when the windows are closed?

Any help would be greatly appreciated. No I don't want to store this information inside a table.

Thanks,

Lorne.

OBP
03-12-2009, 07:39 AM
Lorne, welcome to the forum, this behavour is not normal, except if you create the Global Variable in a Form Module. It gets reset when the form is closed.
The correct place to declare the variable as "Public" is in a VBA Module, if it is still happening when declared in a Module that is not soemthing I have come accross before.

CreganTur
03-12-2009, 07:44 AM
At login, the userid and name are stored inside global variables that are declared inside a module.
As Tony stated, anything placed within a Form module will be reset when the Form is closed.

There are 2 types of modules - Form Moduels and Standard Modules. Form Modules hold all of the code behind a form. They are bound to the form and the scope of objects/variables declared within the Form Module will not extend outside of it.

Standard Modules are also simply called Modules (confusing, I know). Objects and variables within Modules can be project wide, unless you declare it to be private.

If this is not the issue, then I have to ask about these message boxes. Are they standard message boxes? If they are, then we need to see a code example. Please wrap all cod examples within VBA tags (the green VBA button).

lornemcauley
03-12-2009, 08:02 AM
Hi everyone, thanks for the quick replies!

I solved the problem!

My global variables where declared public in a separate module like they should be, but they were getting reset when I was doing my validating.

Here's an example:

Public Function ValidatePathogen() As Integer
Dim intVar As Integer

'check if the user chose a name for the form
If cboName.Value = 0 Or IsNull(cboName.Value) Then
intVar = MsgBox("Please choose a username.", vbExclamation)
End
End If

...
End Function

I was using 'END' if validation failed to break out of the operation. It just so happened that I always did this after displaying a messagebox, leading me to think that they were the cause of the issue.

After some googling I realized that I should be using 'Exit Function' instead, this will keep my global variables in tact.

Thanks again!

Lorne McAuley

CreganTur
03-12-2009, 08:35 AM
Glad you figured it out.

If your issue is resolved, then please mark your thread as solved by clicking on Thread Tools at the top of the page, and then select Mark As Solved.

Thanks :thumb