Sun Chili!Soft ASP Sun Chili!Soft
ASP Sun Microsystems


ADO Recordset Object Filter Property

A filter for data in a recordset.

Filter Property Return Values

Sets or returns a Variant value, which can contain one of the following:

Criteria string

A string made up of one or more individual clauses concatenated with AND or OR operators.

Array of bookmarks

An array of unique bookmark values that point to records in the Recordset object. This return value is not currently supported on UNIX.

One of the following FilterGroupEnum values:




Removes the current filter and restores all records to view.


Enables you to view only records that have changed but have not yet been sent to the server. Only applicable for batch update mode. Not currently supported on UNIX.


Enables you to view only records affected by the last Delete, Resync, UpdateBatch, or CancelBatch call. Not currently supported on UNIX.


Enables you to view records in the current cache, that is, the results of the last call to retrieve records from the database. Not currently supported on UNIX.

Filter Property Remarks

Use the Filter property to selectively screen out records in a Recordset object. The filtered recordset becomes the current cursor. This affects other properties such as AbsolutePosition, AbsolutePage, RecordCount, and ADO Recordset Object PageCount Property that return values based on the current cursor, since setting the Filter property to a specific value will move the current record to the first record that satisfies the new value.

On UNIX systems the Filter property is implemented for Recordset objects whose source is a SELECT query. Setting the Filter property will resubmit the query with the criteria string AND'd with the WHERE clause.

The criteria string is made up of clauses in the form FieldName-Operator-Value (for example, "LastName = 'Smith'"). You can create compound clauses by concatenating individual clauses with AND (for example, "LastName = 'Smith' AND FirstName = 'John'") or OR (for example, "LastName = 'Smith' OR LastName = 'Jones'"). Use the following guidelines for criteria strings:


Must be a valid field name from the recordset. If the field name contains spaces, you must enclose the name in square brackets.


Must be one of the following: <, >, <=, >=, <>, =, LIKE.


The value with which you will compare the field values (for example, 'Smith', #8/24/95#, 12.345 or $50.00). Use single quotes with strings and pound signs (#) with dates. For numbers, you can use decimal points, dollar signs, and scientific notation. If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wildcards are allowed, and they must be the last character in the string. Value may not be Null.

There is no precedence between AND and OR. Clauses can be grouped within parentheses. However, you cannot group clauses joined by an OR and then join the group to another clause with an AND, like this:

(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'

Instead, you would construct this filter as:

(LastName = 'Smith' AND FirstName = 'John') OR

(LastName = 'Jones' AND FirstName = 'John')

In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'), or only at the end of the pattern (for example, LastName Like 'Smit*').

The filter constants make it easier to resolve individual record conflicts during batch update mode by allowing you to view, for example, only those records that were affected during the last ADO Recordset Object UpdateBatch Method call.

Setting the Filter property itself may fail because of a conflict with the underlying data (for example, a record has already been deleted by another user); in such a case, the provider returns warnings to the ADO Errors Collection but does not halt program execution. A run-time error occurs only if there are conflicts on all the requested records. Use the ADO Recordset Object Status Property to locate records with conflicts.

Setting the Filter property to a zero-length string ("") has the same effect as using the adFilterNone constant.

Whenever the Filter property is set, the current record position moves to the first record in the filtered subset of records in the recordset. Similarly, when the Filter property is cleared, the current record position moves to the first record in the recordset.

See the ADO Recordset Object Bookmark Property for an explanation of bookmark values from which you can build an array to use with the Filter property.

Filter Property Example

This Visual Basic example uses the Filter property to open a new recordset based on a specified condition applied to an existing recordset. It uses the RecordCount property to show the number of records in the two recordsets. The FilterField function is required for this procedure to run.

Public Sub FilterX()

Dim rstPublishers As ADODB.Recordset

Dim rstPublishersCountry As ADODB.Recordset

Dim strCnn As String

Dim intPublisherCount As Integer

Dim strCountry As String

Dim strMessage As String

` Open recordset with data from Publishers table.

strCnn = "driver={SQL Server};server=srv;" & _


Set rstPublishers = New ADODB.Recordset

rstPublishers.CursorType = adOpenStatic

rstPublishers.Open "publishers", strCnn, , , adCmdTable

` Populate the Recordset.

intPublisherCount = rstPublishers.RecordCount

` Get user input.

strCountry = Trim(InputBox( _

"Enter a country to filter on:"))

If strCountry <> "" Then

` Open a filtered Recordset object.

Set rstPublishersCountry = _

FilterField(rstPublishers, "Country", strCountry)

If rstPublishersCountry.RecordCount = 0 Then

MsgBox "No publishers from that country."


` Print number of records for the original

` Recordset object and the filtered Recordset

` object.

strMessage = "Orders in original recordset: " & _

vbCr & intPublisherCount & vbCr & _

"Orders in filtered recordset (Country = '" & _

strCountry & "'): " & vbCr & _


MsgBox strMessage

End If


End If

End Sub

Public Function FilterField(rstTemp As ADODB.Recordset, _

strField As String, strFilter As String) As ADODB.Recordset

` Set a filter on the specified Recordset object and then

` open a new Recordset object.

rstTemp.Filter = strField & " = '" & strFilter & "'"

Set FilterField = rstTemp

End Function


When you know the data you want to select, it's usually more efficient to open a recordset with an SQL statement. This example shows how you can create just one recordset and obtain records from a particular country.

Public Sub FilterX2()

Dim rstPublishers As ADODB.Recordset

Dim strCnn As String

` Open recordset with data from Publishers table.

strCnn = "driver={SQL Server};server=srv;" & _


Set rstPublishers = New ADODB.Recordset

rstPublishers.CursorType = adOpenStatic

rstPublishers.Open "SELECT * FROM publishers " & _

"WHERE Country = 'USA'", strCnn, , , adCmdText


` Print current data in recordset.


Do While Not rstPublishers.EOF

Debug.Print rstPublishers!pub_name & ", " & _





End Sub

Copyright 2002 Sun Microsystems, Inc. All rights reserved. Legal Notice.