Solved Ability to Create/Print Report from User Filtered Sub-form (1 Viewer)

Tamille2020

New member
Local time
Today, 07:30
Joined
Feb 22, 2022
Messages
4
My end user is using a filter on a sub-form (SearchQSubform) then pressing the "PrintPDF" button that is located on the form (SearchForms). However, all the searched data in the sub-form is printing. I only wish to print the user's filtered data. I tried putting the following code in the "On Click" event procedure for the "PrintPDF" button:

Private Sub PrintPDF_Click()

DoCmd.OpenReport ReportName:="Report", View:=acViewReport, WhereCondition:="SearchQSubform.Filter"


Here are the details of the database:

Main Form Name: "SearchForms:
Sub-form Name: "SearchQSubform"
The Query the sub-form is based on: "SearchQ"
Name of the report to be viewed/printed: "Report"

What am I doing wrong? Please help.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:30
Joined
Oct 29, 2018
Messages
21,357
Maybe try something like:

WhereCondition:=Me.SearchQSubform.Form.Filter
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:30
Joined
May 7, 2009
Messages
19,169
you may try:

WhereCondition:= Me![SearchQSubform].[Form].[Filter]

//to slow.

but if your report uses different recordsource, that's another thing.
you need to remove the "form" reference from the filter string.
 

Tamille2020

New member
Local time
Today, 07:30
Joined
Feb 22, 2022
Messages
4
WhereCondition:= Me![SearchQSubform].[Form].[Filter]
Record source for both the sub-form and report are the same (SearchQ). I get the "Enter Parameter Value" popup box that asks for "SearchQSubform.Recipients" input. The column that is being filtered by the user is "Recipients".
 

Tamille2020

New member
Local time
Today, 07:30
Joined
Feb 22, 2022
Messages
4
WhereCondition:=Me.SearchQSubform.Form.Filter
I get the "Enter Parameter Value" popup box too with this code. It is asking for "SearchQSubform.Recipients" input. The column that is being filtered by the user is "Recipients".
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:30
Joined
May 7, 2009
Messages
19,169
like i said earlier, you need to "get rid" of the form reference from the filter:

Private Sub PrintPDF_Click()
Dim strCriteria As String
strCriteria = Trim$(Me![SearchQSubform].[Form].[Filter] & "")
If Len(strCriteria) Then
strCriteria = Replace$(Replace$(strCriteria, "[SearchQSubform].", ""), "SearchQSubform.", "")
Else
strCriteria = "(1 = 1)"
End If

DoCmd.OpenReport ReportName:="Report", View:=acViewReport, WhereCondition:=strCriteria
 

Tamille2020

New member
Local time
Today, 07:30
Joined
Feb 22, 2022
Messages
4
Private Sub PrintPDF_Click()
Dim strCriteria As String
strCriteria = Trim$(Me![SearchQSubform].[Form].[Filter] & "")
If Len(strCriteria) Then
strCriteria = Replace$(Replace$(strCriteria, "[SearchQSubform].", ""), "SearchQSubform.", "")
Else
strCriteria = "(1 = 1)"
End If

DoCmd.OpenReport ReportName:="Report", View:=acViewReport, WhereCondition:=strCriteria
That worked! Thank you! It prints the data when it's filtered and when it's not filtered. Brilliant! I am a novice with VBA, I would not have come up with this solution.
 

Users who are viewing this thread

Top Bottom