Solved Help with Login Code (1 Viewer)

slharman1

Member
Local time
Today, 08:26
Joined
Mar 8, 2021
Messages
467
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:26
Joined
Sep 21, 2011
Messages
14,052
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:
 

slharman1

Member
Local time
Today, 08:26
Joined
Mar 8, 2021
Messages
467
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?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:26
Joined
Feb 19, 2002
Messages
42,981
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()
 

slharman1

Member
Local time
Today, 08:26
Joined
Mar 8, 2021
Messages
467
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.
 

slharman1

Member
Local time
Today, 08:26
Joined
Mar 8, 2021
Messages
467
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?
 

plog

Banishment Pending
Local time
Today, 08:26
Joined
May 11, 2011
Messages
11,613
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:26
Joined
Feb 19, 2002
Messages
42,981
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
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:26
Joined
Feb 19, 2002
Messages
42,981
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

Top Bottom