How to Make Report Show Same Records as the Form You're Viewing

home

Using RECORSOURCE (not Filters). Go Here for Making Report Inherit a Form's FILTER

Monday, May 07, 2001 10:29:30

Suppose you have a form which shows certain records and you want a report to show the same records as this form. Suppose further than the criteria making the form show the records it is showing is NOT from a "form filter," but rather is due to the specific RECORDSOURCE of the form, which may be a filter saved as a query which was applied, or due to SQL applied to it. Whatever, these procedures will make it work.

  • Add a new module, with the following code, and save it under a name such as "utility_functions" (but do NOT name it "isloaded")

    Function IsLoaded(ByVal strFormName As String) As Boolean
    ' Returns True if the specified form is open in Form view or Datasheet view.

    Const conObjStateClosed = 0
    Const conDesignView = 0

    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
    If Forms(strFormName).CurrentView <> conDesignView Then
    IsLoaded = True
    End If
    End If
    End Function


  • Then, add the following code to the "on open" event of the report in question:
    If IsLoaded("browse_all_form") Then
    Me.RecordSource = Forms!browse_all_form.RecordSource
    Me.OrderBy = Forms!browse_all_form.OrderBy
    Me.OrderByOn = True
    End If

    Note: the bolded lines are optional lines which also makes sure that any sort orders applied to the browse_all form also get applied to the report, too.

    I also saved newsgroup messages which I received on this subject in early May 2001. Click here to read them.