我似乎无法找到一种将其编码到我的VBA宏中的好方法 . 我正在使用Access . 我有一个表单作为'登录'它Dlookups以确保用户在用户表中 . 一旦确认用户在用户表中,我希望它使用用户名的标准在另一个表上运行查询 .

这是我的查询引用表上的用户名字段的标准:

[Forms]![LoginForm].[txtUserName]

这是我的登录表单点击按钮的宏

Private Sub btnLogin_Click()

    Dim User As String
    Dim UserLevel As Integer
    Dim TempPass As String
    Dim ID As Integer
    Dim UserName As String
    Dim TempID As String


    If IsNull(Me.txtUserName) Then
        MsgBox "Please enter UserName", vbInformation, "Username required"
        Me.txtUserName.SetFocus
    ElseIf IsNull(Me.txtPassword) Then
        MsgBox "Please enter Password", vbInformation, "Password required"
        Me.txtPassword.SetFocus
    Else
        If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then
            MsgBox "Invalid Username or Password!"
        Else
            TempID = Me.txtUserName.Value
            UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
            UserLevel = DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
            TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
            UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
            DoCmd.Close
            If (TempPass = "password") Then
                MsgBox "Please change Password", vbInformation, "New password required"
                DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
            Else
                'open different form according to user level
                 DoCmd.OpenQuery "ClockDefectRun", , acReadOnly
            End If
        End If
    End If

End Sub

每当我运行它时,我的查询就会打开,但它会要求该字段,就好像它在我的表单中不存在一样 . 我怎么能以编程方式通过?