You can use the Clone
method to create a duplicate recordset, then use Filter
to reduce the dataset to what you're interested in. For example:
Dim rs, rs2
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly
Set rs2 = rs.Clone()
rs2.Filter = "Field1 = 'foo'"
The string form of Filter
is basic; it's pretty much <Field> <op> <value>
. You can combine multiple expressions using AND
and OR
, but even that has some limitations (see the documentation link for the full details).
For more complex filtering, you can pass the Filter
property an array of Bookmark
objects. In this case, you loop through the recordset (or a clone of the recordset), testing each record by whatever complex criteria you have. If the record passes the test, you save its Bookmark
to an array or other collection. Then, you can set the Filter
property to your array of Bookmarks and you have a custom-filtered recordset.
'Note that I haven't tested this code
Dim rs, rs2, bookmarks
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open queryString, AuthConn, adOpenKeyset, adLockReadOnly
Set rs2 = rs.Clone()
bookmarks = Array()
Do Until rs2.EOF
If rs2("Field1") = 2 * rs2("Field2") Then
ReDim Preserve bookmarks(UBound(bookmarks) + 1)
bookmarks(UBound(bookmarks)) = rs2.Bookmark
End If
rs2.MoveNext
Loop
rs2.Filter = bookmarks
' Now rs2 contains only records where Field1 = 2*Field2
You can use this same technique to get unique values (aka DISTINCT) by using a Dictionary
object to store the unique key values. Doing a DISTINCT on multiple fields is a bit trickier. What I've done is the past is to combine the multiple fields using a separator that won't be in the data (such as a pipe |
). That's not always possible, though.