>
Function EnumerateUserGroup () As Integer
Dim wrkDefault As Workspace
Dim usrPatSmith As User, usrTemp As User, usrGroupUser As User
Dim grpAccount As Group, grpTemp As Group, grpMember As Group
Dim intI As Integer, intJ As Integer
Set wrkDefault = DBEngine.Workspaces(0)
' Create and append new user.
Set usrPatSmith = wrkDefault.CreateUser("Pat Smith", _
"abc123DEF456", "My Secret")
wrkDefault.Users.Append usrPatSmith
' Create and append new group.
Set grpAccount = wrkDefault.CreateGroup("Accounting", _
"UVW987xyz654")
wrkDefault.Groups.Append grpAccount
' Add new user to new group or converse; do one or the other, not both.
' Use Refresh because both collections are referred to in following
' code.
' wrkDefault.Groups![Accounting].Users.Append usrGroupUser
' wrkDefault.Users![Pat Smith].Groups.Refresh
' Or add group to user and refresh group.
' Create new group object for user Pat Smith
Set grpMember = usrPatSmith.CreateGroup("Accounting")
' wrkDefault.Users![Pat Smith].Groups.Append grpMember
' wrkDefault.Groups![Accounting].Users.Refresh
' Enumerate all users.
For intJ = 0 To wrkDefault.Users.Count - 1
Set usrTemp = wrkDefault.Users(intJ)
Debug.Print
Debug.Print "Enumeration of Users: "; usrTemp.Name
Debug.Print
' Enumerate groups.
Debug.Print " Groups: "
For intI = 0 To usrTemp.Groups.Count - 1
Debug.Print " "; usrTemp.Groups(intI).Name
Next intI
Next intJ
' Enumerate all groups.
Debug.Print "------------------------------------"
For intJ = 0 To wrkDefault.Groups.Count - 1
Set grpTemp = wrkDefault.Groups(intJ)
Debug.Print
Debug.Print "Enumeration of Groups: "; grpTemp.Name
Debug.Print
' Enumerate users.
Debug.Print " Users: "
For intI = 0 To grpTemp.Users.Count - 1
Debug.Print " "; grpTemp.Users(intI).Name
Next intI
Next intJ
EnumerateUserGroup = True
End Function
Example (Microsoft
Access)
The following example creates a
new User object and appends it to the Users
collection of a Workspace object. It then creates a new Group
object and appends it to the Groups collection of the Workspace
object. The new Group object is also appended to the Groups
collection of the User object. The new group is then given
modify and delete permissions for modules.
Note that in order to assign
users to groups, you must either append a User object to
the Users collection of a Group object, or append a
Group object to the Groups collection of a User
object. It doesn't matter which option you choose; either will
result in the specified user being included in the specified
group.
Sub NewModulesGroup()
Dim wsp As Workspace, dbs As Database
Dim usr As User, grp As Group, grpMember As Group
Dim ctr As Container, doc As Document
' Get default Workspace object.
Set wsp = DBEngine.Workspaces(0)
' Return Database variable pointing to current database.
Set dbs = CurrentDb
' Create User object and append to Users collection
' of Workspace object.
Set usr = wsp.CreateUser("Pat Smith", "123abc789xyz", "Password")
wsp.Users.Append usr
' Create Group object and append to Groups collection
' of Workspace object.
Set grp = wsp.CreateGroup("Programmers", "321xyz987abc")
wsp.Groups.Append grp
' Append Group object to Groups collection of User object.
Set grpMember = usr.CreateGroup("Programmers")
usr.Groups.Append grpMember
' Refresh Groups collection of User object.
usr.Groups.Refresh
' Return Container object.
Set ctr = dbs.Containers!Modules
' Set UserName property of Container object.
ctr.UserName = grpMember.Name
'Add modify and delete permissions for new group on all modules.
ctr.Permissions = ctr.Permissions Or acSecModWriteDef
End Sub