VBA代码传递ms-access表格的参数进行查询

时间:2018-06-19 13:59:21

标签: vba ms-access

我似乎找不到一种将其编码到我的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

每当我运行此命令时,我的查询就会打开,但它会询问该字段,就好像它在表单中不存在一样。我该如何通过编程方式通过?

0 个答案:

没有答案