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
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