Search code examples
vbams-access

Access How do i hard code in a username and password


I am not a coder, but I enjoy tinkering.

I have a access database and I have a login screen, but I want to hard code in a high level username and password into the actual code. The username will be "Developer" the password initially will be "One"

This is what I am doing currently. any assistance would be greatly appreciated.

Private Sub cmdLogin_Click()
On Error GoTo cmdLogin_ClickErr
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("Select * From TLKPeople Where Username =  '" & Me.txtUserName & "' And Password = '" & Me.txtPassword & "'")
    If Not rs.EOF Then
        TempVars.Add "UserName", rs!UserName.Value
        TempVars.Add "Password", rs!Password.Value
        TempVars.Add "Admin", rs!Admin.Value
        TempVars.Add "ReadOnly", rs!ReadOnly.Value
        TempVars.Add "StdUser", rs!STDUser.Value
          If Nz(TempVars!UserName, 0) = "Developer" Then
             DoCmd.ShowToolbar "Ribbon", acToolbarYes
           End If
        DoCmd.Close acForm, Me.Name
        DoCmd.OpenForm "FRMMenuMain"
        DoEvents

    Else
        MsgBox "Your login as failed!", vbOKOnly, "Login Failed"
    End If
    rs.Close
    Set rs = Nothing


Exit Sub

cmdLogin_ClickErr:
    MsgBox ("Err: " & Err.Number & " " & Err.Description)
End Sub

Solution

  • Based on that, this should at least get your started. You will probably need to tweak it a little.

    Private Sub cmdLogin_Click()
        On Error GoTo cmdLogin_ClickErr
        
        If Len(Me.txtUserName) = 0 And Len(Me.txtPassword) = 0 Then
            TempVars.Add "UserName", "Developer"
            TempVars.Add "Password", "One"
        Else
          Dim rs As Recordset
          Set rs = CurrentDb.OpenRecordset("SELECT * FROM TLKPeople WHERE Username='" & Me.txtUserName & "' And Password='" & Me.txtPassword & "'")
          If Not rs.EOF Then
              TempVars.Add "UserName", rs!UserName.Value
              TempVars.Add "Password", rs!Password.Value
              TempVars.Add "Admin", rs!Admin.Value
              TempVars.Add "ReadOnly", rs!ReadOnly.Value
              TempVars.Add "StdUser", rs!STDUser.Value
          Else
              MsgBox "Your login as failed!", vbOKOnly, "Login Failed"
              Exit Sub
          End If
          rs.Close
          Set rs = Nothing
        End If
        
        If Nz(TempVars!UserName, 0) = "Developer" Then
             DoCmd.ShowToolbar "Ribbon", acToolbarYes
        End If
        
        DoCmd.Close acForm, Me.Name
        DoCmd.OpenForm "FRMMenuMain"
        DoEvents
    
    Exit Sub
    
    cmdLogin_ClickErr:
        MsgBox ("Err: " & Err.Number & " " & Err.Description)
    End Sub
    

    Note: You may want to use an Or instead:

    If Len(Me.txtUserName) = 0 Or Len(Me.txtPassword) = 0 Then