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

search active directory

Status
Not open for further replies.

chanman525

IS-IT--Management
Oct 7, 2003
169
US
Hello experts,

I am trying to find a way to access active directory thur access 2003. I have added the ActiveDs type lib and tried to use the object browser to figure out how to write the code but I am at my wits end. Does anyone happen to have a snippet of code I could use to query which groups each user is a member of in AD. We have windows server 2003 and I am using access 2003. Any help on how to set up this connection would be much appreciated.

Thanks much,
tired admin
 
Code:
Option Compare Database
Option Explicit

Public Function ListUserMemberof(CurrentUserName) As Boolean
Dim strGroup
Dim objGroup As IADsGroup
Dim objUser As IADsUser
Set objUser = GetObject("LDAP://" & GetADDistinguishedNames(CurrentUserName))
If Not IsEmpty(objUser.memberof) Then
    For Each strGroup In objUser.memberof
        Set objGroup = GetObject("LDAP://" & strGroup)
        Debug.Print objGroup.cn
    Next
End If
End Function
Public Function GetADDistinguishedNames(CurrentUserName)
Dim objConnection As ADODB.Connection
Dim objCommand As ADODB.Command
Dim objRecordset As ADODB.Recordset
Dim struser As String
Dim strGroup
Dim objUser
Dim objGroup
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.ConnectionTimeout = 0
objCommand.CommandTimeout = 0
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://DomainName' WHERE objectCategory='user' AND sAMAccountName ='" & CurrentUserName & "'"

Set objRecordset = objCommand.Execute

objRecordset.MoveFirst
GetADDistinguishedNames = objRecordset!distinguishedName

End Function
 
chanman525,

Here is an ADS object based example thread705-1420301.

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Hi CautionMP and Pwise,

Both of your posts look like exactly what I need but I am haveing problems calling the code. I think I should be able to call this function from a query but I am not haveing any luck getting that to work. How is this code intended to be called to display information? Thanks very much for both of your responses.

Ryan
 
chanman525,
You should be able to run the routine from the other post in a query as a calculated field (but it will be slo
To test it you may want to try running it in the Immediate window to make sure you have the arguments correct ([tt]Environ$[/tt] requires Windows 2000 or newer.)


[tt]? GetUserGroup(Environ$("USERDOMAIN"), Environ$("USERNAME"))[/tt]

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top