Solved Help with Login Code

slharman1

Member
Local time
Yesterday, 18:44
Joined
Mar 8, 2021
Messages
483
I have a table named tblEmployee with some fields: Password and Login.
For one record, the login field contains steveh and the password field contains 1111, both are short text field types.

Dialog Form with unbound Password and Login fields and an ok and cancel command button prodcues errors

I keep getting the error Run-time error '2471': The expression you entered as a query parameter produced this error: steveh
So it seems to me that the Dlookup statement is returning the login value instead of the password value.
This has got me stumped - I can't figure out what I am doing wrong:

Code:
Private Sub Command1_Click()
    If IsNull(Me.txtLogin) Then
        MsgBox "Please Enter Login", vbInformation, "Need ID"
        Me.txtLogin.SetFocus
    ElseIf IsNull(Me.txtPassword) Then
        MsgBox "Please Enter Password", vbInformation, "Need Password"
        Me.txtPassword.SetFocus
    Else
        If Nz(DLookup("[Password]", "tblEmployee", "[Login] =" & Me.txtLogin), "GarbageEntry") = Me.txtPassword Then
            DoCmd.Close
            MsgBox "Success"
            'DoCmd.OpenForm "Master"
        Else
            MsgBox "Incorrect Login or Password"
        End If
    End If
End Sub
 
I would suspect that txtLogin is a string, so should be surrounded with single quotes?

Plus with that code, could I just not put any name and password of GarbageEntry and be able to login? :unsure:
 
I would suspect that txtLogin is a string, so should be surrounded with single quotes?

Plus with that code, could I just not put any name and password of GarbageEntry and be able to login? :unsure:
Are you referring to Me.txtLogin in my Dlookup statement?
 
If Nz(DLookup("[Password]", "tblEmployee", "[Login] =" & Me.txtLogin), "GarbageEntry") = Me.txtPassword Then
Is supposed to be:

If Nz(DLookup("[Password]", "tblEmployee", "[Login] = '" & Me.txtLogin & "'") = Me.txtPassword Then

put "GarbageEntry" in the form control, NOT in the dLookup()
 
Is supposed to be:

If Nz(DLookup("[Password]", "tblEmployee", "[Login] = '" & Me.txtLogin & "'") = Me.txtPassword Then

put "GarbageEntry" in the form control, NOT in the dLookup()
Is supposed to be:

If Nz(DLookup("[Password]", "tblEmployee", "[Login] = '" & Me.txtLogin & "'") = Me.txtPassword Then

put "GarbageEntry" in the form control, NOT in the dLookup()
Pat,
I thought the “GarbageEntry” is what the Nz function will return if the expression value is null.
 
Is supposed to be:

If Nz(DLookup("[Password]", "tblEmployee", "[Login] = '" & Me.txtLogin & "'") = Me.txtPassword Then

put "GarbageEntry" in the form control, NOT in the dLookup()
Ah, could it be that I don’t need the Nz function to return GarbageEntry because a null value would still not verify the password as long as the Dlookup returns a value? Is that what I am missing?
 
Because Dlookup can return a null it makes it poor for use in a conditional. I'd suggest using Dcount and including both the login and password input values in it's filter argument. 0 means invalid, all else is valid.
 
Thanks all but I went with using a recordset instead.
I use both the password and the loginID in the dCount() when I do this.

Code:
If dCount("*", "tblUsers", "LoginID = '" & Me.txtLogin & "' AND Password = '"  & Me.Password & "'" = 0 Then
    Msgbox "LoginID and/or Password are not valid", vbOKOnly
    Exit Sub
Else
    DoCmd.OpenForm "Switchboard"
    Me.frmLogin.Visible = False
End If
 
I thought the “GarbageEntry” is what the Nz function will return if the expression value is null.
You're right, it does. I was focused on the dLookup() and the extra argument didn't make any sense in that context.
 

Users who are viewing this thread

Back
Top Bottom