I have a Login form that has 2 text boxes that are currently unbound. One for Username and the other for Password. I created a backend table called tbl_LoggedIn where I plan to keep records of when the users login and logout. In that table I created these fields: UserID, Login Date, Login Time, Logout Date, and Logout Time. I made the record source for the Login form to be with tbl_LoggedIn and bound the username text box to UserID that is in the tbl_LoggedIn table. I have code behind the login button that will do the check for the username and password in the tblUser table and if there is a match it will set the Login form to hidden and load a splash screen and take the user to the main menu. The reason why the Login form is set to hidden is because I have other unbound text boxes throughout the database to let the users know who is logged in at the moment. It pulls the info from the hidden Login form's username textbox.
I assume to get this Login date/time log going for the tbl_LoggedIn table, I would need to do some VBA for the login button. Correct me if I'm wrong, but if the user is granted access based on their UserID credentials, I can have VBA do a save for the Login form and that will cause the username to be saved to the tbl_LoggedIn table. Then I can do more VBA to auto-populate the Login Date and Login Time for that same table correct? I won't worry about the logout process yet as I would like to focus on this first logging-in part first.
Here is the current code for the login button on the Login form:
Code:
Private Sub cmdLogin_Click()
Dim validCredentials As Integer
Dim userLevel As Variant
Dim ID As Integer
validCredentials = DCount("Username", "[tblUser]", "[Username] ='" & txtUsername & "' AND [Password]='" & txtPassword & "'")
If IsNull(Me.txtUsername) Then
MsgBox "Please Enter Username", vbInformation, "Username Required"
Me.txtUsername.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password", vbInformation, "Password Required"
Me.txtPassword.SetFocus
Else
ID = DLookup("UserID", "tblUser", "Username = '" & Me.txtUsername.Value & "'")
If validCredentials = 1 And Me.txtPassword = "password" Then
DoCmd.Close
MsgBox "Please Change Password", vbInformation, "New Password Required!"
DoCmd.OpenForm "Change Password", , , "[UserID] = " & ID
Else
If validCredentials = 1 Then
userLevel = Nz(DLookup("UserSecurity", "[tblUser]", "[Username]='" & txtUsername & "'"), 0)
If userLevel = "Admin" Then
DoCmd.Close
DoCmd.OpenForm "Main Menu_admin"
ElseIf userLevel = "Supervisor" Then
DoCmd.OpenForm "Splash Screen Load_sup"
Else
DoCmd.OpenForm "Splash Screen Load"
End If
Else
MsgBox "Invalid Username Or Password!", vbExclamation, "UNAUTHORIZED!"
Me.txtPassword.SetFocus
End If
End If
End If
Exit Sub
End Sub
Would adding the DoCmd.RunCommand acCmdSaveRecord right before the user gets the splash screen load save the username into the tbl_LoggedIn table? If so, how would I also get a date and time into that table if there are no fields for it in the Login form? Also, would you have any suggestions on if I should also count the password change or even going into the admin side of the database as an official Login with a date and time for when that user did those things? Or should those types of logins be ignored?
Thanks for the help.