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

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
0
r_naren22atyahoo
Asked:
r_naren22atyahoo
  • 8
  • 7
1 Solution
 
Rory ArchibaldCommented:
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
0
 
r_naren22atyahooAuthor Commented:
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??
0
 
Rory ArchibaldCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
r_naren22atyahooAuthor Commented:
I am gettings a error "table does not exist"
at this line
.Open sQuery, conn, adOpenKeyset, adLockOptimistic

regards
naren
0
 
Rory ArchibaldCommented:
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
0
 
r_naren22atyahooAuthor Commented:
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.???
0
 
Rory ArchibaldCommented:
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
0
 
r_naren22atyahooAuthor Commented:
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
0
 
Rory ArchibaldCommented:
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
0
 
r_naren22atyahooAuthor Commented:
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.
0
 
r_naren22atyahooAuthor Commented:
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
0
 
Rory ArchibaldCommented:
Hi,
If you change this line:
sAttribs = "ADsPath,objectClass,cn"
to this:
sAttribs = "CN,objectClass,cn"
it should output as you want.

HTH

Rory
0
 
Rory ArchibaldCommented:
Naren,
Ta for the points and grade.
Rory
0
 
r_naren22atyahooAuthor Commented:
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
0
 
r_naren22atyahooAuthor Commented:
hi Rory,

I have asked a similar question here,
Any help would be appreciated
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21850138.html

regards
Naren
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now