在 Access VBA 中将文本输入存储为变量而不是数字

Store text input as variable instead of number in Access VBA

我目前正在使用以下代码为我的用户在 Microsoft Access 中创建一个登录表单。它最初设计用于接受列 UserID 中的用户名 ID,该列是 table Employees Table 中的自动编号列。

代码运行正常,但我不想再使用用户 ID,而是使用我创建的名为 Username 的列链接到组合框 cbo_Employee 以验证正确用户名和密码。

我相信该值当前存储在 lngMyUsername 中,但它只会存储数字,如果我尝试将其设置为 string,我会在行 If Me.txt_Password.Value <> DLookup("Password", "Employees Table", "[Username]=" & lngMyUsername) Then

附件是一些图片,概述了我的 Employees Tablecbo_Employees 数据属性的结构。

如有任何帮助,我们将不胜感激。如果您需要更多信息,请随时询问。

谢谢, 贾尔斯

Private Sub cmdLogin_Click()
        'Check to see if data is entered into the Username combo box
    Dim lngMyUsername As Long
    If IsNull(Me.cbo_Employee) Or Me.cbo_Employee = "" Then
        MsgBox "You Must enter a Username.", vbOKOnly, "Required Data"
        Me.cbo_Employee.SetFocus
        Exit Sub
    End If
    lngMyUsername = Me.cbo_Employee.Value

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

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

'Check value of password in Employees Table to see if this matches value chosen in combo box
If Me.txt_Password.Value <> DLookup("Password", "Employees Table", "[Username]=" & lngMyUsername) Then
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
    Me.txt_Password.SetFocus
    Me.txt_Password = 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.txt_Password = Null
    'Open correct form
    Dim strAccessLevel As String

    strAccessLevel = DLookup("Admins", "Employees Table", "Username=" & lngMyUsername)

    If strAccessLevel = "Admin" Then
        MsgBox "Welcome " & DLookup("EmployeeName", "Employees Table", "Username=" & lngMyUsername)
        DoCmd.Close
        DoCmd.OpenForm "A"
    ElseIf strAccessLevel = "Manager" Then
        MsgBox "Welcome " & DLookup("EmployeeName", "Employees Table", "Username=" & lngMyUsername)
        DoCmd.Close
        DoCmd.OpenForm "B"
    ElseIf strAccessLevel = "User" Then
        MsgBox "Welcome " & DLookup("EmployeeName", "Employees Table", "Username=" & lngMyUsername)
        DoCmd.Close
        DoCmd.OpenForm "C"
    End If
End If

End Sub

编辑:下面是修改后的代码,正常工作后会再次更新。

Private Sub cmdLogin_Click()
        'Check to see if data is entered into the Username combo box
    Dim strMyUsername As String
    Dim RS As Recordset
    If IsNull(Me.cbo_Employee) Or Me.cbo_Employee = "" Then
        MsgBox "You Must enter a Username.", vbOKOnly, "Required Data"
        Me.cbo_Employee.SetFocus
        Exit Sub
    End If
    strMyUsername = Me.cbo_Employee.Value

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

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

Set RS = CurrentDb.OpenRecordset("SELECT * FROM [Employees Table] WHERE [Username] = '" & strMyUsername & "'", dbOpenSnapshot)

'Check value of password in Employees Table to see if this matches value chosen in combo box
If Me.txt_Password.Value <> Nz(RS!Password) Then
    MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
    Me.txt_Password.SetFocus
    Me.txt_Password = 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.txt_Password = Null
    'Open correct form
    Dim strAccessLevel As String

    strAccessLevel = RS!Admins

    If strAccessLevel = "Admin" Then
        MsgBox "Welcome " & RS!EmployeeName
        DoCmd.Close
        DoCmd.OpenForm "A"
    ElseIf strAccessLevel = "Manager" Then
        MsgBox "Welcome " & RS!EmployeeName
        DoCmd.Close
        DoCmd.OpenForm "B"
    ElseIf strAccessLevel = "User" Then
        MsgBox "Welcome " & RS!EmployeeName
        DoCmd.Close
        DoCmd.OpenForm "C"
    End If
End If

End Sub

编辑 所以您已经更改了代码,使用用户名而不是用户 ID。一切都清楚了。

在这种情况下,使用字符串变量 strMyUsername,对于密码检查,您需要将参数括在 '...':

' Just to be safe: unmasked ' would break the query
strMyUsername = Replace(strMyUsername, "'", "''")

If Me.txt_Password.Value <> DLookup("Password", "Employees Table", _
    "[Username] = '" & strMyUsername & "'") Then
        ' wrong password...

编辑 2

这条线应该有效

strAccessLevel = DLookup("Admins", "Employees Table", "[Username] = '" & strMyUsername & "'")

当然这还不会:

MsgBox "Welcome " & DLookup("EmployeeName", "Employees Table", "Username=" & strMyUsername)

但是由于您要从 "Employees Table" 读取多个字段,我建议采用不同的方法:一次将所有字段读取到记录集中。
然后你可以用 RS!Field

替换所有 DLookup 调用

OpenRecordset检查用户名和密码是否设置后属于

Dim RS As Recordset
Set RS = CurrentDb.OpenRecordset("SELECT * FROM [Employees Table] WHERE [Username] = '" & strMyUsername & "'", dbOpenSnapshot)

If Me.txt_Password.Value <> Nz(RS!Password) Then
' ...
strAccessLevel = RS!Admins
' ...
MsgBox "Welcome " & RS!EmployeeName

编辑 3

参见 Debugging VBA

Set RS = CurrentDb.OpenRecordset 行设置断点。
将鼠标悬停在 strMyUsername - 它是否包含您期望的值?
执行该行。
立即window(Ctrl+G):? RS.EOF如果EOF为真,则没有记录。

? RS.Name 给你 SQL。复制它,从中创建一个新查询,然后 运行 它。这将有助于弄清楚为什么它没有获得记录。