Using Variables to Pass Data Between Reports


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:

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).

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.

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. (Hint: Right-click on the Supplier Name field and access the Summary menu option.) 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 for use in your formula by double-clicking on it.) The formula definition should resemble Figure 12.8.

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


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 Type Group Footer section, and call it Place Supplier Count. In this formula, declare the same shared numeric variableSupplierCountand make this variable the output of this formula. Figure 12.9 shows what this formula should look like.

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


Note

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.

6.

Add a text field to the report to complement the Supplier Count field called Supplier Count, hide the Details section on the main report and with a little creative formatting and group sorting, the final result should resemble Figure 12.10.

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


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

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.





Crystal Reports XI(c) Official Guide
Crystal Reports XI Official Guide
ISBN: 0672329174
EAN: 2147483647
Year: N/A
Pages: 365

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