To create an event procedure that is executed when the ApplyFilter event occurs, set the OnApplyFilter property to [Event Procedure], and click the Build button.
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
The ApplyFilter event procedure uses the following arguments.
Argument | Description |
Cancel | The setting determining whether to apply the filter. Setting Cancel 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 acShowAllRecords |
Value 0 |
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 (the ApplyType argument is acCloseFilterWindow).
ApplyFilter Event Macros.
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.
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 IfSub