HandsOn Writing the Form Filter 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_Filter event procedure below the last procedure code.

Private Sub Form_Filter(Cancel As Integer, _ FilterType As Integer) Select Case FilterType Case acFilterByForm

MsgBox "You selected to filter records " & _ "by form.", vbOKOnly + vbInformation, _ "Filter By Form" Me.CategoryName.SetFocus Me.CategoryID.Enabled = False Case acFilterAdvanced

MsgBox "You are not authorized to use " & _ " Advanced Filter/Sort.", _ vbOKOnly + vbInformation, _ "Advanced Filter By Form" Cancel = True End Select 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).

5. The code in the Form_Filter event procedure runs and you will see a message box. Click OK. The Filter by Form dialog box appears with the Category ID text box disabled. You can disable certain controls on the form if you don't want the user to filter by them.

Event Programming in Forms and Reports

6. Filter the form to display only records for Seafood and Meat/Poultry.

7. Remove the filter by clicking the Remove Filter button (§y§ ) on the toolbar or by choosing Records | Remove Filter/Sort.

8. Choose Records | Filter | Advanced Filter/Sort. You will not be able to use the advanced filter for this form because the form's Filter event has disabled this action.

9. Close the Product Categories form.

0 0

Post a comment