Filter Event

Applies To

Form.

Description

The Filter event occurs when the user does one of the following:

  • On the Records menu in Form view, points to Filter and then clicks Filter By Form, or clicks the Filter By Form button on the toolbar. This opens the Filter By Form window, where you can quickly create a filter based on the fields in the form.
  • On the Records menu in Form view, points to Filter and then clicks Advanced Filter/Sort. This opens the Advanced Filter/Sort window, where you can create complex filters for the form.
  • Clicks Advanced Filter/Sort on the Filter menu while the Filter By Form window is open, or clicks Filter By Form on the Filter menu while the Advanced Filter/Sort window is open. This causes the ApplyFilter event to occur when the open filter window is closed, and then the Filter event to occur when the other filter window is opened.
Remarks

To run a macro or event procedure when this event occurs, set the OnFilter property to the name of the macro or to [Event Procedure].

You can use the Filter event to:

  • Remove any previous filter for the form. To do this, set the Filter property of the form to a zero-length string (" ") in the Filter macro or event procedure. This is especially useful if you want to make sure extraneous criteria don't appear in the new filter. For example, whenever you use the Filter By Selection feature, the criteria you use (the selected text in the form) is added to the Filter property
  • You can't use the GetOption method or the SetOption method to read or set any of the options found on the Module tab of the Options dialog box.
  • If the return value of the GetOption method is assigned to a variable, the variable must be declared as a Variant.
  • If your database may run on a version of Microsoft Access for a language other than the one in which you created it, then you must supply the arguments for the GetOption and SetOption methods in English.
When you quit Microsoft Access, you can reset all options to their original settings by using the SetOption method on all changed options. You may want to create public variables to store the values of the original settings. You might include code to reset options in the Close event procedure for a form, or in a custom exit procedure that the user must run to quit the application.

Example

The following example uses the GetOption method to return the Default Field Type option setting from the Tables/Queries tab of the Options dialog box and then saves the setting in a variable. If the Default Field Type option isn't currently set to Text, Microsoft Access displays a dialog box asking the user whether it should set the option to Text. If the user clicks Yes, the SetOption method changes the option.

Sub ChangeFieldSize()
    ' Constant represents Text setting of Default Field Type option.
    Const conText = 0

    Dim varSize As Variant, intResponse As Integer
    Dim strMsg As String

    ' Determine current setting.
    varSize = Application.GetOption("Default Field Type")
    strMsg = "Set default field type to Text?"
    If varSize <> conText Then
    ' Prompt user to change setting if it's not Text.
        If MsgBox(strMsg, vbYesNo) = vbYes Then
            ' Change setting to Text.
            Application.SetOption "Default Field Type", conText
        End If
    End If
End Sub