简体   繁体   English

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

[英]Setting User Permissions Within MS Access on Login

I am currently working within MS Access 2016. One of the requirements for the project that I am working on is tying the users Windows login to MS Access. 我当前正在使用MS Access2016。我正在处理的项目的要求之一是将Windows登录用户绑定到MS Access。 I am only grabbing the users "User Name". 我只是抓住用户的“用户名”。 Within Access there will be permissions set to users within a table. 在Access中,将为表中的用户设置权限。 Once the users login based upon there permissions they will be directed to their specific opening page. 用户根据那里的权限登录后,他们将被定向到其特定的打开页面。 I was able to successfully retrieve the users windows login but I am having trouble connecting to my back end table. 我能够成功检索用户的Windows登录名,但无法连接到后端表。

My Table name is tblUser the field names are: 我的表名称是tblUser字段名称是:

FName LName postion UserName(PK) EmployeeType_ID

The code that I have is below I am getting a Run-time error '3077' "Syntax error in string in expression" at "rs.FindFirst "UserName='". I am not sure what the problem is any help would be greatly appreciated. 我下面的代码是在“ 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 I fully understand that someone can bypass the security controls set within Access. ps我完全理解有人可以绕过Access中设置的安全控制。 This is specifically for functionality. 这是专门针对功能性的。

Please find below part of what I use when grabbing data from a backend: 请从下面获取从后端获取数据时使用的部分内容:

You can modify the below to pull that specific individuals access rights, you can then set the correct userform to be displayed based on the data pulled from the back end. 您可以修改以下内容以提取特定的个人访问权限,然后可以基于从后端提取的数据来设置要显示的正确用户窗体。

 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") & "'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM