我有一个获取当前用户ID的全局函数。它的工作文件。现在,我想将其与想要访问该程序的表中的用户列表进行比较。 我的桌子是Tbl_Staff 保持每个人的值的字段是UserID 我的函数叫做GetUserName 这是我到目前为止的内容:
Private Sub Form_Load()
Dim Message As String
Dim Currentdb As DAO.Database
Dim RecSet As DAO.Recordset
Set RecSet = Currentdb.OpenRecordset("SELECT * FROM Tbl_Staff WHERE [Tbl_Staff].[UserID] = UserName())
If UserName = RecSet Then
Exit Sub
Else
Message = MsgBox("You do not have permission to access this database." & vbCrLf & "Please contact Robert Smith for access", vbOKOnly)
DoCmd.CloseDatabase
End If
End Sub
我真的很困。谢谢
答案 0 :(得分:1)
类似这样的东西:
Private Sub Form_Load()
Dim RecSet As DAO.Recordset
Set RecSet = Application.Currentdb.OpenRecordset( _
"SELECT * FROM Tbl_Staff WHERE [Tbl_Staff].[UserID] = '" & UserName() & "'")
If RecSet.EOF Then
MsgBox "You do not have permission to access this database." & _
vbCrLf & "Please contact Robert Smith for access"
DoCmd.CloseDatabase
End If
End Sub
可能需要调整SQL以解决数据库中用户名与UserName()返回值之间的差异
答案 1 :(得分:0)
另一种方法,使用访问内置功能
Private Sub Form_Load()
Dim blnUserExist As Boolean
blnUserExist = Nz(DCount("*", "Tbl_Staff", "UserID='" & Environ("username") & "'"),0)
If blnUserExist = False Then
MsgBox "You do not have permission to access this database." & vbCrLf & "Please contact Robert Smith for access", vbOKOnly
DoCmd.Quit
End If
End Sub