Login form

acarter1

Registered User.
Local time
Today, 09:13
Joined
Oct 15, 2009
Messages
27
:mad::mad:
This is killing me, i cant figure out the problem in my code..

i have a login form called frmLogin
Combo box on this form called: cboEmployee
Text box on this form called: txtPassword
on this form exit button which works, the login button is called: cmdLogin

This is the code behind the login button:

Private Sub cmdLogin_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this
'matches value chosen in combo box
If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", _
"[lngEmpID]=" & Me.cboEmployee.Value) Then
lngMyEmpID = Me.cboEmployee.Value
'Close logon form and open splash screen
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "frmSplash_Screen"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtPassword.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub

I get an error of: 2001: "You cancelled the previous operation."
when i enter a username and password and press login.

what is wrong with this code???
 
That error message means that the DLookup is returning a NULL value. So, are you sure that you are looking up a NUMBER for the employee and not text. If it is TEXT then you need to includes quotes:

If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", _
"[lngEmpID]=" & Chr(34) & Me.cboEmployee.Value & Chr(34)) Then
 
well actually the combo box is a list of usernames, (like jbloggs1) so the employee id can't be equal to the value of the combo box haha, you spark off a brainwave! nice one mate i'll get back to you and let you know how it goes tomorrow!
 
Yes! you did spark a bit of a brainwave, i have also added a little code on the form_load of the login to hide the database window and some on the click event of the login button to show the database window, it is all working great now! My code is as follows:

DoCmd.SelectObject acTable, , True
'Check to see if data is entered into the UserName combo box
If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If
'Check value of password in tblEmployees to see if this
'matches value chosen in combo box
If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", _
"[strEmpName]=" & Chr(34) & Me.cboEmployee.Value & Chr(34)) Then
Module1.lngMyEmpID = Me.cboEmployee.Value

'Close logon form and open splash screen
DoCmd.Close acForm, "frmLogin", acSaveNo
DoCmd.OpenForm "frmSplash"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.txtPassword.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub

Thanks again for your help!
 

Users who are viewing this thread

Back
Top Bottom