| 
ACC2000: Sample Functions to Check User and Group Information
ID: Q210331
 
 | 
The information in this article applies to:
Advanced: Requires expert coding, interoperability, and multiuser skills.
SUMMARY
This article contains several sample user-defined functions that you can
use to do the following:
- Return a list of users in the current system database.
- Return a list of groups in the current system database.
- Return a list of users in a specified group.
- Return a list of groups to which a specified user belongs.
- Determine if the current user belongs to a specified group.
MORE INFORMATION
You can use the following sample functions to return user and group
information in the current system database. Note that each function assumes
there is a user called Developer who is a member of the Admins group, and
that the Developer account has no password.
Microsoft provides programming examples for illustration only, without warranty 
either expressed or implied, including, but not limited to, the implied warranties of 
merchantability and/or fitness for a particular purpose. This article assumes that you 
are familiar with the programming language being demonstrated and the tools used to 
create and debug procedures. Microsoft support professionals can help explain the functionality 
of a particular procedure, but they will not modify these examples to provide added 
functionality or construct procedures to meet your specific needs. If you have limited 
programming experience, you may want to contact a Microsoft Certified  Solution Provider 
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the 
following page on the World Wide Web: 
 http://www.microsoft.com/support/supportnet/overview/overview.asp
The Sample Functions
'********************************************************
' Declarations section of the module
'********************************************************
Option Compare Database
Option Explicit 
Function ListUsersInSystem()
'****************************************************************
' Purpose: Lists users in the current system database.
' Accepts: No arguments.
' Returns: A list of users in the current system database.
' Assumes: The existence of a user called Developer in the Admins
'          group, with no password.
'****************************************************************
   On Error GoTo err_ListUsersInSystem
   Dim MyWorkSpace As WorkSpace, i As Integer
   ' Create a new workspace as a member of the Admins group.
   Set MyWorkSpace = DBEngine.CreateWorkspace("SPECIAL", "Developer", "")
   For i = 0 To MyWorkSpace.Users.count - 1
      Debug.Print MyWorkSpace.Users(i).Name
   Next i
   MyWorkSpace.Close
   Exit Function
err_ListUsersInSystem:
   If Err = 3029 Then
      MsgBox "The account used to create the workspace does not exist"
   Else MsgBox Error(Err)
   End If
   MyWorkSpace.Close
   Exit Function
End Function 
Function ListGroupsInSystem()
'****************************************************************
' Purpose: Lists groups in the current system database.
' Accepts: No arguments.
' Returns: A list of groups in the current system database.
' Assumes: The existence of a user called Developer in the Admins
'          group, with no password.
'****************************************************************
   On Error GoTo err_ListGroupsInSystem
   Dim MyWorkSpace As WorkSpace, i As Integer
   ' Create a new workspace as a member of the Admins group.
   Set MyWorkSpace = DBEngine.CreateWorkspace("SPECIAL", "Developer", "")
   For i = 0 To MyWorkSpace.Groups.count - 1
        Debug.Print MyWorkSpace.Groups(i).Name
   Next i
   MyWorkSpace.Close
   Exit Function
err_ListGroupsInSystem:
   If Err = 3029 Then
        MsgBox "The account used to create the workspace does not exist"
   Else MsgBox Error(Err)
   End If
   MyWorkSpace.Close
   Exit Function
End Function 
Function ListUsersOfGroup(GroupName As String)
'****************************************************************
' Purpose: Lists users who are members of the specified group in
'          the current system database.
' Accepts: The name of a group.
' Returns: A list of users in the specified group.
' Assumes: The existence of a user called Developer in the Admins
'          group, with no password.
'****************************************************************
   On Error GoTo err_ListUsersOfGroup
   Dim MyWorkSpace As WorkSpace, i As Integer
   Dim MyGroup As Group
   ' Create a new workspace as a member of the Admins group.
   Set MyWorkSpace = DBEngine.CreateWorkspace("SPECIAL", "Developer", "")
   Set MyGroup = MyWorkSpace.Groups(GroupName)
   For i = 0 To MyGroup.Users.count - 1
      Debug.Print MyGroup.Users(i).Name
   Next i
   MyWorkSpace.Close
   Exit Function
