Solved

Get Win NT Group for current User

Posted on 2002-05-14
23
482 Views
Last Modified: 2008-02-26
I want to hide a button on a Form for Users that belong to a certain Windows NT Group.

I need to know an easy way to find out which Win NT Group the current User belongs to.

Can someone show me the VBA code to do this?

Thanks.
0
Comment
Question by:naqayya
  • 11
  • 9
  • 2
  • +1
23 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7009746
Take a look at this great code:
http://www.vbsquare.com/demos/adsi-demo/index.html
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7009755
0
 

Author Comment

by:naqayya
ID: 7010426
Thanks Richie:

I haven't got Visual Basic, only the VBA application that comes with Access 2000, so I don't know how to open the files. I'm a bit of a novice so please could you tell me which file has the code that I need and how I could use that code behind my Form.

Thanks again.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7011095
VB-VBA is almost the same. You could open the frm,bas and cls files with Notepad.
0
 

Author Comment

by:naqayya
ID: 7011199
I tried pasting the following code in my module:

Public Sub RemoveUser(strUsername, strGroup, strDomain)

Dim adsGroup As ActiveDs.IADsGroup

  Set adsGroup = GetObject("WinNT://" + strDomain + "/" + strGroup)
  If adsGroup.IsMember(strUsername) Then
    adsGroup.Remove "WinNT://" + strDomain + "/" + strGroup + "/" + strUsername
  End If

End Sub


I get a compile error for the Dim line saying 'User-defined type not defined'.

Can you help? Is this the right code? Are these 2 lines sufficient?

Dim adsGroup As ActiveDs.IADsGroup

Set adsGroup = GetObject("WinNT://" + strDomain + "/" + strGroup)

Thanks.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7011311
You need to set a reference to Active DS type library first (activeds.tlb)
0
 

Author Comment

by:naqayya
ID: 7011356
I have set the reference.

Could you tell me what changes I have to make to the code so that I can have a function that returns the Win NT Group for the current User?

Thanks.
0
 

Author Comment

by:naqayya
ID: 7011448
I have set the reference.

Could you tell me what changes I have to make to the code so that I can have a function that returns the Win NT Group for the current User?

Thanks.
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 300 total points
ID: 7011514
Well, i never used it and since we have Linux here, i can't try it, but i supossse that this should works:
' function returns false if memeber doesn't belongs to group, otherwise True

Public Function MembersOfGroup(strGroup, strDomain, strMember As String) As Boolean
Dim adsGroup As ActiveDs.IADsGroup
Dim adsMembers As ActiveDs.IADsMembers
Dim adsMember As Object
       
On Error Resume Next
Set adsGroup = GetObject("WinNT://" + strDomain + "/" + strGroup)
MembersOfGroup = False
If Err.Number = 0 Then
    Err.Clear
    On Error GoTo 0
    Set adsMembers = adsGroup.Members
    Set oCol = New Collection
    adsMembers.Filter = Array("Members")
    For Each adsMember In adsMembers
        If adsMember.Name = strMember Then
            MembersOfGroup = True
            Exit For
        End If
    Next adsMember
    Set adsMember = Nothing
    Set adsMembers = Nothing
    Set adsGroup = Nothing
End If

End Function
0
 

Author Comment

by:naqayya
ID: 7019387
Thanks Richie.

I need a function that returns the Group name that the current User belongs to.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7023011
With preceding code, you have to pass group names and username to function until it returns True.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:naqayya
ID: 7023366
Thanks again Richie.

This could be a problem. I can get the current UserName and pass it to your function, so that is OK, but I will have to pass each and every Group. If we change the Group names or add/remove Groups then the code will have to be changed each time. I would rather not have this extra maintenance for the code.

Is there no way that I can pass the current UsetrName and get the Group name?

Thanks Richie (sorry to to pester you).
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7023917
i don't know how did you implement that, but if you don't pass username, code shouldn't know to which user we are talking about.
If you add/remove groups... we are stored their names?
In an INI file?, a database? Being dynamic, their must be sotored outside the main program (i mean, on another file than the executable itself), program should read that file and act accordingly, what do you think?
i'll be waiting...
Cheers
0
 

Author Comment

by:naqayya
ID: 7023952
Of course, we have to pass the UserName (and maybe Domain also).
But I want a function in which I don't have to pass the Group name.
The function should get the Group name from Windows. I don't know where Windows stores the Group names and members though.

Anu ideas? Thanks!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7024082
Point well stated!.
Afaik, we need to continue using ADSI to enumerate all groups for a given domain, then in each "lap" of that enumeration, pass the group name with username until we got return true value.
0
 

Author Comment

by:naqayya
ID: 7024118
Could you show me the code to do that please?

Thanks.
0
 

Author Comment

by:naqayya
ID: 7032996
Richie:

Hey, your code works! I tested it out today and it's perfect!

Please forgive me for taking so long, and thanks for your patience with me.

Thanks again!!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7033325
Glad to here that and thanks for "A" grade.
Sorry for not to have too much time to help you more.
Cheers
0
 
LVL 1

Expert Comment

by:rbsnow
ID: 7614589
Richie, you the man.  This is exactly what I needed.  Thank you, sir.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7624051
You are welcome.
0
 
LVL 7

Expert Comment

by:arduk
ID: 7679355
Am trying to find out how to list the names of members of a Win 2000 user group, and came across your code. Do you konw if this will work with win 2000?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7681519
it suppose to, i think. did you check those links?
0
 
LVL 7

Expert Comment

by:arduk
ID: 7681551
Sorry - have since got that to work....but forgot to post another comment.

Thanks very much for getting back to me
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now