Show/Hide controls depeding on User's wrkgrp Group

  • Thread starter Thread starter Walter via AccessMonster.com
  • Start date Start date
W

Walter via AccessMonster.com

Hello

How do i determine what wrkgrp Group the User belongs to during a Form's
OnOpen event?

Walter
 
'DAO method - requires DAO reference
Public Function IsUserInGroupD( _
ByVal UserName As String, _
ByVal GroupName As String _
) As Boolean

Dim usr As DAO.User
Dim grp As DAO.Group

Set usr = DBEngine.Workspaces(0).Users(UserName)
For Each grp In usr.Groups
If grp.Name = GroupName Then
IsUserInGroupD = True
Exit For
End If
Next grp

End Function

'ADOX method - requires ADOX reference
Public Function IsUserInGroupA( _
ByVal UserName As String, _
ByVal GroupName As String _
) As Boolean

Dim cat As ADOX.Catalog
Dim usr As ADOX.User
Dim grp As ADOX.Group

Set cat = New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
Set usr = cat.Users(UserName)
For Each grp In usr.Groups
If grp.Name = GroupName Then
IsUserInGroupA = True
Exit For
End If
Next grp

End Function

Examples of use, in the Immediate window ...

? isuseringroupd(currentuser(),"Admins")
True
? isuseringroupd(currentuser(),"Test")
False
? isuseringroupa(currentuser(),"Admins")
True
? isuseringroupa(currentuser(),"Test")
False
 
Thanks Brendan.

Brendan said:
'DAO method - requires DAO reference

'ADOX method - requires ADOX reference
? isuseringroupa(currentuser(),"Test")
False
 
Brendan said:
Public Function IsUserInGroupD( _
ByVal UserName As String, _
ByVal GroupName As String _
) As Boolean
Dim usr As DAO.User
Dim grp As DAO.Group
Set usr = DBEngine.Workspaces(0).Users(UserName)
For Each grp In usr.Groups
If grp.Name = GroupName Then
IsUserInGroupD = True
Exit For
End If
Next grp
End Function


Why iterate? You can test it directly, without having to loop. Also,
that code will fail with a runtime error if the specified user does not
exist.

This version returns True if the specified user exists AND the
specified group exists AND the specified user is a member of the
specified group - False otherwise:

Public Function IsUserInGroup( _
UserName As String, _
GroupName As String _
) As Boolean
dim s as string
on error resume next
s = dbengine(0)(0).users(UserName).groups(GroupName).name
isuseringroup = (err.number = 0)
End Function


Cheers,
TC
 
TC said:
Why iterate? You can test it directly, without having to loop.

Why not iterate? There would have to be tens of thousands of users and
groups before there would be any perceptible impact on performance.
Also,
that code will fail with a runtime error if the specified user does not
exist.

Rightly so. Attempting to refer to a non-existant user is an error and
should be treated as such. Determining whether a user or a group exist is a
separate operation from determining whether a user is a member of a
specified group. They should be separate functions or, at the least, if they
are combined the function should return different results to distingusih
between the various possibilities - user and group exist, user is in group,
user and group exist, user is not in group, user exists, group doesn't, user
does not exist, group does.

IMHO of course! :-)
 
Back
Top