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

IF CurrentUser In WorkGroup problem??? 1

Status
Not open for further replies.

Kobayashi

Technical User
Oct 11, 2001
69
US
Can anybody please let me know if there is a easy way to check whether, 'IF', the CurrentUser belongs to a particular Workgroup? If so, this would lead to a 'THEN' argument.
Something like this is what I'm looking for:

IF CurrentUser In (or =) WorkGroup 'Name of WorkGroup' THEN
......

Regards,

Adrian
 
If you want to know whether the user is in the current workgroup (the one used to open this database), you can use the DAO Users collection:
Code:
    Set wsp = DBEngine.Workspaces(0)
    On Error Resume Next
    Set usr = wsp.Users("username")
    If Err = 0 Then
        (user is a member of the workgroup)
    Else
        (user is not a member of the workgroup)
    EndIf
    Set usr = Nothing  ' always release DAO objects
    Set wsp = Nothing

For any workgroup file, you can open it as a Database and look in its MSysAccounts table in the Name column.
Code:
    Set db = DBEngine.OpenDatabase("workgroup.mdw")
    Set rst = db.OpenRecordset("MSysAccounts")
    rst.FindFirst "Name='" & username & "'"
    If rst.NoMatch Then
        (user is not a member of the workgroup)
    Else
        (user is a member of the workgroup)
    End If
    rst.Close
    Set rst = Nothing  ' always release DAO objects
    db.Close
    Set db = Nothing
Rick Sprague
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Rick,

Many thanks for your reply. However, I have made a mistake by referring to the Workgroup! I should have said IF the CurrentUser is a member of a security 'group' that I have created?

Many Thanks,

Adrian
 
This will do it:
Code:
Public Function UserIsInGroup(UserName As String, GroupName As String) As Boolean
    Dim wsp As DAO.Workspace, usr As DAO.User

    On Error GoTo ErrorHandler
    Set wsp = DBEngine.Workspaces(0)
    wsp.Groups.Refresh
    wsp.Users.Refresh
    Set usr = wsp.Groups(GroupName).Users(UserName)
    UserIsInGroup = True
NotInGroup:
    Set usr = Nothing  ' always release DAO objects
    Set wsp = Nothing
    Exit Function
ErrorHandler:
    If Err = 3265 Then ' item not found in this collection
        Resume NotInGroup
    End If
    Err.Raise Err.Number
End Function
Note: The two calls to the Refresh methods are needed only if you are changing the Users and Groups collections within the current database, e.g. by using the Tools|Security|User and Group Accounts dialog. They'll do no harm if you're not doing that, however.

Rick Sprague
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Rick,

Again, thanks very much. Do I create this in a module and then call the function from the Forms event procedure (load) that I wish to use?

Regards,

Adrian
 
Yes, that's right--although you could call it from anywhere at all. One way to call it would be:
Code:
    If UserIsInGroup(CurrentUser, "DBAdmins") Then
        (enable administrator controls)
    Else
        (disable them)
    End If
Rick Sprague
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Rick,

Thanks very much, that's exactly what I wished to know.
I'll implement this on Monday and let you know how it goes.

Thanks again,

Adrian
 
Rick,

At first I put the code into an existing Public Module and then called it with an 'IF UserIsInGroup...' statement. However, this resulted in a statement/object not found/defined error?
I then put the code into the Form's Module that I was calling it from and it works perfectly!?
I'm not completely familiar with Private/Public modules so is this normal or should I have been able to call the Public Function in it's own Module within the application from the Private Sub Form module?

This is only for my understanding and to help me in future. The code works perfectly so thanks again.

Regards,

Adrian
 
As long as it was declared as "Public Function" in a standard module (not a class, form, or report module), you should have been able to call it from anywhere. I don't know what could have caused your problem, but I'm glad you got it working. Rick Sprague
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top