Link to home
Start Free TrialLog in
Avatar of r_naren22atyahoo
r_naren22atyahooFlag for Australia

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=organization/ou=site/cn=recipients>;" _
            & "(objectClass=*);ADsPath,objectClass,cn;subtree")

      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
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi 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("defaultNamingContext")
   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=ADsDSOObject"
   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
Avatar of r_naren22atyahoo

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??
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=ADsDSOObject"
   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
I am gettings a error "table does not exist"
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
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.???
Not sure if that's a typo, but if your domain is testing.local then you need sBase = "<LDAP://DC=testing,DC=local>", 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
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
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
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.
hay,

i pointed the problem its here
sBase = "<LDAP://DC=domain,DC=com>"     it should be
sBase = "<LDAP://computername/DC=domain,DC=com>"


i got the output, however it is as follows
LDAP://computername/CN=IT-Syd,OU=Groups,OU=SYD,DC=test,DC=local

How can i just get the output as "IT-Syd"

regards
Naren
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Naren,
Ta for the points and grade.
Rory
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
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