How To…Access Subreport Values in the Main Report

Question

“How do I access subreport values inside the main report in RAP or vice versa?”

Solution

In Delphi this is simple, just access the values directly from the components needed as they are all defined in the Interface section of your application. In RAP however, components added to a Subreport are not available to the main report. To work around this issue use global declarations to give access to certain components to the entire project.

Example: Calculate a grand total in the main report based on Subreport values.

1. Place a variable in the main report to hold the grand total.

2. In the Calc Workspace (RAP), right click in the Report Objects area and select “Module” from the popup menu.

3. Select the Declarations icon. You will notice two new icons in the panel to the right. Right click the Variables icon and select “New”.

4. Define a TppVariable…

var
  gVariable: TppVariable;

5. Select the Events icon in the Module View and create a new OnCreate event. Assign the global variable you just created to the variable on the main report.

procedure GlobalOnCreate;
begin
  gVariable := Variable1;

end;

6. You now have access to this variable from subreports. Tab to a subreport and update the value of the global variable to change it’s value in the main report.

procedure SubVariableOnCalc(var Value: Variant);
begin
  {Running total (subreport)}
  Value := Value + Orders['AmountPaid'];

  {Grand Total (Main report)}
  gVariable.Value := gVariable.Value + Orders['AmountPaid'];

end;

Note that this can work both ways. If you need a value from the main report inside a subreport, simply access the value of the global variable inside the subreport.

Download: RAPGrandTotalInHeader.zip