Try the functions below (you might need to change the SQL Query to match your Table structure)
Code:
Private Function CheckLoginInfo(UserName as String, Password as String) as Boolean
On Error GoTo Error_CheckLoginInfo ' Error checking FTW!
' Declare and initialise our Recordset with a Query that looks up the user's UserName ONLY (password checked in code)
Dim rstInfo as DAO.Recordset
Set rstInfo = CurrentDb().OpenRecordset("SELECT * FROM tblUsers WHERE [UserName]='" & UserName & "'", dbOpenForwardOnly)
' If the RecordCount Property of the Recordset is 0, then no user with that UserName was found
If rstInfo.RecordCount = 0 Then
CheckLoginInfo = False ' Failed Login!
Else
' If a comparison of the password in the database and the user entered password DOES NOT equal 0, then they DO NOT match
If Not StrComp(rstInfo("Password"), Password, 0) = 0 Then
CheckLoginInfo = False ' Therefore failed Login!
Else ' If they DO match
CheckLoginInfo = True ' Successful Login! (YAY!)
End If
End If
' Clean up our Objects and exit the Function
FunctionClosing:
rstInfo.Close
Set rstInfo = Nothing
Exit Function
' If we ran into an error somewhere
Error_CheckLoginInfo:
CheckLoginInfo = False ' Assume Login failed!
Resume FunctionClosing ' Exit the function
End Function
Private Sub LogIn_Click()
On Error GoTo FunctionClosing ' Error checking FTW!
Dim boolGoodLogin As Boolean
' If either the UserName or Password are blank, force the user to enter them before continuing
If IsNull(Me!txtUserName) Then
MsgBox "Please enter a User Name", vbInformation, "Missing User Name"
Me!txtuserName.SetFocus()
Else If IsNull(Me!txtPassword) Then
MsgBox "Please enter a Password for the user name " & Me.txtUserName & ".", vbInformation, "Missing User Name"
Me!txtPassword.SetFocus()
Else ' If both a UserName and Paswrod were entered
boolGoodLogin = CheckLoginInfo(Me!txtuserName, Me!txtPassword) ' Check them against the database
' If the informatched and the login was successful
If boolGoodLogin = True Then
DoCmd.OpenForm "frmMenu" ' Open our new Form
' If our user was bschank and had the correct password
If txtUserName = "bschank" Then
DoCmd.ShowAllRecords ' Show everything!
End If
DoCmd.Close acForm, "frmLogOn" ' Close the Login Form
Else ' If the login failed, alert the user and clear their last attempted password
MsgBox "Your log on information was incorrect. Try again.", vbCritical, "Failed Log On"
Me!txtPassword = Null
Me!txtPassword.SetFocus()
End If
End If
End Sub