ADO -> LDAP .... to get active directory computer list

Posted on 2003-11-20
Last Modified: 2013-12-25
Hi all,

I found this code to get a list of machine names and 'locations' from our active directory... it seems to work fine, however!...
The recordset is always limited to 1000 records... i can see now way to retrieve it all, so i must assume its a limitation of perhaps the provider?...

Public Function AllComputers() As String()

'PURPOSE:  Gets all Computers for the current domain
'and returns them in a string array, using LDAP

'Requires: ADSI, LDAP provider
'This function tested on Windows 2000 RC2

'RETURNS: String array containing all
'Computers for the current domain

'Requires VB6 because in lower versions
'array cannot be return type for a

'Dim sArray() As String
'Dim iCtr As Integer

'sArray = AllComputers
'For iCtr = 0 To UBound(sArray)
'    Debug.Print sArray(iCtr)

Dim conn As New ADODB.Connection
Dim rs As 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 sAns() As String
Dim iElement As Integer

On Error GoTo errhandler:

Set oRoot = GetObject("LDAP://rootDSE")
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & "")
sBase = "<" & oDomain.ADsPath & ">"
sFilter = "(&(objectCategory=*))"
sAttribs = "name, location"
sDepth = "subTree"

sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
             MsgBox sQuery
conn.Open _
  "Data Source=Active Directory Provider;Provider=ADsDSOObject"
Set rs = conn.Execute(sQuery)
ReDim sAns(0) As String

With rs
MsgBox rs.RecordCount
    Do While Not .EOF
      On Error GoTo 0
'        MsgBox rs("name") & " " & rs("location")
       With ListView1.ListItems.Add(, , rs("name"))
          If rs("location") & "" = "" Then
            .SubItems(1) = "No Location"
            .SubItems(1) = rs("location") & ""
          End If
       End With

End With
MsgBox ListView1.ListItems.Count
AllComputers = sAns


On Error Resume Next
If rs.State <> 0 Then rs.Close
If conn.State <> 0 Then conn.Close
Set rs = Nothing
Set conn = Nothing
Set oRoot = Nothing
Set oDomain = Nothing

End Function
Question by:djdidge
  • 4
  • 2
LVL 29

Expert Comment

ID: 9789636
What is the rs.RecordCount equals to?  Is it always 1000? or is it different but you only get 100 items in the list view?


Author Comment

ID: 9796102
yes, the  rs.RecordCount always = 1000  

it first i thought it was just a bizzare cooincidence... but i later learnt that it isnt correct.
LVL 29

Expert Comment

ID: 9796918

No ideas at the moment then.  Researching.

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

LVL 29

Expert Comment

ID: 9799979
Ok, take a look at this:

I think what you are running into is a default setting on teh MaxRecords property of the recordset.

LVL 75

Accepted Solution

Anthony Perkins earned 170 total points
ID: 9801418
By default Active Directory limits search results to a maximum of 1000 records.  You will have to page through all the records.


Author Comment

ID: 9836532
Leon, thanks for your help.
I must however award acperkins the points. Because the black and white fact you explained helped me resolve my issue..

Thx both

LVL 29

Expert Comment

ID: 9838114

No problem, he deserved it.


Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now