The examples up to this point in the chapter that involve passing data between a main report and a Subreport have worked exclusively through the Subreport Linking tab or dialog. Although the functionality provided there is certainly powerful, circumstances might require more flexible passing of data between the main report and the Subreport or the passing of data the other wayfrom a Subreport to a main report.
With the use of variables, it becomes possible to pass data between the main report and any of the Subreports or even among different Subreports in the same main report. By declaring the same shared variable in formulas in both the main report and at least one Subreport, data can be exchanged back and forth fluidly, and each report can leverage information from the other in a very flexible manner.
TIP
Using Subreports and variables to pass data back to a main report from a Subreport is an effective way to capture important summarizations or external information to your main report that is not possible otherwise because of the default groupings of the main report. A simple example in this chapter's last sample report would be the inclusion of a count on the number of suppliers for each product. Using only the default groupings provided in the main report (By Product), this count would be impossible to calculate. By using a Subreport, however, that count can be calculated external to the main report (in a Subreport), shared using variables, and eventually displayed on the main report.
To explore the power of shared variables, follow these steps to modify this chapter's last sample report:
NOTE
When passing shared variables from a Subreport to a main report, the involved Subreport cannot be set to On-Demand. The reason, of course, is that Subreports are not run until specifically requested by the business user. Therefore, their associated variables are not set until that time, making them unusable in the main report.
Figure 12.8. Formula with a shared variable declaration in the Subreport.
Figure 12.9. Formula with a shared variable declaration and output in the main report.
NOTE
As the comments in the Formula shown in Figure 12.9 highlight, it is important that this Formula is placed in the Group Footer of the Product Type Name Group. This strategic placement ensures that the Supplier List Subreport for the involved Product Type has already completed (as it is in the Group Header) and has set the shared SupplierCount variable appropriately. Careful consideration needs to be given when using variables to ensure they are evaluated at the time and in the order desired. In addition to the Top to Bottom and Left to Right default evaluation times of Crystal Reports, the EvaluateAfter() and a few other functions discussed in the Multi-Pass reporting section in Chapter 4, "Understanding and Implementing Formulas," are useful in ensuring the desired reporting results.
Figure 12.10. Sample report with Supplier Count sourced from a shared variable in a Subreport.
NOTE
As you discover the power of variables, you will begin to leverage this programming feature in increasingly complex ways. The Supplier Count example just provided is a relatively simple example that scratches the surface of the power of variables. Another variable-based technique that can be used to circumvent some common reporting challenges is to use variables to manage Running Totals. The flexibility provided within the Formula Editor and with variables enables you to create flexible condition-based running totals.
Perhaps not the prettiest report ever designed, this quick example does begin to convey the power and importance of shared variables in report design.
Part I. Crystal Reports Design
Creating and Designing Basic Reports
Selecting and Grouping Data
Filtering, Sorting, and Summarizing Data
Understanding and Implementing Formulas
Implementing Parameters for Dynamic Reporting
Part II. Formatting Crystal Reports
Fundamentals of Report Formatting
Working with Report Sections
Visualizing Your Data with Charts and Maps
Custom Formatting Techniques
Part III. Advanced Crystal Reports Design
Using Cross-Tabs for Summarized Reporting
Using Record Selections and Alerts for Interactive Reporting
Using Subreports and Multi-Pass Reporting
Using Formulas and Custom Functions
Designing Effective Report Templates
Additional Data Sources for Crystal Reports
Multidimensional Reporting Against OLAP Data with Crystal Reports
Part IV. Enterprise Report Design Analytic, Web-based, and Excel Report Design
Introduction to Crystal Repository
Crystal Reports Semantic Layer Business Views
Creating Crystal Analysis Reports
Advanced Crystal Analysis Report Design
Ad-Hoc Application and Excel Plug-in for Ad-Hoc and Analytic Reporting
Part V. Web Report Distribution Using Crystal Enterprise
Introduction to Crystal Enterprise
Using Crystal Enterprise with Web Desktop
Crystal Enterprise Architecture
Planning Considerations When Deploying Crystal Enterprise
Deploying Crystal Enterprise in a Complex Network Environment
Administering and Configuring Crystal Enterprise
Part VI. Customized Report Distribution Using Crystal Reports Components
Java Reporting Components
Crystal Reports .NET Components
COM Reporting Components
Part VII. Customized Report Distribution Using Crystal Enterprise Embedded Edition
Introduction to Crystal Enterprise Embedded Edition
Crystal Enterprise Viewing Reports
Crystal Enterprise Embedded Report Modification and Creation
Part VIII. Customized Report Distribution Using Crystal Enterprise Professional
Introduction to the Crystal Enterprise Professional Object Model
Creating Enterprise Reports Applications with Crystal Enterprise Part I
Creating Enterprise Reporting Applications with Crystal Enterprise Part II
Appendix A. Using Sql Queries In Crystal Reports
Creating Enterprise Reporting Applications with Crystal Enterprise Part II