La définition et l'appel d'une variable globale dans VBA

J'ai un écran de connexion qui compare les données via un Dlookup pour authentifier les utilisateurs. Je voudrais créer une variable globale sur une connexion correct que toute forme, sur la base de données peut appeler, puis ouvrez un formulaire basé sur ce que cette valeur est. Donc actuellement, j'ai tout configuré comme tel.
FORMULAIRE DE CONNEXION :

Option Compare Database
Public gstrUsr As String

FORMULAIRE DE CONNEXION:

Public Sub Command4_Click()
'Sets the login time to now and then authenticates user credentials
Dim usr As String
Me.Time = Now
Dim lvl As String
Dim lck As Integer
Dim sql As String
Dim msgapp As Integer
Dim chkusr As Variant
chkusr = Nz(DLookup("[Username]", "Login", "[Username]='" & Me.Username.Value & "'"), "")
msgapp = 0
usr = Nz(DLookup("[Password]", "Login", "[Username]='" & Me.Username.Value & "'"), "")
lvl = Nz(DLookup("[Level]", "Login", "[Username]='" & Me.Username.Value & "'"), "")
sql = "INSERT INTO Log ( [User], [Time] )SELECT [Forms]![Login]![Username] AS Expr1, [Forms]![Login]![Time] AS Expr2;"
''" & [Forms]![ItemList1]![SRCB] & "'"
'Runs above sql which adds a time record for the selected username also removes the "You are about to update X rows", will use this in the future on the accounting functions
If chkusr = "" Then msgapp = 1
If chkusr = "" Then MsgBox ("Invalid Credentials")
DoCmd.SetWarnings False
DoCmd.RunSQL (sql)
DoCmd.SetWarnings True
'If password is = to the value that is returned in the "usr" variable declared at the top via Dlookup it will open a form based on what that users "level" is otherwise displays and invalid credentials message box
Do While msgapp = 0
If usr = Me.Password.Value Then
lck = 1
msgapp = 3
Else
MsgBox ("Invalid Credentials")
msgapp = 3
End If
Loop
Do While lck = 1
If lvl = "2" Then
DoCmd.OpenForm "MainB"
gstrUsr = DLookup("[Username]", "Login", "[Username]='" & Me.Username & "'")
lck = 0
Else
DoCmd.OpenForm "Main"
lck = 0
End If
Loop
End Sub

FORMULAIRE QUI se CHARGE une fois la CONNEXION RÉUSSIE: (formulaire Principal avec des boutons pour obtenir d'autres formes, j'ai inclus une zone de texte afin que je puisse voir si l'information est passée à la deuxième forme)

Private Sub Form_Load()
Me.Text75 = gstrUsr
End Sub

Comment puis-je obtenir la variable globale de passer à la deuxième forme?

InformationsquelleAutor Rynoc | 2015-10-12