I got this from a post by Howard Brody. I have not tried
it but it looks preety straight forward.
OK, a couple of assumptions:
Your login form has two TextBoxes (txtUserID and
txtPassword) and a CommandButton (cmdLogIn)
Your database has a global variable (gstrUserID) for
storing the current user tblLogIn has only one record for
each user and UserID and Password fields.
The user enters their UserID and Password and then clicks
cmdLogIn. Try this code in the OnClick event for cmdLogIn:
Private Sub cmdLogIn_Click()
' verify a user id and password have been entered. If not,
display an error message and end the sub
If IsNull([txtUserID]) Or [txtUserID] = "" Then
MsgBox "You forgot to enter your UserID. Please try
again.",,"Oops!"
txtUserID.SetFocus
Exit Sub
End If
If IsNull([txtPassword]) Or [txtPassword] = "" Then
MsgBox "You forgot to enter your password. Please try
again.",,"Oops!"
txtPassword.SetFocus
Exit Sub
End If
' otherwise...
' declare variables
Dim i as Integer
Dim strUserID as String
Dim strPWEntered As String
Dim strPWActual As String
strUserID = [txtUserID]
strPWEntered = [txtPassword]
' verify the UserID is valid (in the table). If not,
display and error message and end the sub.
' This validation can also be done in the AfterUpdate event
of the txtUserID control
i = DCount("[UserID]","tblLogIn","[UserID]='" & strUserID &
"'")
If i =0 Then
MsgBox "You have entered an invalid UserID. Please try
again.",,"Oops!"
[txtUserID] = ""
[txtPassword] = ""
txtUserID.SetFocus
Exit Sub
End If
' look up actual password
strPWActual = DLookUp("[PAssword]","tblLogIn","[UserID]='"
& strUserID & "'")
' compare passwords. If they match, log in and set the
global UserID variable. If they don't, display
' an error message and end the sub
If strPWEntered = strPWActual Then
MsgBox "You are logged into the database"
gblnUserID = strUserID
' any other login code here
Exit Sub
Else
MsgBox "You have entered an invalid password. Please
try again.",,"Oops!"
[txtUserID] = ""
[txtPassword] = ""
txtUserID.SetFocus
Exit Sub
End If
End Sub
Hope this helps!
Jim