Using Variables to Pass Data Between Reports

The examples up to this point in the hour 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 way from 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.

graphics/lightbulb_icon.gif

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 in your reports that are not possible otherwise because of the default groupings of the main report. A simple example in this hour'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 in modifying this hour's last sample report:

  1. Open the most recent sample report if you have closed it. Turn the Supplier Subreport back to an In-Place Subreport (versus On-Demand).

    graphics/bookpencil_icon.gif

    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.


  2. Edit the Supplier's Subreport by right-clicking on the Subreport and selecting the Edit Subreport option.

  3. Select the Supplier Name field and insert a summary field that counts the distinct supplier names in this report. This summary will shortly be assigned to the shared variable that will be created and used to pass the information back to the main report.

  4. Insert a formula into the Report Footer of this Subreport and call it 'Assign Supplier Count'. In this formula, declare a shared numeric variable called SupplierCount and then assign this variable to equal the Supplier Summary created in the last step. (Reminder: You can access the summary created in step 3 by double clicking on it.) The formula definition should resemble Figure 16.8.

    Figure 16.8. Formula with a shared variable declaration in the Subreport.

    graphics/16fig08.jpg

  5. Now click on the Preview or Design tab of the Crystal Reports Designer to take you back to the main report, insert a formula into the Product Group Footer section, and call it "Place Supplier Count". In this formula, declare the same shared numeric variable SupplierCount and make this variable the output of this formula. Figure 16.9 highlights what this formula should look like.

    Figure 16.9. Formula with a shared variable declaration and output in the main report.

    graphics/16fig09.jpg

  6. Add a text field to the report to complement the Supplier Count field called "Supplier Count", and your report should resemble that shown in Figure 16.10.

    Figure 16.10. Sample report with Supplier Count sourced from a shared variable in a Subreport.

    graphics/16fig10.jpg

Perhaps not the prettiest report ever designed, this quick example does begin to convey the power and importance of shared variables in report design.



Sams Teach Yourself Crystal Reports 9 in 24 Hours
Sams Teach Yourself Crystal Reports 9 in 24 Hours
ISBN: B003D7JUVW
EAN: N/A
Year: 2005
Pages: 230

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net