我目前正在使用以下代码为我的用户提供Microsoft Access中的登录表单。它最初设计为接受UserID
列中的用户名ID,该列是表Employees Table
内的自动编号列。
代码工作正常,但我不想再使用用户ID,而是使用我创建的名为Username
的列链接到组合框cbo_Employee
以验证使用密码的正确用户名。
我认为该值目前存储在lngMyUsername
中,但它只会存储数字,如果我尝试将其设置为string
,我会在行{{1}上找到数据类型不匹配}
附上一些图片,概述了我的If Me.txt_Password.Value <> DLookup("Password", "Employees Table", "[Username]=" & lngMyUsername) Then
和Employees Table
数据属性的结构。
任何帮助将不胜感激。如果您需要更多信息,请不要犹豫。
谢谢, 贾尔斯。
cbo_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
答案 0 :(得分:1)
修改强> 所以你已经改变了代码,使用Username而不是UserID。这清除了事情。
在这种情况下,请使用字符串变量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)
但是,由于您正在阅读“员工表”中的多个字段,因此我建议采用不同的方法:将所有字段一次性读入记录集。
然后,您可以按DLookup
RS!Field
次来电
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
在Set RS = CurrentDb.OpenRecordset
行设置断点
将鼠标悬停在strMyUsername
上 - 它是否包含您期望的值?
执行该行。
在立即窗口(Ctrl + G)中:? RS.EOF
如果EOF为true,则没有记录。
? RS.Name
为您提供SQL。复制它,从中创建一个新查询,然后运行它。这将有助于弄清楚为什么它没有得到记录。