>

Group Object

Description

A Group object represents a group of user accounts that have common access permissions when a Workspace object operates as a secure workgroup.

Remarks

You create Group objects and then use their names to establish and enforce access permissions for your databases, tables, and queries using the Document objects that represent the database and database objects (tables and queries) you're working with.

You can append an existing Group object to the Groups collection in a User object to establish membership of a user account in that Group object. Alternatively, you can append a User object to the Users collection in a Group object to give a user account the global permissions of that group. If you use a Groups or Users collection other than the one to which you just appended an object, you may need to use the Refresh method to refresh the collection with current information from the database.

Using the properties of a Group object, you can:

The Microsoft Jet database engine predefines three Group objects named Admins, Users, and Guests.

You can refer to a Group object that you create and append to a Groups collection by its Name property setting using this syntax:

Groups("name")

To create a new Group object, use the CreateGroup method on a User or Workspace object.

Properties

Name Property, PID Property.

Methods

CreateUser Method.

See Also

CreateGroup Method; Appendix, "Data Access Object Hierarchy."

Specifics (Microsoft Access)

You can create Group objects to establish and enforce permissions for Microsoft Access database objects as well as for data access objects. For example, you can set security for forms, reports, macros, and modules.

A Group object has a Name property that you can use in setting permissions for a Container or Document object. For example, you can assign the value of a Group object's Name property to the UserName property of a Container or Document object. You can then set the Permissions property of the Container or Document object to establish permissions for the group of users defined by the UserName property. Or you can read the Permissions property to determine existing permissions for that group.

Example

This example illustrates the use of the Group and User objects and the Groups and Users collections. First, it creates a new User object with the Name, PID, and Password property settings of "Pat Smith," "abc123DEF456," and "My Secret," and appends the object to the Users collection of the default Workspace object. Next, it creates a new Group object with the Name and PID property settings of "Accounting" and "UVW987xyz654" and appends the object to the Groups collection of the default Workspace object. Then the example adds user Pat Smith to the Accounting group. Finally, it enumerates the User and Group objects in the collections of the default Workspace object.


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