DLookup函数 - 返回错误的值

时间:2015-10-02 14:31:18

标签: vba ms-access access-vba ms-access-2013

我正在尝试运行以下代码,但我在DLookUp上遇到运行时错误“2471”。 “您作为查询参数输入的表达式产生了此错误:'dkim'”。

DLookUp返回一个值,但它返回的值不正确。此代码应该将Text7与dlookup“密码”值进行比较。我错过了什么吗?

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

1 个答案:

答案 0 :(得分:1)

  

我收到运行时错误' 2471'在DLookUp上。 "表达你   作为查询参数输入产生此错误:' dkim'"。

由于DLookup表达式为DLookup("Password", "tbl_ref_users", "[ID]=" & Me.Text5.Value).Value的{​​{1}}可能是 dkim

在这种情况下,引用Me.Text5条件中的文本框值:

DLookup

DLookup("[Password]", "tbl_ref_users", "[ID]='" & Me.Text5.Value & "'") 的数据类型必须是文本才能使其生效。如果是数字,则需要将其与数字进行比较,而不是将文本字符串(例如" dkim" )进行比较。

另请注意,我将字段名称​​密码括起来,因为它是一个保留字。

相关问题