我目前在 MS Access 2016 中工作。我正在处理的项目的要求之一是将用户 Windows 登录名绑定到 MS Access。我只是抓住用户的“用户名”。在 Access 中,将为表中的用户设置权限。一旦用户根据那里的权限登录,他们将被引导到他们特定的打开页面。我能够成功检索用户 Windows 登录名,但无法连接到我的后端表。
我的表名是 tblUser 字段名是:
FName LName postion UserName(PK) EmployeeType_ID
我的代码在下面我在“rs.FindFirst“UserName =”处收到运行时错误“3077”“表达式中的字符串语法错误”。我不确定问题是什么,任何帮助都会很大赞赏。
Private Sub Form_Load()
Debug.Print Environ("UserName")
Debug.Print Environ$("ComputerName")
Dim strVar As String
Dim i As Long
For i = 1 To 255
strVar = Environ$(i)
If LenB(strVar) = 0& Then Exit For
Debug.Print strVar
Next
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("tblUser", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='"
If rs.NoMatch = True Then
MsgBox "You do not have access to this database.", vbInformation, "Access"
Exit Sub
End If
If rs!EmployeeType_ID = 4 Then
Dim prop As Property
On Error GoTo SetProperty
Set prop = CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, False)
CurrentDb.Properties.Append prop
SetProperty:
If MsgBox("Would you like to turn on the bypass key?", vbYesNo, "Allow Bypass") = vbYes Then
CurrentDb.Properties("AllowBypassKey") = True
Else
CurrentDb.Properties("AllowBypassKey") = False
End If
End If
DoCmd.OpenForm "frmManager"
DoCmd.Close acForm
If rs!EmployeeType_ID = 3 Then
DoCmd.OpenForm "frmGeneral_User"
DoCmd.Close acForm
End If
If rs!EmployeeType_ID = 2 Then
DoCmd.OpenForm "frmAdmin"
DoCmd.Close acForm
End If
If rs!EmployeeType_ID = 1 Then
DoCmd.OpenForm "frmGuest"
DoCmd.Close acForm
End If
End Sub
ps 我完全理解有人可以绕过 Access 中设置的安全控制。这是专门针对功能性的。