You can use the ApplyFilter action to apply a filter, a query, or a SQL WHERE clause to a table, form, or report to restrict or sort the records in the table, or the records from the underlying table or query of the form or report. For reports, you can use this action only in a macro specified by the report's OnOpen event property.
Note You can only use this action to apply a SQL WHERE clause when applying a server filter. A server filter cannot be applied to a stored procedure record source.
Setting
The ApplyFilter action has the following arguments.
Action argument | Description |
---|---|
Filter Name | The name of a filter or query that restricts or sorts the records of the table, form, or report. You can enter the name of either an existing query or a filter that has been saved as a query in the Filter Name box in the Action Arguments section of the Macro window. |
Note When using this action to apply a server filter, the Filter Name argument must be blank. | |
Where Condition | A valid SQL WHERE clause (without the word WHERE) or an expression that restricts the records of the table, form or report. |
Note In a Where Condition argument expression, the left side of the expression typically contains a field name from the underlying table or query for the form or report. The right side of the expression typically contains the criteria you want to apply to this field in order to restrict or sort the records. For example, the criteria can be the name of a control on another form that contains the value you want the records in the first form to match. The name of the control should be fully qualified, for example:
Forms!formname!controlname Field names should be surrounded by double quotes and string literals should be surrounded by single quotes. |
|
The maximum length of the Where Condition argument is 256 characters. If you need to enter a longer SQL WHERE clause, use the ApplyFilter method of the DoCmd object in Visual Basic. You can enter SQL WHERE clause statements of up to 32,768 characters in Visual Basic. | |
Filter Type | Specifies whether the search includes formatted data. Click Normal (Microsoft Access searches for the data as it's formatted and displayed in the field) or Server (Microsoft Access searches for the data as it's stored in the database, which isn't always the same as it's displayed). The default is Normal. |
Note You can use the Filter Name argument if you've already defined a filter that provides the appropriate data. You can use the Where Condition argument to enter the restriction criteria directly. If you use both arguments, Microsoft Access applies the WHERE clause to the results of the filter. You must use one or both arguments.
Remarks
You can apply a filter or query to a form in Form view or Datasheet view.
The filter and WHERE condition you apply become the setting of the form's or report's Filter or ServerFilter property.
For tables and forms, this action is similar to clicking Apply Filter/Sort or Apply Server Filter on the Records menu or clicking Apply Filter or Apply Server Filter on the toolbar. The menu command or button applies the most recently created filter to the table or form, whereas the ApplyFilter action applies a specified filter or query.
In a Microsoft Access database (.mdb), if you point to Filter on the Records menu and then click Advanced Filter/Sort after running the ApplyFilter action, the Advanced Filter/Sort window shows the filter criteria you've selected with this action.
To remove a filter and display all of the records for a table or form, in an Access database, you can use the ShowAllRecords action, the Remove Filter/Sort command on the Records menu, or Remove Filter on the toolbar. To remove a filter in a Microsoft Access project (.adp), you can return to the Server Filter By Form window and remove all filter criteria and then click Apply Server Filter on the Records menu, or click Apply Server Filter on the toolbar, or set the ServerFilterByForm property to False (0).
When you save a table or form, Microsoft Access saves any filter currently defined in that object, but won't apply the filter automatically the next time the object is opened (although it will automatically apply any sort you applied to the object before it was saved). If you want to apply a filter automatically when a form is first opened, specify a macro containing the ApplyFilter action or an event procedure containing the ApplyFilter method of the DoCmd object as the OnOpen event property setting of the form. You can also apply a filter by using the OpenForm or OpenReport action, or their corresponding methods. To apply a filter automatically when a table is first opened, you can open the table by using a macro containing the OpenTable action, followed immediately by the ApplyFilter action.