With secured databases, one very common scenario is to try & detect the name of the GROUP the current logged-in user belongs to. It's easy enough to detect the name of the user; the expression CurrentUser() does this. But detecting the name of the GROUP the user belongs to is a different matter.
Finally, today, in a reply to a newsgroup posting I made, I got my answer.
You need to create a module, name it something like mdlIsInGroup. The code follows:
Public Function IsInGroup(UsrName As String, GrpName As String) As Boolean 'Determines whether UsrName is a member of GrpName '-- 'http://www.attcanada.net/~kallal.msn/RidesSec/index.html '--Dec 16th 2002 microsoft.public.access.security 'Albert D.kallal 'Edmonton, Alberta Canada 'kallal@ msn.com 'www.attcanada.net/~kallal.msn Dim grp As Group Dim IIG As Boolean Dim usr As User IIG = False For Each usr In DBEngine.Workspaces(0).Users If usr.name = UsrName Then GoTo FoundUser Next GoTo IIG_Exit FoundUser: For Each grp In usr.Groups If grp.name = GrpName Then IIG = True Next IIG_Exit: IsInGroup = IIG End Function |
Then, the code in the control in question which would actually check would look like this:
Private Sub accounting_approved_by_BeforeUpdate(Cancel As Integer) ' Checks to see if this user belongs to the proper security group ' before allowing them to change the contents of this field If (IsInGroup(CurrentUser(), "Accountants") = False) And _ (IsInGroup(CurrentUser(), "Admins") = False) Then MsgBox "You don't belong to the accountants groups " _ & "and thus can't change the contents of this field", vbCritical Cancel = True End If End Sub |
In the above example, the FIELD being checked was accounting_approved_by, and the contents of the field were to only be allowed to be edited if the user was an administrator or a member of the Accountants group.
Derived from newsgroup microsoft.public.access.security thread Get Users security Group name dated Wednesday, March 27, 2002 7:30 PM
Hi:
I am using user-level security in my database, and would like to retrieve the users Group name. I realize CurrentUser() returns the user account name, but it would be much easier for my process to retrieve the users Group name instead. Is there a method that achieves this, or if not, could anyone suggest a way to accomplish this through code. I'm somewhat new and my attempts to code this haven't been successful. Thx...
~ J
Janet,
A user can belong to more than one group, so the following code will enumerate ALL the groups that the current user belongs to.
Dim ctr As Group
For Each ctr In DBEngine(0).Users(CurrentUser()).Groups
Debug.Print ctr.Name
Next ctr
A user can belong to more than one group. You can determine which groups a user belongs to using the Groups collection of the DAO User object. For example, the following code will print to the debug window the names of all groups the current user belongs to:
Dim wrk As DAO.Workspace
Dim usr As DAO.User
Dim grp As DAO.Group
Set wrk = DBEngine.Workspaces(0)
Set usr = wrk.Users(CurrentUser())
For Each grp In usr.Groups
Debug.Print grp.Name
Next grp