Solved Temp Vars and switching users

mistyinca1970

Member
Local time
Today, 11:30
Joined
Mar 17, 2021
Messages
117
Can I simply reuse my login form to switch users. My question is basically, if I assign a Temp Vars, does it replace any existing Temp vars, or is there another bit of code I'll need to use to clear the old Temp Vars first?
Here is the code for my login form:
Code:
Private Sub btnLogin_Click()
    Dim rs As DAO.Recordset
    Dim db As Database
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryLoginAccess", dbOpenSnapshot, dbReadOnly)
        
    rs.FindFirst "Username='" & Me.txtUserName & "'"
    
    If rs.NoMatch = True Then
        Me.lblIncorrectLogin.Visible = True
        Me.txtUserName.SetFocus
        Exit Sub
    End If
    Me.lblIncorrectLogin.Visible = False
    
     
    If rs!Password <> Me.txtPassword Then
        Me.lblIncorrectLogin.Visible = True
        Me.txtPassword.SetFocus
        Exit Sub
    End If
    Me.lblIncorrectLogin.Visible = False
    
    TempVars("UserType") = rs!UserAccess_ID.Value
    TempVars("EmailAddress") = Me.txtUserName.Value
    
        
    If rs!UserAccess_ID = 1 Then
        Dim prop As Property
        On Error GoTo SetProperty
        Set prop = CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, False)
        
        CurrentDb.Properties.Append prop
    End If
        
SetProperty:
     
    DoCmd.OpenForm "frmUpcomingContracts"
    DoCmd.Close acForm, Me.Name
    
        
End Sub
 
That's fine. A correct login will populate the existing tempvars with the new values.

You may wish to clear their values at the beginning of the proc in case of failed login, where the tempvars would retain their previous values.

eg
Code:
 ' ...
 Dim db As Database
 
    TempVars("UserType") = Null
    TempVars("EmailAddress") = Null

    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryLoginAccess", dbOpenSnapshot, dbReadOnly)
' ...

hth,

d
 
That's fine. A correct login will populate the existing tempvars with the new values.

You may wish to clear their values at the beginning of the proc in case of failed login, where the tempvars would retain their previous values.

eg
Code:
 ' ...
Dim db As Database

    TempVars("UserType") = Null
    TempVars("EmailAddress") = Null

    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryLoginAccess", dbOpenSnapshot, dbReadOnly)
' ...

hth,

d
Thank you! This works!
 

Users who are viewing this thread

Back
Top Bottom