tags:

views:

53

answers:

1

I have frmParentForm with multiple controls used to build a filter for frmSubForm.

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.)

+2  A: 

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

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
HansUp
So, basically, **all** subform events occur before the parent form?
tbone
Not sure about all, but wouldn't be surprised if it turned out to be all. See if my updated answer helps.
HansUp