err_ListUsersOfGroup:
   If Err = 3265 Then
      MsgBox UCase(GroupName) & " isn't a valid group name", 16, "Error"
   ElseIf Err = 3029 Then
      MsgBox "The account used to create the workspace does not exist"
   Else MsgBox Error(Err)
   End If
   MyWorkSpace.Close
   Exit Function
End Function 
Function ListGroupsOfUser(UserName As String)
'****************************************************************
' Purpose: Lists the groups to which a specified user belongs.
' Accepts: The name of a user.
' Returns: A list of groups for the specified user.
' Assumes: The existence of a user called Developer in the Admins
'          group, with no password.
'****************************************************************
   On Error GoTo err_ListGroupsOfUser
   Dim MyWorkSpace As WorkSpace, i As Integer
   Dim MyUser As User
   ' Create a new workspace as a member of the Admins group.
   Set MyWorkSpace = DBEngine.CreateWorkspace("SPECIAL", "Developer", "")
   Set MyUser = MyWorkSpace.Users(UserName)
   For i = 0 To MyUser.Groups.count - 1
      Debug.Print MyUser.Groups(i).Name
   Next i
   MyWorkSpace.Close
   Exit Function
err_ListGroupsOfUser:
   If Err = 3265 Then
      MsgBox UCase(UserName) & " isn't a valid user name", 16, "Error"
   ElseIf Err = 3029 Then
      MsgBox "The account used to create the workspace does not exist"
   Else MsgBox Error(Err)
   End If
   MyWorkSpace.Close
   Exit Function
End Function 
Function CurrentUserInGroup(GroupName As String)
'****************************************************************
' Purpose: Determines if the current user belongs to the specified
'          group.
' Accepts: The name of a group.
' Returns: True if the current user is a member of the specified
'          group, False if the current user is not a member of
'          the group.
' Assumes: The existence of a user called Developer in the Admins
'          group, with no password.
'****************************************************************
   On Error GoTo err_CurrentUserInGroup
   Dim MyWorkSpace As WorkSpace, i As Integer
   Dim MyGroup As Group, MyUser As User
   ' Create a new workspace as a member of the Admins group.
   Set MyWorkSpace = DBEngine.CreateWorkspace("SPECIAL", "Developer", "")
   Set MyGroup = MyWorkSpace.Groups(GroupName)
   Set MyUser = MyWorkSpace.Users(CurrentUser())
   For i = 0 To MyGroup.Users.count - 1
      If MyGroup.Users(i).Name = MyUser.Name Then
         CurrentUserInGroup = True
         Exit Function
      End If
   Next i
   CurrentUserInGroup = False
   MyWorkSpace.Close
   Exit Function
err_CurrentUserInGroup:
   If Err = 3265 Then
      MsgBox UCase(GroupName) & " isn't a valid group name", 16, "Error"
      CurrentUserInGroup = False
   ElseIf Err = 3029 Then
      MsgBox "The account used to create the workspace does not exist"
   Else MsgBox Error(Err)
   End If
   MyWorkSpace.Close
   Exit Function
End Function 
To test these functions, run them in the Immediate window. For example, to test the ListGroupsOfUser() function, follow these steps:
- Open the sample database Northwind.mdb.
- Create a new module and enter the sample functions.
- On the View menu, click Immediate Window.
- In the Immediate window, type the following line, and then press ENTER:
? ListGroupsOfUser("Admin") 
REFERENCES
For more information about user and group accounts, click Microsoft Access Help on the 
Help menu, type "work with user and group accounts" in the Office Assistant or the Answer Wizard, 
and then click Search to view the topics returned.
Additional query words: 
security dao retrieve 
Keywords          : kbprg kbdta AccCon KbVBA 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbhowto