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
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.
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
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.