Managing Users and Groups

The Workspace object contains a Groups collection and a Users collection. The Groups collection contains all the Group objects used in the workgroup. A Group, as its name suggests, is a collection of Users to whom you want to assign the same privileges. You can enumerate the users and groups using the following code:

Public Sub EnumUsersAndGroups() Dim wrk As DAO.Workspace Dim grp As DAO.Group Dim usr As DAO.User

Set wrk = DBEngine(0)

'Enumerate the groups Debug.Print "Groups..." For Each grp In wrk.Groups

Debug.Print vbTab & grp.Name Next grp

'Enumerate the users Debug.Print "Users..." For Each usr In wrk.Users

Debug.

.Print vbTab & usr.Name

Next usr

Set grp =

Nothing

Set wrk =

Nothing

End Sub

The above code simply lists all the users and groups that exist in the system, but it doesn't show the relationship between them. If you want to find out which users belong to a specific group, you need to enumerate the Users collection for that specific group.

Public Sub EnumGroupUsers(strGroup As String) Dim wrk As DAO.Workspace Dim varUser As Variant

Set wrk = DBEngine(0)

Debug.Print "Users belonging to the '" & strGroup & "' group... " For Each varUser In wrk.Groups(strGroup).Users

Debug.Print vbTab & varUser.Name Next varUser

Set wrk = Nothing End Sub

Similarly, you can list all the groups that a specific user belongs to by enumerating the Groups collection for that user.

Public Sub EnumUserGroups(strUser As String) Dim wrk As DAO.Workspace Dim varGroup As Variant

Set wrk = DBEngine(0)

Debug.Print "Groups to which user '" & strUser & " For Each varGroup In wrk.Users(strUser).Groups

Debug.Print vbTab & varGroup.Name Next varGroup

1 belongs..."

Set wrk = Nothing End Sub

Was this article helpful?

0 0

Post a comment