r_naren22atyahoo
asked on
VB LDAP Query
Hi Guys,
i am trying to use this sample code for a VB Form
-------------
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
Set rs = conn.Execute( _
"<LDAP://server/o=organiza tion/ou=si te/cn=reci pients>;" _
& "(objectClass=*);ADsPath,o bjectClass ,cn;subtre e")
While Not rs.EOF
Debug.Print rs.Fields(0).Value, rs.Fields(1).Value, _
rs.Fields(2).Value
rs.MoveNext
Wend
conn.Close
--------------------
Reference
http://support.microsoft.com/kb/q187529/
i am not very good at the LDAP query language
so what i did is i used Windows 2003 AD to create a query,
i.e. Find Exchange recipients
Mail-Enabled Groups and for that input
the query Definition is as follows
(&(&(& (mailnickname=*) (| (objectCategory=group) ))))
How can i used this in the above VB Code.
Main idea is to populate the List Box with the all mail-Enabled Groups including the Distribution groups
regards
Naren
i am trying to use this sample code for a VB Form
-------------
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set conn = New ADODB.Connection
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
Set rs = conn.Execute( _
"<LDAP://server/o=organiza
& "(objectClass=*);ADsPath,o
While Not rs.EOF
Debug.Print rs.Fields(0).Value, rs.Fields(1).Value, _
rs.Fields(2).Value
rs.MoveNext
Wend
conn.Close
--------------------
Reference
http://support.microsoft.com/kb/q187529/
i am not very good at the LDAP query language
so what i did is i used Windows 2003 AD to create a query,
i.e. Find Exchange recipients
Mail-Enabled Groups and for that input
the query Definition is as follows
(&(&(& (mailnickname=*) (| (objectCategory=group) ))))
How can i used this in the above VB Code.
Main idea is to populate the List Box with the all mail-Enabled Groups including the Distribution groups
regards
Naren
ASKER
Hi Rory,
Thanks, But i have a problem.
we have multiple domains here.
So if i want to query a perticular windows 2003 server, what i need to change in the code??
Thanks, But i have a problem.
we have multiple domains here.
So if i want to query a perticular windows 2003 server, what i need to change in the code??
Never tried it on a multiple domain arrangement, but see if this works:
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sBase As String
Dim sFilter As String
Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
'On Error GoTo ErrHandler:
'Get record set
sBase = "<LDAP://DC=domain,DC=com> " <------------ change to the appropriate domain
sFilter = "(& (mailnickname=*) (| (objectCategory=group) ))"
sAttribs = "ADsPath,objectClass,cn"
sDepth = "subtree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
With conn
.CursorLocation = adUseClient
.Open "Data Source=Active Directory Provider;Provider=ADsDSOOb ject"
End With
With rs
.CursorLocation = adUseClient
.ActiveConnection = conn
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
End With
On Error Resume Next
rs.MoveFirst
While Not rs.EOF
Debug.Print rs.Fields(0).Value
'cboAuthor.AddItem rs.Fields(0).Value
rs.MoveNext
Wend
RoutineExit:
Exit Sub
ErrHandler:
On Error Resume Next
If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State <> 0 Then conn.Close
Set conn = Nothing
End If
Resume RoutineExit
End Sub
HTH
Rory
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sBase As String
Dim sFilter As String
Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
'On Error GoTo ErrHandler:
'Get record set
sBase = "<LDAP://DC=domain,DC=com>
sFilter = "(& (mailnickname=*) (| (objectCategory=group) ))"
sAttribs = "ADsPath,objectClass,cn"
sDepth = "subtree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
With conn
.CursorLocation = adUseClient
.Open "Data Source=Active Directory Provider;Provider=ADsDSOOb
End With
With rs
.CursorLocation = adUseClient
.ActiveConnection = conn
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
End With
On Error Resume Next
rs.MoveFirst
While Not rs.EOF
Debug.Print rs.Fields(0).Value
'cboAuthor.AddItem rs.Fields(0).Value
rs.MoveNext
Wend
RoutineExit:
Exit Sub
ErrHandler:
On Error Resume Next
If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State <> 0 Then conn.Close
Set conn = Nothing
End If
Resume RoutineExit
End Sub
HTH
Rory
ASKER
I am gettings a error "table does not exist"
at this line
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
regards
naren
at this line
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
regards
naren
Hi Naren,
That leads me to think that whatever you altered the line:
sBase = "<LDAP://DC=domain,DC=com> "
to be was incorrect. If your company domain iswidgets.com, you would alter this to:
sBase = "<LDAP://DC=widgets,DC=com >"
or if it is widgets.co.uk, you would use
sBase = "<LDAP://DC=widgets,DC=co, DC=uk>"
Does that help?
Rory
That leads me to think that whatever you altered the line:
sBase = "<LDAP://DC=domain,DC=com>
to be was incorrect. If your company domain iswidgets.com, you would alter this to:
sBase = "<LDAP://DC=widgets,DC=com
or if it is widgets.co.uk, you would use
sBase = "<LDAP://DC=widgets,DC=co,
Does that help?
Rory
ASKER
our domain is
testing.local
i used as
sBase = "<LDAP://DC=test,DC=local> "
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
when i put the cursor on these variables
it gives me the
"<LDAP://DC=test,DC=local> ", ADOconnection, 1, 3
are there any simple LDAP commands to check the output using a message box.???
testing.local
i used as
sBase = "<LDAP://DC=test,DC=local>
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
when i put the cursor on these variables
it gives me the
"<LDAP://DC=test,DC=local>
are there any simple LDAP commands to check the output using a message box.???
Not sure if that's a typo, but if your domain is testing.local then you need sBase = "<LDAP://DC=testing,DC=loc al>", not "<LDAP://DC=test,DC=local> "
Which ouput do you want to check? Currently the code will print a list of what it finds to the immediate window but it could be changed to use msgbox rather than debug.print (note: these are not LDAP commands, but VB ones. LDAP is just a means to query the Directory)
Regards,
Rory
Which ouput do you want to check? Currently the code will print a list of what it finds to the immediate window but it could be changed to use msgbox rather than debug.print (note: these are not LDAP commands, but VB ones. LDAP is just a means to query the Directory)
Regards,
Rory
ASKER
sorry it was a typo here,
i was guessing that it could be a recordset problem
so i thought to query and print the output without recoredset.
like printing the output of 1 groupname, i mean the first one in the object list
i was guessing that it could be a recordset problem
so i thought to query and print the output without recoredset.
like printing the output of 1 groupname, i mean the first one in the object list
That error means that the query is not finding any data to put in the recordset, so it's not a recordset error, but something wrong with the query string and I am 99% sure it will be a problem with the <LDAP://...> bit. I will try and do some research on it.
Rory
Rory
ASKER
OK
may be i will try to use a simple filter....
instead of
sFilter = "(& (mailnickname=*) (| (objectCategory=group) ))"
any other simple one.
to make sure the LDAP is rite.
may be i will try to use a simple filter....
instead of
sFilter = "(& (mailnickname=*) (| (objectCategory=group) ))"
any other simple one.
to make sure the LDAP is rite.
ASKER
hay,
i pointed the problem its here
sBase = "<LDAP://DC=domain,DC=com> " it should be
sBase = "<LDAP://computername/DC=d omain,DC=c om>"
i got the output, however it is as follows
LDAP://computername/CN=IT- Syd,OU=Gro ups,OU=SYD ,DC=test,D C=local
How can i just get the output as "IT-Syd"
regards
Naren
i pointed the problem its here
sBase = "<LDAP://DC=domain,DC=com>
sBase = "<LDAP://computername/DC=d
i got the output, however it is as follows
LDAP://computername/CN=IT-
How can i just get the output as "IT-Syd"
regards
Naren
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Naren,
Ta for the points and grade.
Rory
Ta for the points and grade.
Rory
ASKER
No, Thank you for your presious help.
i havent tested the code, but i hope it would work.
i have a similar question to ask, i will ping you on this question on monday
thanks
Naren
i havent tested the code, but i hope it would work.
i have a similar question to ask, i will ping you on this question on monday
thanks
Naren
ASKER
hi Rory,
I have asked a similar question here,
Any help would be appreciated
https://www.experts-exchange.com/questions/21850138/VB-LDAP-Query-Member-of.html
regards
Naren
I have asked a similar question here,
Any help would be appreciated
https://www.experts-exchange.com/questions/21850138/VB-LDAP-Query-Member-of.html
regards
Naren
See if this does what you want:
Private Sub GetUserList(Optional varInput As Variant)
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim oRoot As IADs
Dim oDomain As IADs
Dim sBase As String
Dim sFilter As String
Dim sDomain As String
Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
Dim user As IADsUser
'On Error GoTo ErrHandler:
Set oRoot = GetObject("LDAP://rootDSE"
'Work in the default domain
sDomain = oRoot.Get("defaultNamingCo
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
'Get record set
sFilter = "(& (mailnickname=*) (| (objectCategory=group) ))"
sAttribs = "ADsPath,objectClass,cn"
sDepth = ADS_SCOPE_SUBTREE
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
With conn
.CursorLocation = adUseClient
.Open "Data Source=Active Directory Provider;Provider=ADsDSOOb
End With
With rs
.CursorLocation = adUseClient
.ActiveConnection = conn
.Open sQuery, conn, adOpenKeyset, adLockOptimistic
End With
On Error Resume Next
rs.MoveFirst
While Not rs.EOF
Debug.Print rs.Fields(0).Value
'cboAuthor.AddItem rs.Fields(0).Value
rs.MoveNext
Wend
RoutineExit:
Exit Sub
ErrHandler:
On Error Resume Next
If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
Set rs = Nothing
End If
If Not conn Is Nothing Then
If conn.State <> 0 Then conn.Close
Set conn = Nothing
End If
Set oRoot = Nothing
Set oDomain = Nothing
Resume RoutineExit
End Sub
HTH
Rory