Microsoft Office 2000/Visual Basic Programmer's Guide   

Managing Security Accounts

The Users collection of a Catalog object contains all users defined for the workgroup information file specified for the Connection object that is used to open the Catalog object. Similarly, the Groups collection contains all groups in that workgroup information file. By manipulating these collections, you define and control the security accounts that Microsoft Jet uses. The Groups and Users collections are interesting in that they are self-referencing — the Users collection contains a Groups collection, and the Groups collection contains a Users collection. By using this structure, you can easily determine which users belong to which groups, and which groups contain which users.

There are a few things to keep in mind when you're working with users and groups:

The following examples illustrate how to create and manage user accounts. These examples are available in the modSecurity module in AccessSecurity.mdb in the ODETools\V9\Samples\ODETools\V9\Samples\OPG\Samples\CH18 subfolder on the Office 2000 Developer CD-ROM.

The following procedure is designed to be used within Access after logging on and opening a database. It creates a new user account, and then appends it to the default Users group in the workgroup information file that is used for the current database.

Sub CreateUserInAccess(strUser As String, _
                       strPID As String, _
                       Optional strPwd As String)
   Dim catDB As ADOX.Catalog

   Set catDB = New ADOX.Catalog
   With catDB
      ' Open Catalog object by using connection to the current database.
      .ActiveConnection = CurrentProject.Connection
      ' Create new user account.
      .Users.Append strUser, strPwd, strPID
      ' Append new user account to default Users group.
      .Groups("Users").Users.Append strUser
   End With

   ' Close Catalog object.
   Set catDB = Nothing
End Sub

The next procedure creates a new user by opening another database while using the specified workgroup information file (system database), and logging in by using the specified user account and password. This procedure can be used from Access or any other Office application that supports VBA.

Sub CreateUserInSystemDB(strDB As String, _
                         strSystemDb As String, _
                         strUserID As String, _
                         strUserIDPwd As String, _
                         strNewUser As String, _
                         strPID As String, _
                         Optional strNewUserPwd As String)
   Dim catDB As ADOX.Catalog
   Dim cnnDB As ADODB.Connection

   ' Open connection to database by using specified system database, user ID,
   ' and password.
   Set cnnDB = New ADODB.Connection
   With cnnDB
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .Properties("Jet OLEDB:System Database") = strSystemDb
      .Properties("User ID") = strUserID
      .Properties("Password") = strUserIDPwd
      .Open strDB
   End With

   Set catDB = New ADOX.Catalog
   With catDB
      ' Open Catalog object by using cnnDB.
      .ActiveConnection = cnnDB
      ' Create new user account.
      .Users.Append strNewUser, strNewUserPwd, strPID
      ' Append new user account to default Users group.
      .Groups("Users").Users.Append "MyUser"
   End With

   ' Close Catalog object.
   Set catDB = Nothing
   ' Close connection.
   cnnDB.Close
   Set cnnDB = Nothing
End Sub

The process of creating a new group is nearly identical to the one used to create a new user. The following procedure creates a new group from within Access after you log on and open a database. To create a new group for a database other than the current database, or from another Office application, open a connection to that database as shown in the previous example and pass this connection to the ActiveConnection property of the Catalog object.

Sub CreateGroupInAccess(strGroup As String, _
                        strPID As String)
   Dim catDB As ADOX.Catalog

   Set catDB = New ADOX.Catalog
   With catDB
      ' Open Catalog object by using connection to the current database.
      .ActiveConnection = CurrentProject.Connection
      ' Create new group.
      .Groups.Append strGroup, strPID
   End With
   ' Close Catalog object.
   Set catDB = Nothing
End Sub

To add a new user account to an existing group, simply append that user's name to the Users collection of that group. The following procedure is designed to add a new user account to a group from within the current Access database.

Sub AddUserToGroupInAccess(strUser As String, _
                           strGroup As String)
   Dim catDB As ADOX.Catalog

   Set catDB = New ADOX.Catalog
   With catDB
      ' Open Catalog object by using connection to the current database.
      .ActiveConnection = CurrentProject.Connection
      ' Add strUser to strGroup.
      .Groups(strGroup).Users.Append strUser
   End With
   ' Close Catalog object.
   Set catDB = Nothing
End Sub

To delete a user or group account, use the Delete method of the Users or Groups collection. The following example deletes a user account from within the current Access database.

Sub DeleteUserInAccess(strUser As String)
   Dim catDB As ADOX.Catalog

   Set catDB = New ADOX.Catalog
   With catDB
      ' Open Catalog object by using connection to the current database.
      .ActiveConnection = CurrentProject.Connection
      ' Delete strUser.
      .Users.Delete strUser
   End With
   ' Close Catalog object.
   Set catDB = Nothing
End Sub

Keep the following additional points in mind when you are working with User and Group objects: