To illustrate how to do this in your own reports, the customer sales report that has been used throughout this chapter is modified so that it won’t show the subreport if there are no credits for a customer. As shown in Figure 12-7, customer Spokes ‘N Wheels has no credits listed, but the subreport still shows the column headers.
Figure 12-7. An empty subreport still shows column headers.
The first step is to add a new section where the subreport is and insert a new copy of the subreport into this section. If the subreport was created from an existing report, then simply base the new subreport off the same report. If the subreport was created from scratch, then you have to run through the report expert again to recreate it. This new subreport must have the same links to the main report as the original subreport.
When finished, the main report now has two sections with a subreport in each. The first section has the original subreport and the second section has the copy. Figure 12-8 shows how the sample report looks. I put the copy of the subreport in Group Footer #1b (I used the template shortcut method) and Group Footer #1c has the original subreport.
Figure 12-8. The subreports in the Group Footer sections.
The first subreport is responsible for determining if there are any records being printed and it sets a shared Boolean variable accordingly. You can test for the existence of any records by using either the IsNull() function or seeing if the Count() function returns 0. For this example, I used the IsNull() function and tested the Credit Authorization Number field. This formula is placed in the Report Header section of the subreport.
Shared SuppressSub As Boolean
SuppressSub = IsNull({Credit.Credit Authorization Number})
Formula = SupressSub
The purpose of the first subreport is to determine whether there is any data that should be printed, but you don’t want two copies of the same subreport to be shown to the user. So you have to hide the first subreport. As you learned earlier, if you hide or suppress a subreport, then its formulas won’t be calculated. So you have to set the subreport object to have a height of 0 and set the section to Underlay Following Sections (so that the section doesn’t take any space on the report). Setting the Underlay property is done via the format properties in the designer. To set the subreport height to 0, use the following code when opening the report in your .NET application.
Dim rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim rptObjects As CrystalDecisions.CrystalReports.Engine.ReportObjects
rpt = New SuppressSubreportDemo()
rptObjects = rpt.ReportDefinition.ReportObjects
rptObjects.Item("SubreportCopy").ObjectFormat.EnableCanGrow = False
rptObjects.Item("SubreportCopy").Height = 0
CrystalReportViewer1.ReportSource = rpt
The subreport in the second section is the one that the user will see. It is modified so that it is only displayed if the first subreport determined that there are records to be printed. Use the shared Boolean variable in the conditional formatting formula of the Suppress (No Drill-Down) property for the section that has the subreport.
Shared SuppressSub As Boolean
Formula = SupressSub
Figure 12-9 shows the report after these changes have been made. You can see that the subreport is now suppressed for the customer Spokes ‘N Wheels, and it is still visible for the next customer.
Figure 12-9. The subreport is suppressed when there are no records.