Filter Event -- Event Procedures

Description

To create an event procedure that is executed when the Filter event occurs, set the OnFilter property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_Filter(Cancel As Integer, FilterType As Integer)

The Filter event procedure uses the following arguments.

Argument

Description

Cancel

The setting determining whether to open the filter window. Setting Cancel to True (-1) prevents the filter window from opening. You can also use the CancelEvent method of the DoCmd object to cancel opening the filter window.

FilterType

The filter window the user is trying to open. The FilterType argument can be one of the following intrinsic constants:

Constant Value

acFilterByForm 0

acFilterAdvanced 1


Remarks

You can use the Filter event procedure to disable or hide certain controls in the Filter By Form window, preventing them from being used in filter criteria. For example, you may always want your Orders form to show records that have a value in the TotalDue control, no matter how small. You can disable or hide this control in the Filter event procedure, then enable or show it in the ApplyFilter event procedure. Disabling and hiding controls in the Filter By Form window is best accomplished in the Filter event procedure rather than the Filter macro, because you can check the FilterType argument to see if the user opened the Filter By Form window. All of the fields in a form appear in the Advanced Filter/Sort window, even if the controls are disabled or hidden.

See Also

Filter Event — Macros.

Example

The following example shows how to disable the TotalDue control on an Orders form when the user tries to create a filter, so that the user can’t filter on this field. Any records that have a TotalDue value and meet the other filter criteria will always be shown on the filtered form. This example also forces the user to use the Filter By Form window and not the Advanced Filter/Sort window.


Private Sub Form_Filter(Cancel As Integer, FilterType As Integer)
    If FilterType = acFilterByForm Then
        Forms!Orders!TotalDue.Enabled = False
    ElseIf FilterType = acFilterAdvanced Then
        MsgBox "The best way to filter this form is to use the " & _
            "Filter By Form command or toolbar button.", vbOKOnly + _            vbInformation
        Cancel = True
    End IfSub