Using Global Variables (1 Viewer)

leolanza

Registered User.
Local time
Today, 21:18
Joined
Dec 4, 2009
Messages
10
Hello everyone and thanks for reading this.

Im developing a database and theres one part of it i seem to be seriously struggling on as im new to VBA.

My system as a login that is hard coded into a form and draws from a query in the database.

What i would like to do is declare a global variable that i can store the username in and use that variable in other forms.

Ive never used global variables before. Ive looked at other sights and know how to declare a global variable, but do not understand how i can set the list values of usernames to it.

Ive declared a glaobal variable in a module names UserSession like

'Global UserSession as String'

The form with a list of usernames has the syntax

List0.value = UserSession

but this fails to work

Can anyone assist? Thankyou in advance!
 
Global was an old word, but still should work, however, try the following

Code:
Public strUserName As String

Then in your login screen pass the username from the textbox to the variable

Code:
strUserName = Me.UserName

Then to test this on your main form go to the form load property and enter

Code:
Me.Caption = "User: " & strUserName

So when you get the main menu you should se the users name on the form caption. If this works then you can have proved that the public variable has been populated correctly.

David
 
Global Variables are simply Public variables in a Standard module. They need to be declared before and outside of any procedures in the module. I personally prefer to use a housekeeping form that is the first form to load from the AutoExec macro and it is hidden. It is also the last form to close and therefore you can use its Unload event to cancel a quit unless your criteria is met. Controls (hidden or not) are available to store the data you need and retrieve from any other form through the Forms collection. Just my $0.02.
 
Thanks for your assistance

I placed the code in and got an unexpected message

'Compile Error

Expected Variable or Procedure, not Module'

I placed the code in teh Command_Click of the event button.


Should I have placed it somewhere else?
 
It appears that you have named the module the same as the variable. You need to rename the module.

David
 
Hi. A question regarding the use of Global Variables in a Form. Once the variable has been defined and has a value attached, is there a way to display it in a textbox on the Form other than via code. Each time I put the variable name (eg '=strField') in the Control Source properties of the text box, it displays the usual error message of '#Name?'. Am I stuck with filling the text box using code.
 
Hi. A question regarding the use of Global Variables in a Form. Once the variable has been defined and has a value attached, is there a way to display it in a textbox on the Form other than via code. Each time I put the variable name (eg '=strField') in the Control Source properties of the text box, it displays the usual error message of '#Name?'. Am I stuck with filling the text box using code.
That's correct. There are other ways of passing a value from one form to another if that's the main goal.

You can use the OpenArgs argument in the Docmd.Openform method.

Or are you using that variable value to filter the record source of a form?
 
Last edited:

Users who are viewing this thread

Back
Top Bottom