Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

9.21 Tutorial 9-8 Resetting Subreport Variables

Tutorial 9-8. Resetting Subreport Variables

One thing you might not have noticed in the previous report is that there is actually a problem with it. The subreport doesn’t reset the shared variable back to zero. If there is a customer without any credits, the net sales figure would be wrong because it will still be using the total credits from the previous customer. If you open the report and go to page 8 (the first customer without any credits), you’ll see that the net sales figure is, in fact, completely wrong.


The net sales amount should be the same as the total sales because there aren’t any credits to subtract from it. Instead, the net sales are off by $3,847.13. If you go back to page 7 to the previous customer, you’ll see that their credit sub-total is exactly $3,847.13. Since the subreport didn’t have any data, it didn’t calculate a new value for the TotalCredits shared variable. The previous customer’s amount got carried forward to the next customer. Let’s fix this right away so that all customers have the correct totals.

  1. Open the report ‘11 Customer Net Sales.rpt’.
  2. Double-click on the Customer Credits subreport to open it in design mode.
  3. Add a new formula and call it Reset Credits. Enter the following code:

WhilePrintingRecords;
Shared CurrencyVar TotalCredits;
TotalCredits := 0;

  1. This formula declares the shared variable and sets it to zero. It also uses the function WhilePrintingRecords to force it to be called each time the subreport prints.
  2. Save and close the formula.
  3. Drag and drop the Reset Credits formula into the report header.
  4. Since the formula is always zero, we don’t want it to be visible on the subreport. Right-click on it and select Format Field. Go to the Common tab and click on the Suppress property to select it. Click the OK button to save your changes.
  5. Preview the report and go to page 8 again. This time the net sales amount should be equal to the total sales amount because the credits from the previous customer did not get carried forward.
  6. Save this report as Customer Net Sales Corrected.rpt.