通过按钮应用过滤器以匹配记录

Apply a filter via button to match records

我有一个包含两个子表单的表单(总共 3 个表)。

表格: PI(患者信息)
推荐人
报告

表格在顶部显示 PI,然后将推荐和报告显示为子表格(见下面的屏幕截图)

在当前视图中,表格显示患者 #1 的所有转介(1 个转介可用)和所有报告,无论它是否与转介相关。我想创建一个按钮,以便在单击时应用过滤器以仅显示与当前显示的 ReferralID 匹配的报告。

PI Form 上的 SubForm 控件有 LinkChildFields 属性 和 LinkMasterFields 属性.

编辑: 您可以将报告表单作为推荐子表单中的子表单。我做过几次,效果很好。它可能会限制你在表单设计上的一点点。

MSDN Article on SubForm.LinkChildFields Property:

You can use the LinkChildFields property (along with the LinkMasterFields property) together to specify how Microsoft Access links records in a form... to records in a subform... If these properties are set, Microsoft Access automatically updates the
related record in the subform when you change to a new record in a main form.

请参阅this page了解如何引用子窗体和父窗体上的控件和属性。至于应用过滤器的按钮,这应该是一个切换按钮,点击应用过滤器,或向上删除它。

在您的情况下,将名为 tbtnFilter 的切换按钮添加到 Referrals 子表单,并在表单的 OnCurrent 事件中修改 Reports 过滤器:

Me.Parent!Reports.Form.Filter = "ReferralsID = " & CStr(Me.ReferralsID)
Me.Parent!Reports.Form.FilterOn = Me.tbtnFilter

tbtnFilter OnClick Event apply and remove the filter, and modify the ToggleButton.Caption Property 为 "Apply Filter" 向上时 (FALSE),而 "Remove Filter" 向下时 (TRUE):

Me.Parent!Reports.Form.FilterOn = Me.tbtnFilter
Me.tbtnFilter.Caption = IIf(Me.tbtnFilter, "Remove", "Apply") & " Filter"