Share via

How to write code for filter button? (MS ACCESS 2010)

Anonymous
2010-11-20T10:45:02+00:00

Is it possible to create a filter button on a form that shows record for the [datanumber] from 1 to 20? I tried searching for one but no luck. I don't know if using a macro or writing an event procedure is the right way to do it. Can someone give me an example of how I should write the code? I want to filter by [datanumber] by clicking a button on the form. Thanks

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-11-27T03:30:19+00:00

Then you will need to reference the subform from the main form (UNTESTED)....

If [NameOfYourSubform].Form.FilterOn Then

    [NameOfYourSubform].Form.FilterOn = False

Else

    [NameOfYourSubform].Form.Filter = "datanumber <=20"

    [NameOfYourSubform].Form.FilterOn = True

End If


--

Gina Whipp

2010 Microsoft MVP (Access)

Please post all replies to the forum where everyone can benefit.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2010-11-20T12:37:47+00:00

The following in the button's Click event procedure will toggle the form between filtered for the datanumber being 20 or less, and unfiltered to show all records:

If Me.FilterOn Then

    Me.FilterOn = False

Else

    Me.Filter = "datanumber  <=20"

    Me.FilterOn = True

End If

For a more flexible approach you could add two unbound text box's to the form, cboFrom and cboTo, each with a RowSource property of:

SELECT [datanumber] FROM [YourTableName] ORDER BY [datanumber];

The buttons code would then be:

If Me.FilterOn Then

    Me.FilterOn = False

Else

    Me.Filter = "datanumber  >= " & Me.cboFrom & _

        " And datanumber  <= " & Me.cboTo

    Me.FilterOn = True

End If

This would enable you to select the range from the two combo boxes and then click the button to filter the form.


Ken Sheridan, Stafford, England

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-12-07T17:54:18+00:00

    Note that in Gina's code NameOfYourSubform is the name of the subform control in the parent form. i.e. the control which houses the subform, not the name of its underlying form object, unless both have the same name of course.


    Ken Sheridan, Stafford, England

    Thanks, it works perfectly.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-11-27T17:19:38+00:00

    Note that in Gina's code NameOfYourSubform is the name of the subform control in the parent form. i.e. the control which houses the subform, not the name of its underlying form object, unless both have the same name of course.


    Ken Sheridan, Stafford, England

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-11-23T01:17:02+00:00

    The following in the button's Click event procedure will toggle the form between filtered for the datanumber being 20 or less, and unfiltered to show all records:

    If Me.FilterOn Then

        Me.FilterOn = False

    Else

        Me.Filter = "datanumber  <=20"

        Me.FilterOn = True

    End If

    For a more flexible approach you could add two unbound text box's to the form, cboFrom and cboTo, each with a RowSource property of:

    SELECT [datanumber] FROM [YourTableName] ORDER BY [datanumber];

    The buttons code would then be:

    If Me.FilterOn Then

        Me.FilterOn = False

    Else

        Me.Filter = "datanumber  >= " & Me.cboFrom & _

            " And datanumber  <= " & Me.cboTo

        Me.FilterOn = True

    End If

    This would enable you to select the range from the two combo boxes and then click the button to filter the form.


    Ken Sheridan, Stafford, England

    Actually, I'm trying to put a button on the mainform that filters what is shown on the subform. Is this still the same code? I can't get it to work. It always ask me to specify what datanumber is. Thanks.

    Was this answer helpful?

    0 comments No comments