可以在子表单数据加载之前在父表单的子表单上设置过滤器 [英] Possible to set filter on subform from parent form before subform data loads
问题描述
我有frmParentForm,其中包含多个控件,这些控件用于为frmSubForm构建过滤器.
I have frmParentForm with multiple controls used to build a filter for frmSubForm.
在frmParentForm_Load上,我正在做(简化示例):
On frmParentForm_Load, I am doing (simplified example):
Me.sbfInvoice_List.Form.filter = "[created_on] >= #" & Me.RecentOrderDateCutoff & "#"
Me.sbfInvoice_List.Form.FilterOn = True
问题是,在初始加载时,似乎首先发生了子窗体加载,因此整个表都已加载.
The problem is, on initial load, it seems the subform load is occurring first, so the entire table is loaded.
是否有一种方法(可能在其他情况下)可以从父表单中正确设置子表单过滤器 ,以便在子表单加载其初始数据之前对其应用? (子窗体可以单独存在,也可以作为许多不同父窗体的子窗体(有时被过滤,有时不存在),因此我不希望在子窗体本身中使用一些复杂的技巧来实现这一目的.)
Is there a way (in a different event perhaps) to properly set the subform filter from the parent form so it is applied before the subform does its initial data load? (The subform can exist on its own, or as a child of many different parent forms (sometimes filtered, sometimes not), so I'd rather not put some complicated hack in the subform itself to accomplish this.)
推荐答案
由于子窗体在父窗体之前加载,因此父窗体无法在子窗体最初加载之前设置子窗体过滤器.
Because the subform loads before the parent form, the parent form can not set a subform filter before the subform initially loads.
如果您想灵活地使用子表单(所有记录单独使用时,但记录的不同子集包含在不同的父表单中时,则使用不同的记录子集),我认为您必须修改子表单才能使用它.
If you want to use the subform flexibly (all records when stand alone, but different subsets of records when included on different parent forms), I think you have to modify the subform to do it.
Private Sub Form_Open(Cancel As Integer)
Dim strParent As String
Dim strMsg As String
On Error GoTo ErrorHandler
strParent = Me.Parent.Name
Select Case strParent
Case "frmYourParentForm"
'set filter to only records from today '
Me.Filter = "[created_on] >= #" & Date() & "#"
Me.FilterOn = True
Case "frmSomeOtherParent"
'do something else '
End Select
ExitHere:
On Error GoTo 0
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2452
'The expression you entered has an invalid reference to '
'the Parent property. '
Resume Next
Case Else
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure Form_Open"
MsgBox strMsg
End Select
GoTo ExitHere
End Sub
编辑:如果要跟踪父表单和子表单中的事件顺序,可以将这样的过程添加到表单模块中.
Edit: If you want to track the sequence of events in the parent form and subform, you can add procedures like this one to the forms' modules.
Private Sub Form_Load()
Debug.Print Me.Name & ": Form_Load"
End Sub
这是跟踪父表单和子表单的Open和Load事件时得到的信息.
Here is what I get when tracking the Open and Load events for my parent form and subform.
fsubChild: Form_Open
fsubChild: Form_Load
frmParent: Form_Open
frmParent: Form_Load
这篇关于可以在子表单数据加载之前在父表单的子表单上设置过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!