Database 登录时在MS Access中设置用户权限

Database 登录时在MS Access中设置用户权限,database,ms-access,vba,ms-access-2016,Database,Ms Access,Vba,Ms Access 2016,我目前在MS Access 2016内工作。我正在处理的项目的一个要求是将用户的Windows登录绑定到MS Access。我只抓取用户的“用户名”。在Access中,将为表中的用户设置权限。一旦用户基于这些权限登录,他们将被定向到其特定的打开页面。我能够成功检索用户的windows登录名,但连接到后端表时遇到问题 我的表名为tblUser,字段名为: FName LName postion UserName(PK) EmployeeType_ID 下面的代码是我在“rs.FindFirst”

我目前在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

p、 我完全理解有人可以绕过Access中设置的安全控制。这是专门用于功能的。

请查看我从后端获取数据时使用的部分内容:

您可以修改以下内容以获取特定的个人访问权限,然后可以根据从后端获取的数据设置要显示的正确用户表单

 Dim acc As Access.Application
 Dim db As DAO.Database
 Dim rs As DAO.Recordset
 Dim strSQL As String
 Dim strPassword As String
 Dim DBpath As String
 Dim DBname As String
 Dim tblStructure As String

 DBpath = "C:\Projects 
 DBname = "Self Serve Database.accdb"
 tblStructure = "_tbl_Structure"
 strPassword = "OpenSesame"

 strSQL = "INSERT INTO _tbl_Structure " & _
          "SELECT * " & _
          "FROM [MS Access;pwd=" & strPassword & ";database=" & DBpath & "\" & DBname & "].[" & tblStructure & "] " & _
          "WHERE [USER ID] = '" & Environ("username") & "'"

看起来这个问题与AD无关,您应该检查。FindFirst语法-有未闭合的字符串。请参阅此处答案中的正确示例-谢谢@sarh我已更改了我问题的标题。我目前正在查看另一篇文章。@sarh感谢文章中的示例修复了我的问题。