To access values in sub-report which are available in main report and vice versa.
There are two ways of passing values from main report to sub report:
i) Linking Parameters
ii) Shared Variables
Linking parameters: To access a value in sub-report through linked parameter we need to do following steps:
1) Create a parameter in sub-report
2) In main report right click on sub-report and click “Change Sub report link”
3) Select a data field (or a parameter value or a formula field) from main report which we want to access in sub-report and link it with the parameter (created in sub-report in step 1) as shown in below screen shot
Figure 1: Change sub report link
4) Click OK
This concludes linking the parameter from main report to sub report. Now the sub report parameter will have the value from the main report through linked parameter. That means, we can access the value of main report in sub report thru linked parameter or even we can use this parameter value to filter the records in sub report.
Linking of parameters between main report and sub report will not only allows us to access main report parameter values in sub report, but also allows us to filter the sub report records as per some parameter values from main report in order to maintain Parent – Child relation.
Shared Variables: To access a value in sub-report through shared variable we need to do following steps:
1) Create a formula (“Shared Product Class” as shown in below screen shot) in main report by right clicking on formula fields in “Fields Explorer” and click “New”.
2) Now the Formula Editor will be displayed as in below screenshot.
3) Create a shared variable and assign a value to it as shown in below screen shot.
4) Place this formula in a section where the value needs to be processed and the sub report (in which this value needs to be referred) has to be placed in subsequent section.
Figure 2: Shared variable – Main report
5) Click on “Save and close”.
6) Now go to sub report and create a formula (“Shared Prod Class” as shown in below screen shot) in sub report and copy the code in formula editor.
Figure 3: Shared variable – Sub report
7) Click on “Save and close”.
8) Place this formula in the section in sub-report where we want to display this value as shown in above screen shot (@Shared Prod Class).
9) Now go to main report and run the report. We can see the value of shared variable in below screen shot.
Figure 4: Output for shared variable passing
In the above screenshot, the Product Class value (i.e. Accessory) is displayed once from main report and another time from sub report (as indicated in red font). Here, the database field used for this is available from main report only. But, since we have shared the value from main report and accessing the same in sub report, the same value is available in sub report (even though, there is no related database field in sub report).