View Full Version : If statement not doing what I expect


ggreg
03-26-2005, 10:51 AM
See below in comments what my problem is:

Private Sub Command4_Click()
Dim strSQL As String
If IsNull(Me.txtLogin) Or Me.txtLogin = "" Then
MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.txtLogin.SetFocus
Exit Sub
End If
If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If

If strSQL = DLookup("[strEmpName]", "tblEmployees", "[strEmpName]='" & Forms!frmlogin!txtLogin & "'") Then

'The above is not doing what I think it should be doing
'I know I have a lot more coding but just trying to test this one part
'ok at this point if a name that is in the tblEmployees is there
'instead of it opening frmMain it jumps to the else and tells me
'Password Invalid why is it jumping to the else and what do I have
'to do at this point to get it to open frmMain if a name is in the table?
'Thanks in advance!


DoCmd.Close acForm, "frmLogon", acSaveNo
DoCmd.OpenForm "frmMain"
:confused: Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If

Sergeant
03-26-2005, 11:31 AM
You're not filling the strSQL string before you try to compare it to your DLookup. strSQL is empty.

ggreg
03-26-2005, 11:43 AM
thanks!!!

I ended up using
If strSQL = Me.txtLogin Then

Sergeant
03-26-2005, 11:48 AM
You could just change this line:
If strSQL = DLookup("[strEmpName]", "tblEmployees", "[strEmpName]='" & Forms!frmlogin!txtLogin & "'") Then
to this:
If me.txtLogin = DLookup("[strEmpName]", "tblEmployees", "[strEmpName]='" & Forms!frmlogin!txtLogin & "'") Then

I'm not saying this is optimum, I just think that with the code you presented, this should make it work.

ggreg
03-26-2005, 11:50 AM
wow thanks for posting so quick I just may look at your option too~ :)

ggreg
03-26-2005, 11:52 AM
how do I get bad response to the names in the table meaning
if they put a wrong name in there how do I trap for that error?
because now it not doing the else it is throwing an error

wow I see my way does not work but your way does...
now tho this part of my code is not working:

intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", vbCritical, "Restricted Access!"
Application.Quit

its not counting my attempts

Sergeant
03-26-2005, 12:28 PM
Go to the declarations section at the top of the code page and put this:
Private intLogonAttempts as Byte