User groups in VBA Code

  • Thread starter Thread starter Jim Franklin
  • Start date Start date
J

Jim Franklin

Hi,

Can anyone tell me if it is possible to determine whether the current user
is a member of a particular user group by using code. I want to modify the
appearance of a form on opening, depending on whether the user is a member
of group xxxx.

Thanks as always for any help anyone can provide,

Jim F.
 
There is code in the Security FAQ to do that. Bear in mind that a given user
can be a member of more than one group.
 
Function IsUserInGroup(strUser As String, strGroup As String) As Boolean
'Returns True if the user is in the group.
'Example: IsUserInGroup(CurrentUser(), "Admins")
Dim wk As Workspace
Dim grx As Groups
Dim grp As Group
Dim usx As Users
Dim usr As User

Set grx = DBEngine(0).Groups
For Each grp In grx
If grp.Name = strGroup Then
Set usx = grp.Users
For Each usr In usx
If usr.Name = strUser Then
IsUserInGroup = True
Exit For
End If
Next
End If
Next

Set usr = Nothing
Set usx = Nothing
Set grp = Nothing
Set grx = Nothing
End Function
 
Jim,

You might find this a bit simpler:

Public Function IsUserInGroup(sUser As String, sGroup As String) As Boolean
'Determines if a specific user belongs to a specific group
Dim sReturn As String

On Error Resume Next

Err.Clear
sReturn = DBEngine(0).Groups(sGroup).Users(sUser).Name
IsUserInGroup = (Err = 0)
End Function

You can determine the current username with:
DBEngine(0).UserName

So, you can call the above function like so:
If IsUserInGroup(DBEngine(0).UserName, "Admins") Then
'The user is a member of the Admins group.
End If

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
Back
Top