Directory Services

LDAP Dialect

The LDAP dialect is a format for query statements that use the LDAP search filter syntax. Use an LDAP query statement with the following ADSI search interfaces:

An LDAP dialect string consists of four parts separated by semicolons (;).

The following is a code example of the LDAP dialect in ADSI that searches all the objects in the subtree.

"<LDAP://DC=Fabrikam,DC=com>;(objectClass=*);AdsPath, cn;subTree"

Not all search options (search page size, for example) can be expressed in the LDAP dialect, so you must set the options before the actual query execution starts.

Example Code for Performing an LDAP Query

Dim con As New Connection, rs As New Recordset
Dim v
Dim Com As New Command
 
On Error GoTo Cleanup
 
' Open a Connection object.
con.Provider = "ADsDSOObject"
Debug.Print con.Properties.Count
For j = 0 To con.Properties.Count - 1
	Debug.Print con.Properties(j).Name
Next j
 
con.Open "Active Directory Provider", "CN=administrator,CN=users,DC=MyDomain,DC=Fabrikam,DC=com", ""
 
' Create a command object on this connection.
Set Com.ActiveConnection = con
 
' Set the query string.
Com.CommandText = "<LDAP://MyServer/DC=MyDomain,DC=Fabrikam,DC=com>;(objectClass=*);ADsPath, objectclass;base"
 
For j = 0 To Com.Properties.Count - 1
	Debug.Print Com.Properties(j).Name
Next j
 
' Set search preferences.
Com.Properties("Page Size") = 1000
Com.Properties("Timeout") = 30 'seconds
 
' Execute the query.
Set rs = Com.Execute
 
For i = 0 To rs.Fields.Count - 1
	Debug.Print rs.Fields(i).Name, rs.Fields(i).Type
Next i
 
rs.MoveLast
Debug.Print "No. of rows = ", rs.RecordCount
 
' Navigate the record set.
rs.MoveFirst
While Not rs.EOF
	For i = 0 To rs.Fields.Count - 1
		If rs.Fields(i).Type = adVariant And Not (IsNull(rs.Fields(i).Value)) Then
			Debug.Print rs.Fields(i).Name, " = "
			For j = LBound(rs.Fields(i).Value) To UBound(rs.Fields(i).Value)
				Debug.Print rs.Fields(i).Value(j), " # "
			Next j
		Else
			Debug.Print rs.Fields(i).Name, " = ", rs.Fields(i).Value
		End If
	Next i
	rs.MoveNext
Wend
 
rs.MoveLast
Debug.Print "No. of rows = ", rs.RecordCount
 
Exit Sub
 
Cleanup:
Debug.Print "Error", Hex(Err.Number), " :", Error(Err.Number)
Exit Sub