HandsOn Writing the Form ApplyFilter Event Procedure

This hands-on uses the Product Categories form created in Hands-On 24-6.

1. In the Visual Basic Editor window, open the Product Categories form.

2. The Code window shows other event procedures already written for this form. Enter the following Form_ApplyFilter event procedure below the last procedure code.

Private Sub Form_ApplyFilter(Cancel As Integer, _ ApplyType As Integer) Dim Response As Integer If ApplyType = acApplyFilter Then If Me.Filter = "" Then

MsgBox "You did not select any criteria.", _

vbOKOnly + vbCritical, "No Selection" GoTo ExitHere End If

Response = MsgBox("The selected criteria " & _

Part IV

"is as follows:" & vbCrLf & _ Me.Filter, vbOKCancel + vbQuestion, _ "Filter Criteria")

End If

If Response = vbCancel Then Cancel = True

End If

If ApplyType = acShowAllRecords Then Me.Filter = ""

MsgBox "Filter was removed."

End If

If ApplyType = acCloseFilterWindow Then

Response = MsgBox("Are you sure you " & _

"want to close the Filter window?", vbYesNo) If Response = vbNo Then

Cancel = True End If

End If ExitHere:

With Me.CategoryID .Enabled = True .SetFocus

End With End Sub

3. To test the above event procedure, open the Product Categories form in Form view.

4. Choose Records | Filter | Filter by Form (or click the Filter by Form button on the toolbar). The Form_Filter event will be triggered (see Hands-On 24-18). Click OK to the message box.

5. Select a category from the Category Name combo box and click the Apply Filter button on the toolbar. This action will trigger the Form_ApplyFilter event procedure. Experiment with the form filter, testing other situations such as clicking the Apply Filter button when the filtering criteria were not specified or closing the Filter by Form window.

6. To test the condition that removes the filter, choose the filtering criteria and press OK to limit the records. Next, click the Remove Filter button. Notice that the name of this button changes to Remove Filter or Apply Filter depending on the situation.

7. Close the Product Categories form.

0 0

Post a comment