MS Access 2016 - 使用Tempvars检查Openform上的访问级别

时间:2018-01-14 04:07:03

标签: vba ms-access access-vba

我的目的是拒绝不符合某种访问级别访问权限的用户访问表单。在编写代码时,我最初遇到了错误代码3265的问题:

TempVars("EmployeeType").Value = rs!EmployeeType_ID.Value

这不再是问题;但是,即使适当的用户试图进入,我也无法访问该表单。我也多次检查过表格和列名的拼写。

下面是我的登录代码(我使用的是tempvars),后面是Load()形式的代码。

Option Compare Database
Option Explicit

Private Sub btnLogin_Click()
Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("Employees", dbOpenSnapshot, dbReadOnly)

rs.FindFirst "UserName='" & Me.txtUserName & "'"

If rs.NoMatch = True Then
    Me.lblWrongUser.Visible = True
    Me.txtUserName.SetFocus
    Exit Sub
End If
Me.lblWrongUser.Visible = False

If rs!Password <> Me.txtPassword Then
    Me.lblWrongPass.Visible = True
    Me.txtPassword.SetFocus
    Exit Sub
End If

If IsNull(Me.txtUserName) Or IsNull(Me.txtPassword) Then
    MsgBox "You must enter password or login ID.", vbOKOnly + vbInformation, "Required Data"
    Me.txtUserName.SetFocus
    Exit Sub
End If
Me.lblWrongPass.Visible = False

If rs!EmployeeType >= 4 Then
    Dim prop As Property
    On Error GoTo SetProperty
    Set prop = CurrentDb.CreateProperty("AllowBypassKey", dbBoolean, False)

    TempVars("UserName").Value = Me.txtUserName.Value
    TempVars("EmployeeType").Value = rs!EmployeeType_ID.Value

    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

Me.Visible = False
DoCmd.OpenForm "frmMain"

Globals.LoggingSignOn "Logon"

End Sub

Private Sub Form_Load()
Me.txtUserName = Null
Me.txtPassword = Null
Me.txtUserName.SetFocus
End Sub

Private Sub Form_Unload(Cancel As Integer)
Globals.LoggingSignOn "Logoff"
End Sub

Private Sub Form_Load()
    If Nz(DLookup("HasAccess", "tbl9EmployeeAccess", "EmployeeType_ID=" & TempVars("EmployeeType") & " FormName='" & Me.Name & "'"), False) = False Then
        MsgBox "You do not have access to access this location."
        DoCmd.Close acForm, Me.Name
    End If
End Sub

感谢您抽出时间,感谢任何关注此事的人。

0 个答案:

没有答案