Solved

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

Posted on 2003-11-20
7
1,115 Views
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
'function

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

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

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://" & "nwhc.ac.uk")
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
rs.MoveLast
rs.MoveFirst
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"
          Else
            .SubItems(1) = rs("location") & ""
          End If
       End With

       .MoveNext
    Loop
End With
MsgBox ListView1.ListItems.Count
AllComputers = sAns

errhandler:

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
0
Comment
Question by:djdidge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:leonstryker
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?

Leon
0
 
LVL 2

Author Comment

by:djdidge
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.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9796918
djdidge,

No ideas at the moment then.  Researching.

Leon
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 29

Expert Comment

by:leonstryker
ID: 9799979
Ok, take a look at this:

http://www.w3schools.com/ado/prop_rs_maxrecords.asp

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

Leon
0
 
LVL 75

Accepted Solution

by:
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.

Anthony
0
 
LVL 2

Author Comment

by:djdidge
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

DiDGE
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9838114
DiDGE,

No problem, he deserved it.

Leon
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

734 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