Using ADO for Querying

The following example shows how Automation clients can use ActiveX Data Objects (ADO) to query directory services using ADSI and its OLE DB implementation. The ADO object model consists of the following objects:

Connection
An open connection to an OLE DB data source such as ADSI.
Command
Defines a specific command to execute against the data source.
Parameters
An optional collection for any parameters to provide to the command object.
Recordset
A set of records from a table, command object, or SQL syntax. A recordset can be created without any underlying Connection object.
Field
A single column of data in a recordset.
Property
A collection of values supplied by the provider for ADO.
Error
Contains details about data access errors, refreshed for each time an error occurs in a single operation.

For more information, see the Microsoft Platform SDK under ActiveX Data Objects.

The following example disables the user accounts on an ldap server.

Dim X as IADs
Dim con As New Connection, rs As New Recordset
Dim MyUser As IADsUser
    
con.Provider = "ADsDSOObject"
con.Open "Active Directory Provider", "CN=Foobar,CN=Users,DC=MICROSOFT,DC=COM,O=INTERNET", "Password"
Set rs = con.Execute("<LDAP://ldapserver.microsoft.com/O=Internet/DC=COM/DC=MICROSOFT/OU=MyOrg>;(objectClass=User);ADsPath;onelevel")
    
While Not rs.EOF
    MyUser = GetObject(rs.Fields(0).Value)
    MyUser.AccountDisabled = True
    rs.MoveNext
Wend