In addition to passing a linking field to a subreport from a main report to limit the subreport s record selection, you may want to pass data from a main report to a subreport for other purposes. Or, you may want to pass data from a subreport back to the main report to use in summary calculations or similar functions. You can pass data to a subreport from the main report by using a parameter field. Crystal Reports also provides the shared variable, which allows you to pass data back and forth between main reports and subreports.
Passing data to a subreport from the main report without the subreport using it in record selection is fairly straightforward. Display the Subreport Links dialog box, as explained earlier in this chapter. Choose a linking field from the main report and add it to the Field(s) To Link To list. This will automatically add a parameter field prefixed with Pm- (all parameter fields automatically begin with a question mark) to the Subreport Parameter Field To Use drop-down list. Now, simply uncheck Select Data in Subreport Based on Field.
These steps will create the Pm-parameter field in the subreport and pass data to it, but the parameter field won t be used for subreport record selection. Then, just use the parameter field in subreport formulas or place it on the subreport for display, as desired. For example, if you added a parameter for the summary field for the last year s sales by region figure in the sample report, your subreport would know the region s sales subtotal even though the subreport is not grouped by region. You could then add, in the footer of the subreport, a formula that compared the credit sum for the region and the sales sum for the region, and conditionally display appropriate text.
Note | Chapter 14 discusses how to use parameter fields in your reports. |
Using shared variables is a consistent way to pass data back and forth between the main report and one or more subreports, or even from subreport to subreport. You ll need to create formulas that declare the same shared variable in both the main report and the subreport. You can assign the variable a value in the subreport and then read the contents of the variable in the main report. Or, you can assign a value in the main report and read it in the subreport.
Here s an example of a formula in a subreport that places the sum of a currency field into a shared variable:
WhilePrintingRecords;
Shared CurrencyVar CreditTotal := Sum ({Credit.Amount})
Here s the corresponding formula in the main report that retrieves the value of the shared variable:
WhilePrintingRecords;
Shared CurrencyVar CreditTotal
An important step in using shared variables is determining if and when you need to reset the value contained in the shared variable. This may be necessary if, for example, you are passing the credit total from the subreport to the main report, and your data set includes regions that have no credit records. Should this be the case, the subreport won t encounter any data records and therefore won t evaluate the report section containing the formula that places a value in the shared variable. The variable will retain its value from the previous region that did process, resulting in incorrect calculations for the region with no credit records. To avoid this problem, create a formula to reset the shared variable to zero for each new region:
WhilePrintingRecords;
Shared CurrencyVar CreditTotal := 0
After you place this formula on the main report (in the region group header, for this example) and verify that it s producing the correct results, you can use the Format Editor to suppress it so that the zeros don t display.
For more information on assigning and using variables, and other formula topics, refer to Chapter 5.
Note | Because subreports process after formulas in the main report processing cycle, you must take special steps to retrieve the proper contents of a shared variable set by the subreport. For example, if you place both the subreport and a formula to retrieve the contents of a shared variable set in that subreport in a group footer, you ll notice odd behavior. Typically, you ll find that the formula returns the value of the shared variable from the previous group, instead of the current group. This is because the subreport is setting the value of the shared variable after the formula to retrieve the variable it has already processed . To resolve this problem, insert an additional section (for example, a Group Footer #1 b), as described in Chapter 10. Then, place the formula that retrieves the value of the shared variable in the second section (Group Footer #1 b), while leaving the subreport in the first section (Group Footer #1 a). The formula that retrieves the contents of the shared variable will then retrieve the value from the correct subreport. |
When you link subreports, there may be situations in which the subreport won t retrieve any records that match the linking field from the main report. Typically, this will just result in a subreport showing up without any details sections. If there are column headings or other information in other sections, they may appear with zeros for subtotals.
There are two graceful ways to handle empty subreports: suppress them and any related totals, or display an informational message denoting the lack of data.
If you want to suppress the blank subreport altogether, check that option on the Subreport tab of the Format Editor. For this solution to work effectively, the subreport should reside alone in its own section. You may then format the section with the Section Expert and choose Suppress Blank Section. The entire section of the main report will then be suppressed if the subreport contains no data.
You may prefer to display an informational message instead of suppressing the blank subreport, similar to what s shown in Figure 13-5.
This is accomplished by conditionally suppressing different sections of the subreport based on the presence or absence of database records. Look at the Design tab for the subreport.
The group footer contains objects that you want to appear if the subreport actually returns records. The report footer contains the object (the text message) that you want to appear if the subreport doesn t return records. Conditional suppression of these two sections is done within the Section Expert.
Conditionally suppress the section containing the informational message by adding the following formula with the Conditional Formula button next to the Section Expert s Suppress property:
Not IsNull({Credit.Credit Authorization Number})
Then, conditionally suppress the sections that contain the actual subreport data with the following conditional formula applied to the Suppress property:
IsNull({Credit.Credit Authorization Number})
Don t forget ”you are conditionally suppressing these sections, not displaying them, so you may need to think backward. If the subreport is empty because no records were returned based on the linked field from the main report, the Credit Authorization Number will be null. In this case, you ll want to suppress the actual subreport data but not the informational message. If data is returned, then the Credit Authorization Number will contain data and will not be null. In this case, you want to suppress the informational message but not the actual subreport data.
Tip | You can use this technique for all of your Crystal Reports, not just subreports. If you encounter main reports that return no records, you can display an informational message in them. See Chapter 10 for more information on conditionally formatting sections. |