Question : Print a Filtered SubForm

Hello all.  I can't believe how difficult this has become.  I'm trying to print a subform  which has been filtered with a mutiselect listbox.  The main form, subform, and listbox work perfectly.  The user can make as many selections as they want, and the subform, in datasheet view, responds accordingly.  I simply want to click a print button, on the main form, and print the filtered subform.  This would definitely be the most user friendly way.  I'm familiar with queries and filter by form, but for the crowd which will be using this database, simple is better.    

My problem is, when I hit the print button, I get a query parameter box, then the report opens with all records displayed, not the filtered records.

The report is the subform saved as a report.

Here is the code I'm using for the listbox filter and print button:

This is the code in the after_Update event of my listbox:

Private Sub LstShifts_AfterUpdate()
    Dim sWhere As String
    Dim varItem As Variant
   
    For Each varItem In LstShifts.ItemsSelected
        If Len(sWhere) > 0 Then sWhere = sWhere & " OR "
        sWhere = sWhere & "Shift='" & LstShifts.ItemData(varItem) & "'"
    Next varItem
   
    Me!SubSenority.Form.Filter = Nz(sWhere)
    Me!SubSenority.Form.FilterOn = Len(sWhere) > 0
End Sub


This is the code in the on_click event of my print report button:

Private Sub Command8_Click()
    If InStr(Me![SubSenority].Form.RecordSource, "sWHERE ") <> 0 Then
        MsgBox "Apply a filter to the form first"
    Else
        DoCmd.OpenReport "RptSenorityDetails", A_PREVIEW, , Mid$(Me![SubSenority].Form.RecordSource, _
        InStr(Me![SubSenority].Form.RecordSource, "sWHERE") + 1)
    End If
   
End Sub

The main form is unbound.
The listbox contains 4 text values.
The subform is based on a query.

I've spent two days scouring the forums, and have tried countless suggestions.  Nothing is working.  Can anyone, anyone, please help me.

Answer : Print a Filtered SubForm

Try using the open event of the report to set the recordsource and filter to that of the subform.

Something like this:

Private Sub Report_Open()
      me.recordsource = Forms!YourMainFormName.YourSubformName.Form.recordsource
      me.Filter = Forms!YourMainFormName.YourSubformName.Filter
      Me.FilterOn = true
End sub
Random Solutions  
 
programming4us programming4us