Access权限 判断函数(2)
根据网上和书本的教程,结合自己的实际自定义了适合自己系统的权限判断函数,具体如下:
- Function OpenForm(FormID As Integer)
- On Error GoTo Err_OpenForm
- Dim i As Integer
- Dim STemp As String
- Dim Rs1 As ADODB.Recordset
- Dim Rs2 As ADODB.Recordset
- Set Rs1 = New ADODB.Recordset
- Set Rs2 = New ADODB.Recordset
- Dim blnOpen As Boolean
- Dim FormName As String
- Dim UserGroup As String
- Dim UserID As String
- UserGroup = DLookup("用户组", "系统用户表", "用户名 ='" & User & "'")
- UserID = DLookup("用户组ID", "用户组表", "用户组 ='" & UserGroup & "'")
- '打开“用户组权限表”数据表
- STemp = "Select * From 用户组权限表"
- Rs1.Open STemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
- '打开“系统窗体表”数据表
- STemp = "Select * From 系统窗体表"
- Rs2.Open STemp, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
- blnOpen = False
- '判断“用户组权限”数据是否为空
- If Rs1.RecordCount < 1 Then
- blnOpen = False
- Else
- Rs1.MoveFirst
- '判断当前“登录用户”是否有权限打开FormID对应的窗体
- For i = 1 To Rs1.RecordCount
- If Rs1("用户组ID") = UserID And Rs1("窗体ID") = FormID And Rs1("权限") = True Then
- blnOpen = True
- Else
- Rs1.MoveNext
- End If
- Next i
- End If
- '在“系统窗体表”数据表中搜索待打开窗体的“名称”
- Rs2.MoveFirst
- For i = 1 To Rs2.RecordCount
- If Rs2("窗体ID") = FormID Then
- FormName = Rs2("窗体名称")
- Else
- Rs2.MoveNext
- End If
- Next i
- '判断用户是否有权限打开窗体,blnOpen为“真”有权限
- If blnOpen = False Then
- MsgBox "您没有权限使用" & "“" & FormName & "”窗体", vbCritical, "无权使用"
- Exit Function
- Else
- DoCmd.OpenForm FormName, acNormal, , , , acWindowNormal
- End If
- Set Rs1 = Nothing
- Set Rs2 = Nothing
- Exit Function
- Err_OpenForm:
- Set Rs1 = Nothing
- Set Rs2 = Nothing
- MsgBox Err.Description, vbOKOnly, "窗体打开错误"
- End Function
最后需要调用该函数的地方直接调用好了。如果没有权限就会有以下提示。
共有 0 条评论