Using Shared Variable in Crystal Reports 8.5

February 28, 2014

This example show how to use Shared Variables in CR 8.5

I know it is an old version but the principles can be used in later versions as well.

Objective was to transfer a value from a sub-report into the main report for further calculations.

1. First create the sub-report, and link it to the Main report using the Insert Menu

image

2. When the link has been setup, insert a new Formula Field

image

In the new formula field you add a Shared Variable.

WhilePrintingRecords;
Shared NumberVar myTotal :=Sum ({Your.Field})

3. Next you go the Main Report and add as well a new Formula field.

image

In the new formula field you add reference to the previous  Shared Variable, of the sub-report.

WhilePrintingRecords;
Shared NumberVar myTotal;
myTotal;

4. Add the new Formula field in the main report

image

Keep in mind that you need to add the field in a SECTION BELOW the detailed section of the main report. In order for the variable to grab the values correct.

5. Making calculations using Shared Variables.

You can use the this just like any other field value to make your calculations.

WhilePrintingRecords;
Shared NumberVar myTotal;
If {Your.Field} <> 0 then
myTotal / {Your.Field};

Tips:

To get the sub-report field properly formatted in your report it’s a bit tricky.

Remove the BORDERS and if needed uncheck the Keep Objects Together

image

 

image

Enjoy !!