Public Variable losing value

ChrisLayfield

Registered User.
Local time
Today, 07:19
Joined
May 11, 2010
Messages
55
When the user logins in, I have a value in the profile to determine their access to the database. I edited out the code that doesn't apply

Code:
Option Compare Database
Public strUserID As String
Public strUserAccess As String
'------------------------------------------------------------
' cbxEmployeeID_AfterUpdate
'
'------------------------------------------------------------
Public Sub cbxEmployeeID_AfterUpdate()  
    strUserID = Forms!frmNCSLogin.cbxEmployeeID
    strUserAccess = Nz(DLookup("dbAccess", "tbl_Employees", _
        "tbl_Employees.EmployeeID = '" & strUserID & "'"), "NoAccess")
End Sub

This part works and in this same form there is a code to open the desired menu
Code:
'------------------------------------------------------------
' cmdEnter_Click
'
'------------------------------------------------------------
Private Sub cmdEnter_Click()
    DoCmd.OpenForm strUserAccess, acNormal
End Sub
But somewhere between here and a submenu of the user menu, the variable strUSerAccess loses its value...to that on a submenu I get an error that it needs a method or form name because strUserAccess ="" for some reason.
Code:
'------------------------------------------------------------
' cmdMainMenu_Click
'
'------------------------------------------------------------
Private Sub cmdMainMenu_Click()
On Error GoTo cmdMainMenu_Click_Err
    On Error Resume Next
        DoCmd.Close acForm, "menuPersonel", acSaveNo
        If strUserAccess <> "menuAdmin" Then
            DoCmd.OpenForm strUserAccess, acNormal, acReadOnly
        End If
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If
cmdMainMenu_Click_Exit:
    Exit Sub
cmdMainMenu_Click_Err:
    MsgBox Error$
    Resume cmdMainMenu_Click_Exit
End Sub
 
I think I figured it out with a little more reading, the issue is that because I am switching from one form to another, the public variable is only available within the subs on that form and not across the datbase. Is that correct? So to make across the entire dB I would need to make it a global variable?
 
Your variable is only public to that form and is lost once the form is closed. You need to put it in a public module, not a form class module.

Code:
Global gsUserID As String
Global gsUserAccess As String
 
perfect and thanks for making it abundantly clear. Will help whoever comes after as well.
 

Users who are viewing this thread

Back
Top Bottom