this is DK
this is DK

Reputation: 39

DLookup Function - returning wrong value

I am trying to run the following code, but I'm getting a Run-time error '2471' on DLookUp. "The expression you entered as a query parameter produced this error: 'dkim'".

DLookUp returns a value, but it's returning the wrong value. This code is supposed to compare Text7 to a dlookup 'Password' value. Am I missing something?

Private Sub btn_enter_Click()

    If Me.Text7.Value = DLookup("Password", "tbl_ref_users", _
        "[ID]=" & Me.Text5.Value) Then

        ID = Me.Text5.Value

        'Close logon form and open splash screen

        DoCmd.Close acForm, "form_Login", acSaveNo
        DoCmd.OpenForm "form_Menu"

    Else
        MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
            "Invalid Entry!"
        Me.Text7.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

Upvotes: 0

Views: 939

Answers (1)

HansUp
HansUp

Reputation: 97101

I'm getting a Run-time error '2471' on DLookUp. "The expression you entered as a query parameter produced this error: 'dkim'".

Since the DLookup expression is DLookup("Password", "tbl_ref_users", "[ID]=" & Me.Text5.Value), perhaps the .Value of Me.Text5 is dkim.

In that case, quote the text box value in the DLookup criteria:

DLookup("[Password]", "tbl_ref_users", "[ID]='" & Me.Text5.Value & "'")

However the datatype of tbl_ref_users.ID must be text in order for that to work. If it's numeric, you need to compare it to a number instead of a text string such as "dkim".

Also note I bracketed the field name Password because it's a reserved word.

Upvotes: 1

Related Questions