INF: How to Determine If Current User Is in a Particular Group
  
PSS ID Number: Q119707
Article last modified on 08-17-1995
 
2.00
 
WINDOWS
 

---------------------------------------------------------------------
The information in this article applies to:
 
 - Microsoft Access version 2.0
---------------------------------------------------------------------
 
SUMMARY
=======
 
This article describes a sample function that you can use to determine if
the current user of the database is a member of a particular group, such as
the Admins group. This function can be useful when you need to determine
whether the user should be allowed to access certain commands or carry out
certain tasks.
 
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools provided
with Microsoft Access. For more information on Access Basic, please refer
to the "Building Applications" manual.
 
MORE INFORMATION
================
 
The following steps describe how to create and use the sample function
CurrentUserInGroup():
 
1. Create a new module and enter the following line in the Declarations
   section:
 
      Option Explicit
 
2. Enter the following code in the module:
 
   Function CurrentUserInGroup (groupname As String)
 
   'Purpose: Determines if the current user is in a 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 "Invalid group name"
      Else MsgBox Error(Err)
      End If
      Myworkspace.Close
      Exit Function
 
   End Function
 
3. From the View menu, choose Immediate Window.
 
4. Type the following line in the Immediate window, and then press ENTER:
 
      ? CurrentUserInGroup("admins")
 
If the current user is a member of the Admins group, -1 (true) is returned.
If the current user is not a member of the Admins group, 0 (false) is
returned.
 
REFERENCES
==========
 
Microsoft Access "Building Applications," version 2.0, Chapter 14,
"Securing Your Application"
 
Additional reference words: 2.00 belongs
KBCategory: kbusage
KBSubcategory: PgmOthr
=============================================================================
Copyright Microsoft Corporation 1995.
