The following code shows all the groups a given user belongs to:
Sub ShowUserGroups(strWorkgroup As String, strUser As String) Dim usr As User Dim grp As Group ' Specify a workgroup information file. DBEngine.SystemDB = strWorkgroup ' Return a reference to the specified user. Set usr = DBEngine.Workspaces(0).Users(strUser) ' Enumerate the Groups collection of the User object. For Each grp In usr.Groups Debug.Print grp.Name Next grp End Sub
The following code reverses this logic to show all the users in a given group:
Sub ShowGroupUsers(strWorkgroup As String, strGroup As String) Dim usr As User Dim grp As Group ' Specify a workgroup information file. DBEngine.SystemDB = strWorkgroup ' Return a reference to the specified group. Set grp = DBEngine.Workspaces(0).Groups(strGroup) ' Enumerate the Users collection of the Group object. For Each usr In grp.Users Debug.Print usr.Name Next usr End Sub
Or you can show all the users and all the groups they belong to with the following code:
Sub ShowUsersGroups(strWorkgroup As String) Dim wrk As Workspace Dim usr As User Dim grp As Group ' Specify a workgroup information file. DBEngine.SystemDB = strWorkgroup ' Return a reference to the default Workspace object. Set wrk = DBEngine.Workspaces(0) For Each usr In wrk.Users Debug.Print "User: " & usr.Name For Each grp In usr.Groups Debug.Print " Group: " & grp.Name Next grp Next usr End Sub
And, as with other collections, you use the Count property to determine the number of members. To find the number of user accounts in the current workgroup information file, you use:
Debug.Print DBEngine.Workspaces(0).Users.Count
Similarly, to find the number of groups in the current workgroup information file, use:
Debug.Print DBEngine.Workspaces(0).Groups.Count
To determine the number of groups a specific user belongs to, use the Count property of that user’s User object’s Groups collection:
Debug.Print DBEngine.Workspaces(0).Users!Hannah.Groups.Count