Definition und Aufruf eine Globale variable in VBA

Ich habe ein login-Bildschirm, die vergleicht die Daten über ein Dlookup, um Benutzer zu authentifizieren. Ich möchte erstellen Sie eine Globale variable auf eine korrekte Anmeldung, dass jede form in der Datenbank aufrufen kann auf öffnen und dann ein Formular basierend auf, was das Wert ist. Also derzeit habe ich alles so aufgebaut.
LOGIN-FORMULAR :

Option Compare Database
Public gstrUsr As String

LOGIN-FORMULAR:

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

FORM, LÄDT NACH dem ERFOLGREICHEN LOGIN: (Hauptformular mit buttons gelangen Sie zu anderen Formen, habe ich ein Textfeld, damit ich sehen kann, ob die information weitergegeben wird, um die zweite form)

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

Wie bekomme ich die Globale variable übergeben an die zweite form?

InformationsquelleAutor Rynoc | 2015-10-12
Schreibe einen Kommentar