Office中国论坛/Access中国论坛

标题: [求助]如何判断用户所属的组? [打印本页]

作者: lzongb    时间: 2004-9-18 19:54
标题: [求助]如何判断用户所属的组?
数据库设置了用户级安全机制后,如何使用VBA代码才能读取登录用户所属的组并赋值给一个变量?

我是想实现根据登录用户所属的组来决定某些菜单项是否可用的功能,请指教!
作者: 方漠    时间: 2004-9-20 06:26
Here it is .........

Groups 和 Users 的 Append、ChangePassword 方法范例 (VB)



本范例通过向系统中添加新的 Group 和新的 User 来演示 Groups 的 Append 方法和 Users 的 Append 方法。新的 Group 被追加到新的 User 的 Groups 集合中。结果,新的 User 被添加到 Group 中。同样,使用 ChangePassword 方法来指定 User 密码。

Sub GroupX()

    Dim cat As ADOX.Catalog

    Dim usrNew As ADOX.User

    Dim usrLoop As ADOX.User

    Dim grpLoop As ADOX.Group

   

    Set cat = New ADOX.Catalog

    cat.ActiveConnection = "rovider=Microsoft.Jet.OLEDB.4.0;" & _

        "Data Source=c:\Program Files\" & _

        "Microsoft Office\Office\Samples\Northwind.mdb;" & _

        "jet oledb:system database=c:\samples\system.mdb"

    With cat

        'Create and append new group with a string.

        .Groups.Append "Accounting"

      

        ' Create and append new user with an object.

        Set usrNew = New ADOX.User

        usrNew.Name = "at Smith"

        usrNew.ChangePassword "", "assword1"

        .Users.Append usrNew

        ' Make the user Pat Smith a member of the

        ' Accounting group by creating and adding the

        ' appropriate Group object to the user's Groups

        ' collection. The same is accomplished if a User

        ' object representing Pat Smith is created and

        ' appended to the Accounting group Users collection

        usrNew.Groups.Append "Accounting"

      

        ' Enumerate all User objects in the

        ' catalog's Users collection.

        For Each usrLoop In .Users

            Debug.Print "  " & usrLoop.Name

            Debug.Print "    Belongs to these groups:"

            ' Enumerate all Group objects in each User

            ' object's Groups collection.

            If usrLoop.Groups.Count <> 0 Then

                For Each grpLoop In usrLoop.Groups

                    Debug.Print "    " & grpLoop.Name

                Next grpLoop

            Else

                Debug.Print "    [None]"

            End If

        Next usrLoop

        ' Enumerate all Group objects in the default

        ' workspace's Groups collection.

        For Each grpLoop In .Groups

            Debug.Print "  " & grpLoop.Name

            Debug.Print "    Has as its members:"

            ' Enumerate all User objects in each Group

            ' object's Users collection.

            If grpLoop.Users.Count <> 0 Then

                For Each usrLoop In grpLoop.Users

                    Debug.Print "    " & usrLoop.Name

                Next usrLoop

            Else

                Debug.Print "    [None]"

            End If

        Next grpLoop

        

        ' Delete new User and Group objects because this

        ' is only a demonstration.

        .Users.Delete "Pat Smith"

        .Groups.Delete "Accounting"

    End With

End Sub



            If grpLoop.Users.Count <> 0 Then

                For Each usrLoop In grpLoop.Users

                    Debug.Print "    " & usrLoop.Name

                Next usrLoop

            Else

                Debug.Print "    [None]"

            End If

        Next grpLoop

        

        ' Delete new User and Group objects because this

        ' is only a demonstration.

        .Users.Delete "Pat Smith"

        .Groups.Delete "Accounting"

    End With

End Sub

[此贴子已经被作者于2004-9-19 22:33:23编辑过]






欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3