ApplyFilter Event — Event Procedures

Description

To create an event procedure that runs when the ApplyFilter event occurs, set the OnApplyFilter property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)

The ApplyFilter event procedure has the following arguments.

Argument

Description

Cancel

The setting determines whether to apply the filter. Setting the Cancel argument to True (–1) prevents the filter from being applied. You can also use the CancelEvent method of the DoCmd object to cancel applying the filter.

ApplyType

The action you took that caused the ApplyFilter event to occur. The ApplyType argument can be one of the following intrinsic constants:

Constant

Value

acShowAllRecords

0

acApplyFilter

1

acCloseFilterWindow

2


Remarks

You can use the ApplyFilter event procedure to show or hide, enable or disable certain controls on the form when the filter is applied or removed. For example, when a filter is applied to an Orders form to show only orders that have been paid for, you can hide the AmountDue, Tax, and TotalDue fields when the ApplyType argument is acApplyFilter, and show them again when the ApplyType argument is acShowAllRecords.

You can also use the ApplyFilter event procedure to undo actions you took when the Filter event occurred. This is especially useful if the user closed the filter window without creating a filter when the ApplyType argument is acCloseFilterWindow.

See Also

ApplyFilter event — macros.

Example

The following example shows how to hide the AmountDue, Tax, and TotalDue controls on an Orders form when the applied filter restricts the records to only those orders that have been paid for.

To try this example, add the following event procedure to an Orders form that contains AmountDue, Tax, and TotalDue controls. Run a filter that lists only those orders that have been paid for.

Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
    If Not IsNull(Me.Filter) And (InStr(Me.Filter, "Orders.Paid = -1")>0 _
            Or InStr(Me.Filter, "Orders.Paid = True")>0)Then
        If ApplyType = acApplyFilter Then
            Forms!Orders!AmountDue.Visible = False
            Forms!Orders!Tax.Visible = False
            Forms!Orders!TotalDue.Visible = False
        ElseIf ApplyType = acShowAllRecords Then
            Forms!Orders!AmountDue.Visible = True
            Forms!Orders!Tax.Visible = True
            Forms!Orders!TotalDue.Visible = True
        End If
    End If
End Sub