2

我目前在 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 中设置的安全控制。这是专门针对功能性的。

4

1 回答 1

0

请在下面找到我从后端获取数据时使用的部分内容:

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

 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") & "'"
于 2018-04-03T13:53:07.883 回答