ms访问查询以创建新的登录ID,每次我希望通过用户详细信息表单添加新用户

时间:2014-01-12 08:40:56

标签: vba ms-access login vb6 stack-overflow

我有一个Login表,其中包含用户信息,例如登录ID,用户名,密码和访问级别。所以我创建了一个名为用户详细信息表单的表单,我可以将新用户添加到数据库中。

每次打开用户详细信息表单时,都会通过运行查询生成新的ID。

这是我在查询字段中输入的内容,用于生成新的ID

new_login_ID: "LI" & Right([Login_ID],2)+1

问题是,在我的登录表单中,我尝试使用以下代码登录:

Private Sub LoginBtn_Click()

'Check to see if data is entered into the UserName combo box

Dim lngMyEmpID As Long

If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then
    MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
    Me.cboEmployee.SetFocus
    Exit Sub
End If

lngMyEmpID = Me.cboEmployee.Value

'Check to see if data is entered into the password box

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then
    MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
    Me.txtPassword.SetFocus
    Exit Sub
End If

'Check value of password in tblAdmins to see if this matches value chosen in combo box

If Me.txtPassword.Value <> DLookup("Password", "tbl_login", "[Login_ID]=" & lngMyEmpID) Then

            MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
            Me.txtPassword.SetFocus
            Me.txtPassword = Null
            intLogonAttempts = intLogonAttempts + 1
            'If User Enters incorrect password 3 times database will shutdown
            If intLogonAttempts >= 3 Then
                MsgBox "You do not have access to this database. Please contact your system administrator.", vbCritical, "Restricted Access!"
                Application.Quit
            End If

Else
    Me.txtPassword = Null
    'Open correct form
    Dim strAccessLevel As String

    strAccessLevel = DLookup("[Access]", "tbl_login", "[Login_ID]=" & lngMyEmpID)

    If strAccessLevel = "Admin" Then

        MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID=" & lngMyEmpID)
        DoCmd.Close
        DoCmd.OpenForm "frm_Admin"

    ElseIf strAccessLevel = "Manager" Then
        'MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID")
        MsgBox "Welcome " & DLookup("Username", "tbl_login", "Login_ID=" & lngMyEmpID)
        DoCmd.Close
        DoCmd.OpenForm "frm_main_menu"
    End If
End If


End Sub

我收到运行时错误,说“不匹配”,当我调试时,它指向我的登录代码的这一行 - lngMyEmpID = Me.cboEmployee.Value

我不知道如何解决这个问题,有人可以解释我哪里出错了以及如何解决这个问题..提前感谢:)

1 个答案:

答案 0 :(得分:0)

您在评论中写的这一行应该更正为此(因为lngMyEmpID现在是一个字符串):

If Me.txtPassword.Value <> DLookup("Password", "tbl_login", "[Login_ID]='" & lngMyEmpID & "'")

对于您以这种方式使用DLookup和ID的每一行也是如此。