Defining Global Variables

dealwi8me

Registered User.
Local time
Today, 10:21
Joined
Jan 5, 2005
Messages
187
Where should i define a global variable in order to be accesible from all forms?
 
In a standard module just under:
Option Compare Database
Option Explicit

And before and Functions or Sub's
 
Here what i did.

I defined a global variable Username to form1.
When i assign this variable to form2 i can't get the value.
I assign the value to form2 as follows: me!textfield=Username

What do i do wrong?

Thank you in advance.
 
Hi

I don't think the variable has been globally defined in the first place by the inclusion of the Option Explicit statement and you have to declare it publically in a module (if I remember correctly).

However, I was advised, rightly or wrongly, not to define variables globally in Access as you can run into troubles. To avoid the global declaration I created a table in Access and stored the user as they logged onto the database - together with the number of attempts made to log on etc. And everytime I wanted the User I just referenced this table in my code.

This may not be the best way and if I was to do this again I would get the user from the NT logon.

Does this help you?
 
Victoria has some good points worth reviewing. As I stated earlier, the Global Variables need to be in a Standard Module and not a Form Module.
 
dealwi8me said:
but how you recognize which user is each time?

This gets the user's login (from Windows) and puts it in strUser.

Code:
Dim strUser As String
strUser = Environ("USERNAME")

You can get a good deal of information from Environ. You'll be wanting to know more next...

Code:
Dim myEnv As New Collection
Dim i As Integer
Dim x As Integer
i = 1
Do While Environ(i) <> ""
    myEnv.Add (Environ(i))
    i = i + 1
Loop
' Print Environmentals to immediate window
For x = 1 To myEnv.Count
    Debug.Print myEnv.Item(x)
Next x

That will print to the "Immediate" window
 
i used the windows login name and works.

Thanks a lot guyz:)
 
Is there a way to assign environ("username") as a default value in a table?
 
did you try TempVars! in your application (access 2007)

I use them as global variables in my application
 
Last edited:
I've put the username into Employees and pushed their Initials (for brevity) onto the main Menu form and on all subsequent forms, I drag the Employee from the Main form using the default property.

Simon
 

Users who are viewing this thread

Back
Top Bottom