• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

Get Win NT Group for current User

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
naqayya
Asked:
naqayya
  • 11
  • 9
  • 2
  • +1
1 Solution
 
Richie_SimonettiIT OperationsCommented:
Take a look at this great code:
http://www.vbsquare.com/demos/adsi-demo/index.html
0
 
Richie_SimonettiIT OperationsCommented:
0
 
naqayyaAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Richie_SimonettiIT OperationsCommented:
VB-VBA is almost the same. You could open the frm,bas and cls files with Notepad.
0
 
naqayyaAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
You need to set a reference to Active DS type library first (activeds.tlb)
0
 
naqayyaAuthor Commented:
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
 
naqayyaAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
naqayyaAuthor Commented:
Thanks Richie.

I need a function that returns the Group name that the current User belongs to.
0
 
Richie_SimonettiIT OperationsCommented:
With preceding code, you have to pass group names and username to function until it returns True.
0
 
naqayyaAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
naqayyaAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
naqayyaAuthor Commented:
Could you show me the code to do that please?

Thanks.
0
 
naqayyaAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
Glad to here that and thanks for "A" grade.
Sorry for not to have too much time to help you more.
Cheers
0
 
rbsnowCommented:
Richie, you the man.  This is exactly what I needed.  Thank you, sir.
0
 
Richie_SimonettiIT OperationsCommented:
You are welcome.
0
 
ardukCommented:
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
 
Richie_SimonettiIT OperationsCommented:
it suppose to, i think. did you check those links?
0
 
ardukCommented:
Sorry - have since got that to work....but forgot to post another comment.

Thanks very much for getting back to me
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 11
  • 9
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now