Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function to check if a user belongs to some group

Status
Not open for further replies.

hayo

MIS
Sep 14, 2001
56
DE
Hi.

Has somebody code for a function, which receives a username and a user group name as input, and returns True if the user belongs to the specified group ?

Hayo
 
If you have a table in your db called tblGroupList that is set up with 2 fields, username & groupname then you could use the dlookup function:

Function InGroup(sUserName as string, sGroupName as String) as Boolean

if Dlookup("GroupName","tblGroupList","UserName='" & sUserName & "'")=sGroupName then
Ingroup=true
else
ingroup=false
end if

end function


HTH

Ben ----------------------------------
Ben O'Hara
Home: bpo@RobotParade.co.uk
Work: bo104@westyorkshire.pnn.police.uk
Web: ----------------------------------
 
Thanks for your quick answer, Ben !

The way you do it is ok, of course.

But what I am interested in is the check of the MS Access catalog. Currently, I use the access protection, so each user belongs to one or more user groups. So I need code to analyze the user and group definitions within access.
 
The following code will print out the groups the user "YourUserName" is assigned to.

Dim wsp As Workspace
Dim i As Integer

Set wsp = DBEngine.Workspaces(0)

For i = 0 To wsp.Users("YourUserName").Groups.Count - 1
Debug.Print wsp.Users("YourUserName").Groups(i).Name
Next i
 
If you are using Access 2000 or above here are a couple of functions you can check out and modify as needed.

Function JetSecurity2()
'-- set reference to ADOX library
'- Microsoft ADO Ext. 2.6 for DDL and Security
'-- Microsoft ActiveX data objects 2.6 library also needed for ADO

Dim cg As New ADOX.Catalog
Set cg.ActiveConnection = CurrentProject.Connection

Dim ur As User, gp As Group
For Each ur In cg.Users
Debug.Print "Users = "; ur.Name

For Each gp In ur.Groups
Debug.Print " Group = "; gp.Name

Next
Next

End Function

Function InGroup(Iuser As String, Igrp As String) As String
'-- set reference to ADOX library
'- Microsoft ADO Ext. 2.6 for DDL and Security
'-- Microsoft ActiveX data objects 2.6 library also needed for ADO

Dim cg As New ADOX.Catalog
Set cg.ActiveConnection = CurrentProject.Connection

InGroup = "No"

Dim ur As User, gp As Group
For Each ur In cg.Users
Debug.Print "Users = "; ur.Name
If ur.Name = Iuser Then
For Each gp In ur.Groups
Debug.Print " Group = "; gp.Name
If gp.Name = Igrp Then
InGroup = "Yes"
End If
Next
End If
Next
Debug.Print "In Group = "; InGroup

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top