Directory Services |
The following code example contains a subroutine that uses ranging with ActiveX Directory Objects (ADO) to retrieve the members of a group.
Private Sub EnumGroupWithADO(strGroupDN As String, strUsername As String, strPassword As String) Set oConn = CreateObject("ADODB.Connection") Set oComm = CreateObject("ADODB.Command") oConn.Provider = "ADsDSOObject" oConn.Properties("ADSI Flag") = 1 If strUsername <> "" Then oConn.Properties("User ID") = strUsername oConn.Properties("Password") = strPassword End If oConn.Open oComm.ActiveConnection = oConn ' For compatibility with all operating systems, the number of objects ' retrieved by each query should not exceed 999. rangeStep = 999 lastLoop = False lowRange = 0 highRange = lowRange + rangeStep commandPrefix = "<LDAP://" & strGroupDN & ">;(objectClass=*);member;range=" commandSuffix = ";base" Do If lastLoop Then ' Perform this query with the "range=<lowRange>-*" range. oComm.CommandText = commandPrefix & lowRange & "-*" & commandSuffix Else ' Perform this query with the "range=<lowRange>-<highRange>" range. oComm.CommandText = commandPrefix & lowRange & "-" & highRange & commandSuffix End If Debug.Print "Current search command: " & oComm.CommandText ' Execute the query. Set oRS = oComm.Execute ' Reset the retrieved members counter. nRetrieved = 0 ' Enumerate the retrieved members. While Not oRS.EOF For Each oField In oRS.Fields If VarType(oField) = (vbArray + vbVariant) Then For Each oValue In oField.Value Debug.Print vbTab & oValue nRetrieved = nRetrieved + 1 Next End If Next oRS.MoveNext Wend ' If the last query was just performed, exit the loop. If lastLoop = True Then Exit Do End If If nRetrieved = 0 Then ' No objects were retrieved by the last query, so perform one last query ' with the "range=<lowRange>-*" range. lastLoop = True Else ' Increment the high and low ranges to query for the next block of objects. lowRange = highRange + 1 highRange = lowRange + rangeStep End If Loop While True End Sub