我目前正在构建一个数据库,其中显示的数据库表和表单基于用户权限。如果用户没有数据库的权限,则会出现一个消息框,指出“您无权访问此数据库”。此代码在登录屏幕中执行,用户只需按下登录按钮即可。我遇到的问题是,当用户确实拥有权限时,仍会显示消息框。一旦用户与消息框交互,他们就会被重定向到相应的表单。我想只有当用户没有权限时才会出现此消息框,一旦他们与消息框交互,数据库就会关闭。任何帮助将不胜感激。
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 = '" & EmployeeType_ID & "'"
If rs.NoMatch = True Then
MsgBox "You do not have access to this database.", vbInformation, "Access"
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
If rs!EmployeeType_ID = 4 Then
DoCmd.OpenForm "frmManager"
DoCmd.Close acForm, "frmLogin", acSaveNo
End If
If rs!EmployeeType_ID = 3 Then
DoCmd.OpenForm "frmAssistant_Manager"
DoCmd.Close acForm, "frmLogin", acSaveNo
End If
If rs!EmployeeType_ID = 2 Then
DoCmd.OpenForm "frmLead"
DoCmd.Close acForm, "frmLogin", acSaveNo
End If
If rs!EmployeeType_ID = 1 Then
DoCmd.OpenForm "frmGeneral_User"
DoCmd.Close acForm, "frmLogin", acSaveNo
End If
End Sub
答案 0 :(得分:0)
我已在数据库中修复了我的问题。我换了:
rs.FindFirst "UserName = '" & EmployeeType_ID & "'" with rs.FindFirst "UserName = '" & Environ("UserName") & "'"
并在我放置Access.Quit的代码中的消息框下面,所以当用户与消息框交互时,它将关闭。