数据库错误424运行时错误

时间:2014-01-21 09:50:36

标签: vba access-vba

我正在尝试为我的用户创建登录页面时遇到问题,它给我一个运行时错误'424'我该如何使用它?我正在访问2010年创建一个表单,我是新手,所以所有提示都将不胜感激。

这是我的代码

Private Sub cmdLogin_Click()

    Dim dbs As Database
    Dim rstUserPwd As Recordset
    Dim bFoundmatch As Boolean

    Set dbs = CurrentDb
    Set rstUserPwd = dbs.OpenRecordset("QryUserPwd")

    bFoundmatch = False

    If rstUserPwd.RecordCount > 0 Then    
        rstUserPwd.MoveFirst
        'Checks for matching records

        Do While rstUserPwd.EOF = False
            If rstUserPwd![UserName] = Form_Fromlogin.txtusername.Value And rstUserPwd![Password] = Form_Fromlogin.txtpassword.Value Then
                bFoundmatch = True
                Exit Do
            End If
            rstUserPwd.MoveNext
        Loop
    End If

    If bFoundmatch = True Then
        'open the next form and closes this one.
        DoCmd.Close acForm, Me.Name
        DoCmd.OpenForm "frmNavigation"
    Else
        MsgBox "Incorrect username or password, please try again"
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

如果你能指出我们遇到运行时错误的那一行会更有帮助。

424是对象需要,所以从你的代码来看,它可能是这一行:

Set rstUserPwd = dbs.OpenRecordset("QryUserPwd")

或者这一行:

If rstUserPwd![UserName] = Form_Fromlogin.txtusername.Value 
And rstUserPwd![Password] = Form_Fromlogin.txtpassword.Value Then

如果是第一种情况,请确保使用Access中的确切名称保存查询。

如果是第二种情况,我会考虑更改对记录集字段的引用,如下所示:

    rstUserPwd("[UserName]") 
    rstUserPwd("[Password]") 
相关问题