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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

group of security

Status
Not open for further replies.

vilmarie

Programmer
Feb 10, 2004
41
PR
How I can obtain the name of the group that the user is member? example: username:vfiguer
groupneme:ADMINISTRATOR.

The function CurrentUser() give the name of the username, but how I obtain the name of the group of this username?
 
The following routine will tell you if the user is a member of a specified group.
Code:
Function UserBelongsToGroup(strGroupName As String, _
                   Optional varCurrentUser As Variant) As Boolean

'********************************
'*  Declaration Specifications  *
'********************************

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

    Dim strCurrentUser As String
    
'****************
'*  Initialize  *
'****************

    On Error GoTo ErrHandler
    
    If (IsMissing(varCurrentUser)) Then
        strCurrentUser = CurrentUser
    Else
        strCurrentUser = CStr(varCurrentUser)
    End If
        
    Set wsp = DBEngine.Workspaces(0)
    Set grp = wsp.Groups(strGroupName)
    
'******************************************************************
'*  Loop to determine if the User is part of the group specified  *
'******************************************************************

    For Each usr In grp.Users
        If (usr.Name = CurrentUser) Then UserBelongsToGroup = True: GoTo ExitProcedure
    Next
    
    UserBelongsToGroup = False
    
'********************
'*  Exit Procedure  *
'********************

ExitProcedure:

    On Error Resume Next
    
    wsp.Close
    Set wsp = Nothing
    Set grp = Nothing
    
    Exit Function

'****************************
'*  Error Recovery Section  *
'****************************

ErrHandler:

    If Err = 3265 Then
        Err.Raise Err.Number, "UserBelongsToGroup", """" & UCase(strGroupName) & """ isn't a valid group account."

    ElseIf Err = 3029 Then
        Err.Raise Err.Number, "UserBelongsToGroup", "The account used to create the workspace does not exist."
         
    Else
        Err.Raise Err.Number, "UserBelongsToGroup", Err.Description
    
    End If
    
End Function
 
Thanks Fancy, but How I obtain the group that correspond to this user?
 
Sorry 'bout that. I read your post, then got interrupted and when I came back to answer the post I guess I assumed I remembered what you asked. Guess not.

Note that a user can be a member of more than one group. The following code will list all of the groups a user is a member of. Also note that if you don't pass the name of the user, it defaults to CurrentUser. Therefore, you can call this roiutine 1 of 2 ways:

1) UserBelongsToGroup

2) UserBelongsToGroup "JustinOtherSmith"

Code:
Function UserBelongsToGroup(Optional varCurrentUser As Variant)

'********************************
'*  Declaration Specifications  *
'********************************

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

    Dim strCurrentUser As String
    
'****************
'*  Initialize  *
'****************

    On Error GoTo ErrHandler
    
    If (IsMissing(varCurrentUser)) Then
        strCurrentUser = CurrentUser
    Else
        strCurrentUser = CStr(varCurrentUser)
    End If
        
    Set wsp = DBEngine.Workspaces(0)
    
    For Each grp In wsp.Groups
        MsgBox grp.Name
    Next
    
'********************
'*  Exit Procedure  *
'********************

ExitProcedure:

    On Error Resume Next
    
    wsp.Close
    Set wsp = Nothing
    Set grp = Nothing
    
    Exit Function

'****************************
'*  Error Recovery Section  *
'****************************

ErrHandler:

    MsgBox Err.Number & Err.Description
    Resume ExitProcedure
    
End Function
 
Where I can insert this code: in a module or in the form?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top