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!

How do I fix my if statement?

Status
Not open for further replies.

ggreg

Programmer
Mar 9, 2001
201
US
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"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If
 
Hi

Your statement compares the variable "strSQL to the result of the DLookup function. As there is no line in the code that defines strSQL, this causes the problem.

What do you have to do? There are two options:
First is to define the contents of strSQL
Second is to compare the result of the DLookup function to the textbox.

Either way you choose, the same technique will work for the password, but remember you need to ensure you get the password for the username entered.

John
 
Thanks I used :

If strSQL = Me.txtLogin Then
 
That won't work, because you've still not told the computer what is in strSQL.

John
 
your right it took

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

Now i can not get my counter to work look below:

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




DoCmd.Close acForm, "frmLogon", acSaveNo
DoCmd.OpenForm "frmMain"
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
 
Code:
Private Sub Command4_Click()

Dim strName As String, strMsg as String

strName = ""
strMsg = ""

If Len(Me.txtLogin & "") Then
   If Len(Me.txtPassword & "") Then

      strName = DLookup("[strEmpName]", "tblEmployees", _
      "[strEmpName]='" & Forms!frmlogin!txtLogin & "'")

      If Len(strName & "") Then

         Me.EmpName = strName

      Else

         strMsg = "Username / password incorrect"
         Me.txtLogin.SetFocus

   Else
      strMsg = "Enter your password."
      Me.txtPassword.SetFocus
   End If

Else
   strMsg = "Enter user name."
   Me.txtLogin.SetFocus
End If

If Len(strMsg) Then
   Me.txtBoxCountHidden = Me.txtBoxCountHidden + 1

   If Me.txtBoxCountHidden.Value > 3 Then
      strMsg = strMsg & vbCrLf & "Closing application
      MsgBox strMsg

      Application.CloseCurrentDatabase

   Else

      strMsg = strMsg & vbCrLf & Me.txtBoxCountHidden _
      " of 3 Chances used"

      MsgBox strMag
   End If

Else

   DoCmd.Openform "YourNextForm"
   DoCmd.Close

End If

...just a guess

Richard
 
Thanks Richard,
I got it to work before seeing your post but again thanks
for posting just in case I did not come up with it below is what I ended up doing to get it to work:

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 Me.txtLogin = Me.txtPassword Then

DoCmd.OpenForm "frmChangePassword"
Else
If Me.txtLogin = DLookup("[strEmpName]", "tblEmployees", "[strEmpName]='" & Forms!frmLogin!txtLogin & "'") Then




DoCmd.Close acForm, "frmLogon", acSaveNo
DoCmd.OpenForm "frmMain"
Else

MsgBox "Login Name Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 2 Then
MsgBox "You do not have access to this database.Please contact admin.", vbCritical, "Restricted Access!"
Application.Quit
Else
End If

Me.txtLogin.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 If


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top