Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop Login form from closing if incorrect info entered.

Status
Not open for further replies.

1ooo1

Technical User
Jan 20, 2003
50
AU
Hi,
In the following example of a Login form's code is there any way for me to prevent the form from closing if a user has entered an incorrect password or user name. I want the form to remain, so that the user can try again!
(This is not my own form or code)


Private intLogonAttempts As Integer

'-- code for Click event of cmdEnter_Click()
Public Sub cmdEnter_Click()
Dim rst As DAO.Recordset
Dim strSQL As String

'-- test username given, exit sub if not
If Nz(txtname) = "" Then
MsgBox "Use your first name as your User Name!!", vbInformation, "No User Name - How do I know who you are?"
Forms!Login!txtname.SetFocus

Exit Sub
End If

'-- test password given, exit sub if not
If Nz(txtPassword) = "" Then
MsgBox "Good try, but you need a password!!", vbInformation, "Do you really expect to get in with no password!!"
Forms!Login!txtname.SetFocus
Exit Sub
End If

'-- create query to get record for specifed username
strSQL = "SELECT [UserPassword],[AccessLevel] FROM [Students] WHERE [StudentName]='" & txtname & "';"

'-- run query
Set rst = CurrentDb.CreateQueryDef("", strSQL).OpenRecordset

'-- test if record found for given username, if not; incorrect username given
If Not rst.EOF Then

'-- test if password matches password in users record
If rst("UserPassword") = txtPassword Then

'-- correct password! now determine accesslevel, and open correct form
Select Case rst("AccessLevel")
Case "User"

'-- open user form
DoCmd.OpenForm ("frmReports")
Case "Manager"

'-- open manager form
DoCmd.OpenForm ("frmManager")
End Select
Else

'-- code to handle incorrect password
MsgBox "Password Incorrect. Please try again", vbExclamation, "Password Incorrect"
Forms!Login!txtPassword.SetFocus
End If
Else

'-- code to handle invalid username
MsgBox "Check your UserName - Yours is wrong - Use your first name.!!", vbExcalamation, "Invalid UserName"
Forms!Login!txtname.SetFocus
End If

'-- close up recordset object
rst.Close
Set rst = Nothing

'-- and finally, increment the logon attempt counter, quit access after 3 attempts!
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database. Please contact the Customer Service Supervisor.", vbCritical, "Restricted Access!"
Application.Quit
End If


'--Close Login Form
DoCmd.Close acForm, "Login", acSaveNo


If intLogonAttempts & gt = 3 Then DoCmd.Quit acSaveNone

End Sub


Ken
 
You could change the if statement below to any number you want.

If intLogonAttempts > 3

To allow infinte attempts, remove this section of the code:

'-- and finally, increment the logon attempt counter, quit access after 3 attempts!
intLogonAttempts = intLogonAttempts + 1

If intLogonAttempts > 3 Then
MsgBox "You do not....", vbCritical, "Restricted Access!"
Application.Quit
End If

Consider remarking out each line with a ', instead of removing it. This way you can reeanble it later if you change your mind.
 
Change this:
'-- and finally, increment the logon attempt counter, quit access after 3 attempts!
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database. Please contact the Customer Service Supervisor.", vbCritical, "Restricted Access!"
Application.Quit
End If


'--Close Login Form
DoCmd.Close acForm, "Login", acSaveNo


TO:
'-- and finally, increment the logon attempt counter, quit access after 3 attempts!
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database. Please contact the Customer Service Supervisor.", vbCritical, "Restricted Access!"
'--Close Login Form
DoCmd.Close acForm, "Login", acSaveNo
Application.Quit
End If

I'm not sure what the last line is doing, since gt is not defined withing the code you posted. If you don't comment it out, the form may still close.
 
Thanx for the replies, but what the problem is is this...if a user tries to login, but has used a wrong password or username, the message box appears and on closing the message box, the form closes, I want the form to stay visible, so that the user can try again...but if they do enter the correct info, I want the form to disappear, I am not really worried about how many attempts they can make.
Does that make sense, I can email a sample of what I have for you to look at if required.

Ken
 
You can use Dlookup to find out if there is a matching record for user name and password and store the access level in a variable for later use:


Public varAccessLevel 'to use it later, place it in a module



'-- code for Click event of cmdEnter_Click()
Public Sub cmdEnter_Click()

Static intLogonAttempts As Integer 'static means that it preserves the value between calls

'-- test username given, exit sub if not
If Nz(txtname) = "" Then
MsgBox "Use your first name as your User Name!!", vbInformation, "No User Name - How do I know who you are?"
Forms!Login!txtname.SetFocus
Exit Sub
End If

'-- test password given, exit sub if not
If Nz(txtPassword) = "" Then
MsgBox "Good try, but you need a password!!", vbInformation, "Do you really expect to get in with no password!!"
Forms!Login!txtname.SetFocus
Exit Sub
End If

varAccesLevel = DLookup("IDField","UsersTable","StudentName = '" & txtname & "' And UserPassword = '" & txtpassword & "'")


If IsNull(varAccessLevel) Then
Beep
MsgBox "No such username or password"
'--increment the logon attempt counter, quit access after 3 attempts!
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts >= 3 Then
MsgBox "You do not have access to this database. Please contact the Customer Service Supervisor.", vbCritical, "Restricted Access!"
Application.Quit
End If

Else
'varAccessLevel is stored
Select Case varAccessLevel
Case "User"

'-- open user form
DoCmd.OpenForm ("frmReports")
Case "Manager"

'-- open manager form
DoCmd.OpenForm ("frmManager")
End Select
DoCmd.Close acForm, Me.Name
Exit Sub
End If

In this example, the password is not case sensitive. To make it case sensitive, you'll have to additionaly compare the textbox contents with the value found in the record in a module that has Option Compare Binary instead of Option Compare Database.

You are aware, of course, that such login form is just a joke for what concerns database security...

Good luck



[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top