Solved Temp Vars and switching users (1 Viewer)

mistyinca1970

Member
Local time
Today, 04:57
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
 

cheekybuddha

AWF VIP
Local time
Today, 12:57
Joined
Jul 21, 2014
Messages
997
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
 

mistyinca1970

Member
Local time
Today, 04:57
Joined
Mar 17, 2021
Messages
117
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

Top Bottom