Login Script

  • Thread starter Thread starter T.
  • Start date Start date
T

T.

Hi all, hope someone can help me out here....

I need to create a login script that will loop through each record in
tblLogin until both the user and pass fields match. Then I need to store
the username that was entered in a global variable that all other forms will
be able to reference.

I have drawn a blank here so any help would be great. I know that I have
completed this task before but that was a LONG time ago :)

Thanks much in advance,

T.
 
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 use
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 su
If IsNull([txtUserID]) Or [txtUserID] = "" The
MsgBox "You forgot to enter your UserID. Please try again.",,"Oops!
txtUserID.SetFocu
Exit Su
End I

If IsNull([txtPassword]) Or [txtPassword] = "" The
MsgBox "You forgot to enter your password. Please try again.",,"Oops!
txtPassword.SetFocu
Exit Su
End I

' otherwise..

' declare variable
Dim i as Intege
Dim strUserID as Strin
Dim strPWEntered As Strin
Dim strPWActual As Strin

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 contro
i = DCount("[UserID]","tblLogIn","[UserID]='" & strUserID & "'"

If i =0 The
MsgBox "You have entered an invalid UserID. Please try again.",,"Oops!
[txtUserID] = "
[txtPassword] = "
txtUserID.SetFocu
Exit Su
End I

' look up actual passwor
strPWActual = DLookUp("[PAssword]","tblLogIn","[UserID]='" & strUserID & "'"

' compare passwords. If they match, log in and set the global UserID variable. If they don't, displa
' an error message and end the su
If strPWEntered = strPWActual The
MsgBox "You are logged into the database
gblnUserID = strUserI
' any other login code her
Exit Su
Els
MsgBox "You have entered an invalid password. Please try again.",,"Oops!
[txtUserID] = "
[txtPassword] = "
txtUserID.SetFocu
Exit Su
End I

End Su

Hope this helps

Howard Brod

----- T. wrote: ----

Hi all, hope someone can help me out here...

I need to create a login script that will loop through each record i
tblLogin until both the user and pass fields match. Then I need to stor
the username that was entered in a global variable that all other forms wil
be able to reference

I have drawn a blank here so any help would be great. I know that I hav
completed this task before but that was a LONG time ago :

Thanks much in advance

T
 
Thank you Howard, this looks great so far. The only question that I have
right now is that I can't remember where to declare the global variable.
Everything else looks perfect.

Thanks again and if you could fill me in on that last little part it would
be appreciated :)

T.

Howard Brody said:
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!

Howard Brody

----- T. wrote: -----

Hi all, hope someone can help me out here....

I need to create a login script that will loop through each record in
tblLogin until both the user and pass fields match. Then I need to store
the username that was entered in a global variable that all other forms will
be able to reference.

I have drawn a blank here so any help would be great. I know that I have
completed this task before but that was a LONG time ago :)

Thanks much in advance,

T.
 
Back
Top