Peter Peterson
Member
- Local time
- Tomorrow, 02:21
- Joined
- Sep 14, 2020
- Messages
- 43
Good morning,
I am looking too hard and suspect I can't see the obvious error in my ways - noting I an new to this.
If I enter a correct Username and leave the Password field blank my code seems crash - see "CRASH" below as comment
Any guidance will be most welcome
Thanking you
Peter
I am looking too hard and suspect I can't see the obvious error in my ways - noting I an new to this.
If I enter a correct Username and leave the Password field blank my code seems crash - see "CRASH" below as comment
Any guidance will be most welcome
Thanking you
Peter
Code:
'======================================================================
' NAME: frmLogin
' PURPOSE: Login form restricting access to application
' AUTHOR: Peter Peterson
' DATE: July 2021
'======================================================================
Option Compare Database
Option Explicit
'-----------------------------------------------------------------------
' Name: btnExit_Click
' Purpose: Close the Login form
'-----------------------------------------------------------------------
Private Sub btnExit_Click()
On Error GoTo Err_btnExit_Click ' Initialise error handling
DoCmd.Close ' Close the Login form
Exit_btnExit_Click:
Exit Sub
Err_btnExit_Click:
MsgBox Err.Description
Resume Exit_btnExit_Click
End Sub
'---------------------------------------------------------------------------
' Name: btnLogin
' Purpose: The check the credentials of the user and determine
' whether they can gain access to the application.
'----------------------------------------------------------------------------
Private Sub btnLogin_Click()
Dim rs As Recordset
Dim myUsername As String
Dim myPassword As String
On Error GoTo Err_btnLogin_Click
Set rs = CurrentDb.OpenRecordset("tblUsers", dbOpenSnapshot, dbReadOnly)
' Check if Username field is empty - if so, reset username and password fields
If IsNull(Me.txtUserName) Or Me.txtUserName = "" Then
MsgBox "Username is empty. You must enter a valid Username.", vbOKOnly, "LOGIN Error"
Me.txtPassword.SetFocus
Me.txtPassword.Text = ""
Me.txtUserName.SetFocus
Me.txtUserName.Text = ""
Exit Sub
Else
' Check if Username is correct
Me.txtUserName.SetFocus
myUsername = Me.txtUserName.Text
If rs!UserName <> Me.txtUserName Then
' If Username incorrect - reset Password and Username fields
MsgBox "Incorrect Username entered.", vbOKOnly, "LOGIN Error"
Me.txtPassword.SetFocus
Me.txtPassword.Text = ""
Me.txtUserName.SetFocus
Me.txtUserName.Text = ""
Exit Sub
Else
MsgBox "CORRECT USERNAME", vbOKOnly, "LOGON Progressing"
End If
End If
' Check if Password field is empty
MsgBox "checking password is not empty"
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then ' Seems to CRASH here???
MsgBox "Password is empty. You must enter a Password.", vbOKOnly, vbInformation, "LOGIN Error"
Me.txtPassword.SetFocus
Me.txtPassword.Text = ""
Exit Sub
Else
' Check if Password is correct
Me.txtPassword.SetFocus
myPassword = Me.txtPassword.Text
If rs!UserPassword <> Me.txtPassword Then
MsgBox "Incorrect Password entered.", vbOKOnly, "LOGIN Error"
Me.txtPassword.SetFocus
Me.txtPassword.Text = ""
Exit Sub
Else
MsgBox "Password Correct", vbOKOnly, "LOGIN Successful"
DoCmd.Close acForm, "frmLogin"
DoCmd.OpenForm "frmMain"
End If
End If
Exit_btnLogin_Click:
DoCmd.Close
Exit Sub
Err_btnLogin_Click:
MsgBox Err.Description
Resume Exit_btnLogin_Click
End Sub