Passing Values Between Reports (Crystal Reports – 2008)
There are 2 ways in which a value or a pre-calculated field can be passed from a main report to the sub report.
1) Creating a Parameter and linking the sub report to the main report (primary report)
2) Creating Shared Variables.
Passing values using Parameters
Scenario: – In a Business requirement where we have the Main Report which displays the country data and the sub report which displays the sales data.
Following are the steps required to pass a value from a main report to a sub report using linked parameter
1) Create a Parameter at the Main Report (Say Parameter name: – Country ID).
2) Link the two reports using the Country ID.
3) Run the Report. (Here when the report runs, it finds the first country data and passes the associated country id from that record to the sub report parameter field. Like wise the process is followed for all the associated country ids with the corresponding country data).
4) All the parameter field manipulation takes place behind the scenes. We only have to pick up the fields that will link the main report (primary report) with the sub report.
(Note: – The values are passed from the main report to the sub report, with out the parameter field prompting you for a value)
In the above scenario we can even create a calculated formula field at the main report and then pass the same field to the sub report by following the below process
1) Create Formula field at the main report from the field explorer menu.
2) Link the sub report to the main report by the newly created formula.
3) Run the report.
4) The calculated formula field would be directly passed on to the sub report through the linked parameter. (An equivalent linked parameter will be created automatically at the sub report when both the reports are linked).
5) At the sub report level the parameter field value can be further used for any customized calculation or for display at the sub report section level.
Passing values using shared variables.
Shared variables are those variables which can be shared with the sub report as well as with the current report.
Steps to pass values from a main report to the sub report using shared variables
1) Create a formula say fr_shared_variable using the formula editor from the field explorer menu
2) Paste the below code at the formula editor window
Shared NumberVar z: =10;
(Assigns a value to the shared variable z of type Number)
(Note: – we can also assign any calculated business logic or a database field as per the requirement to the shared variable)
3) Now go to the sub report and create a new formula say fr_value_shared_variable using the formula editor from the field explorer menu
4) Paste the below code at the formula editor window
Shared NumberVar z;
(Declares the shared variable at sub report)
5) The shared variable which is assigned a value at the Main report using formula fr_shared_variable and declared at the sub report using the formula fr_value_shared_variable is now ready to be used.
6) At the sub report level the shared variable can be further used for any customized calculation or for display at the sub report section level by making use of the formula fr_value_shared_variable.
1) In order to use shared variables, the variable must be declared and assigned a value before it can be passed between the main report and the sub report.
2) The values can also be passed from the sub report to a main report by using shared variable which is assigned a value at sub report and declared at main report. For doing so the sub report should be placed at a section in the main report level which is executed prior to the section at the main report which uses the shared variable.
3) By using a parameter the values can only be passed from a main report to the sub report and not from the sub report to the main report.