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

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?

djdidgeAuthor Commented:
yes, the  rs.RecordCount always = 1000  

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

No ideas at the moment then.  Researching.

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Ok, take a look at this:

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

Anthony PerkinsCommented:
By default Active Directory limits search results to a maximum of 1000 records.  You will have to page through all the records.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
djdidgeAuthor Commented:
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


No problem, he deserved it.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.