Solved Problem in Storing Value in Public Variable for Login Logout Button (1 Viewer)

Pac-Man

Active member
Local time
Today, 20:15
Joined
Apr 14, 2020
Messages
408
Hello,

I've been trying to follow total to create login system mentioned on this link. I'm saving access level in a string strAcsLvl. 0 means no user is logged in whole 1 is for developer, 2 for administrator (except 0 value, other AccessLevelID are picked from tblUser.) I've modified the code as below

Rich (BB code):
Option Compare Database
Dim strAcsLvl As String

Private Sub txtPassword_AfterUpdate()

'Check that EE is selected
If IsNull(Me.cboUser) Then
    MsgBox "You need to select a user!", vbCritical
    Me.cboUser.SetFocus
Else
    'Check for correct password
    If Me.txtPassword = Me.cboUser.Column(2) Then
        'Check if password needs to be reset
        If Me.cboUser.Column(3) = True Then
            DoCmd.OpenForm "frmPasswordChange", , , "[UserID] = " & Me.cboUser
        End If
                If DLookup("[AccessLevelID]", "tblUser", "[UserID] = " & Forms!frmLogin!cboUser) = 1 Then
                SetProperties "AllowBypassKey", dbBoolean, True
                MsgBox "Developer access verified. Hold shift key while opening the app to open as developer."
                DoCmd.Close
                DoCmd.Quit acQuitSaveAll
                End If
       'DoCmd.OpenForm "frmMainMenu"
       strAcsLvl = Nz((DLookup("[AccessLevelID]", "tblUser", "[UserID] = " & Forms!frmLogin!cboUser)), 0)
       Forms![frmMore]![cmdLogin].Caption = "Logout"
       DoCmd.Close
    Else
        MsgBox "Password does not match, please re-enter!", vboOkOnly
        Me.txtPassword = Null
        Me.txtPassword.SetFocus
    End If
End If
End Sub

I've placed following code in the form update event of the form where cmdLogin is created to open a login form so that when strAcsLvl value is zero, it should show caption of button as Login otherwise Logout.

Rich (BB code):
Private Sub Form_Load()
    If strAcsLvl = 0 Then
        Me.cmdLogin.Caption = "Login"
        
    Else
        Me.cmdLogin.Caption = "Logout"
       
    End If

End Sub

Now problem I'm facing is, even though I defined strAcsLvl as global variable but when login form is closed and I reopen my form on which cmdLogin is placed, it still shows Login which does that strAcsLvl is cleared or has not retained the AccessLevelID in it. I am new to Access and don't where I'm doing wrong. Kindly help.

Best Regards,
Abdullah
 

Minty

AWF VIP
Local time
Today, 15:15
Joined
Jul 26, 2013
Messages
10,354
You need to put your global variable in a separate module, not the forms code module.
Once the form is closed that variable will be destroyed, it's only "in circulation" while that form is open.
 

Pac-Man

Active member
Local time
Today, 20:15
Joined
Apr 14, 2020
Messages
408
You need to put your global variable in a separate module, not the forms code module.
Once the form is closed that variable will be destroyed, it's only "in circulation" while that form is open.
Thanks for reply. Do i have to define a new independent module for this variable or i can define it in already created module? My DB already contain a module which defined setproperty function. Can I put the following code in that module after Option Compare Database line?

Code:
Dim strAcsLvl As String
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:15
Joined
May 7, 2009
Messages
19,169
you declare it as:

Public strAcsLvl As Integer
or
Global strAcsLvl As Integer

on any Standard module (new or existing).
 

Pac-Man

Active member
Local time
Today, 20:15
Joined
Apr 14, 2020
Messages
408
You need to put your global variable in a separate module, not the forms code module.
Once the form is closed that variable will be destroyed, it's only "in circulation" while that form is open.
Thanks a lot. Problem is solved.
 

Users who are viewing this thread

Top Bottom