Mike
Mike

Reputation: 1011

Is it possible to filter a control that refers to a subform?

This is the code I am referring to:

Dim ctl as Control
For Each ctl In Me.Form
  If ctl.ControlType = acSubform
    ctl.Filter = "[StartDate] <= [Date] and [EndDate] >= [Date]"
    ctl.FilterOn = True
  End If
Next ctl

Apparently this doesn't work (and it has nothing to do with the filter string being invalid). I am guessing that I cannot use the Filter method on a control object. But is there a way to get around this? Maybe create a SubForm variable and somehow assign it to the object the Control is referring to? Help! Thanks!

Upvotes: 0

Views: 56

Answers (1)

Gustav
Gustav

Reputation: 55816

It doesn't make much sense. You neither can nor would apply a filter on all controls.

This is what you may have in mind:

With Me!NameOfYourSubformControl.Form
    .Filter = "[StartDate] <= [Date] and [EndDate] >= [Date]"
    .FilterOn = True
End With

Then it would be:

Dim ctl as Control
For Each ctl In Me.Form
  If ctl.ControlType = acSubform
    ctl.Form.Filter = "[StartDate] <= [Date] and [EndDate] >= [Date]"
    ctl.Form.FilterOn = True
  End If
Next ctl

Upvotes: 2

Related Questions