Group Object, Groups Collection Example (MDB)

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.

Note   When programming security, you should avoid including password and PID information in your code. The following example is intended for demonstration purposes only.

Sub NewMacrosGroup()
    Dim wsp As Workspace, dbs As Database
    Dim usr As User, grp As Group, grpMember As Group
    Dim ctr As Container

    ' Return referenct to default workspace.
    Set wsp = DBEngine.Workspaces(0)
    ' Return reference 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!Scripts
    ' 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 acSecMacWriteDef
    Set dbs = Nothing
    Set wsp = Nothing
End Sub