Home Subreports
New Blog Posts: Merging Reports - Part 1 and Part 2

Variable calculation in a subreport question

edited November 2005 in Subreports
Hello,

Background:
I have a Customer Account statement that has a subreport main report's
detail band.
The report is grouped by customerid and the page numbering will be reset
when it encounters a new customerid value.
The subreport1 has a detail band with a variable3 object in it.
I have coded the calculation for the variable in the
detailbandbeforegenerate (see code below).
Goal: is to have the balance be a cummulative total of the amount column.


Problem:
The variable calculation is correct with one exception. When the report for
the current customer is more than one page, the calculation is performed
twice.
Example:
Amount Balance
32.00 4668.10
43.00 4711.10
203.00 4914.10 (this is the last line of the first page
for the current customerid)

30.60 4975.30 (this is the first line of the next page
for the current customerid) This is the problem line where 30.60 is
being added twice.
-50.00 4925.30
-40.00 4885.30
12.42 4897.72


Question: What do I need to do in order to get the right cummulative
calculation total?

Report code in the subreport's detailband
procedure TForm1378.ppDetailBand2BeforeGenerate(Sender: TObject);
begin
inherited;
if (Rpt1378r1.Tag = 4) then
begin
if AddBalanceForward then
begin
ppVariable3.Value := (ppVariable2.Value + (ppvariable3.Value +
DM1378.qMasterARAMOUNT.Value));
AddBalanceForward := False;
end
else
ppVariable3.Value := (ppvariable3.Value +
DM1378.qMasterARAMOUNT.Value);

end;


Thanks in advance,

Karen

end;

Comments

  • edited November 2005
    Hi Karen,

    When making calculations, you need to use the TppVariable.OnCalc event
    rather than the BeforePrint event to guarantee only firing once per
    traversal. See the article on making calculations below for more
    information...

    ----------------------------------------------------------------------
    TECH TIP: Performing Calculations
    ----------------------------------------------------------------------

    Calculations can be done either on the data access side
    or within ReportBuilder.

    When designing reports there are always decisions to be made as to
    how much processing to do on the data side versus the report side.
    Usually doing more on one side can greatly simplify the other. So it is
    often a personal choice based on the power and flexibility of the data
    and report tools being used.


    DataAccess
    ----------

    a. Use SQL - using SQL you can perform many common calculations:

    example: Select FirstName + ' ' + LastName As FullName,
    Quantity * Price AS Cost,


    b. Delphi TDataSets enable you to create a calculated TField object
    and use the DataSet.OnCalcFields event

    c. Perform any amount of data processing, summarizing, massaging
    etc. to build a result set (query or temp table) to feed to the report.


    ReportBuilder
    -------------

    Calculations in ReportBuilder are performed primarily using
    the TppVariable component.

    a. Set the Variable.DataType

    b. Code the calculations using the Variable.OnCalc event.

    c. Use the Timing dialog to control the timing of the OnCalc event.
    To access the Timing dialog, right click over the Variable
    component and select the Timing... option from the speed menu.

    d. Set the LookAhead property to True, when you need to display
    summary calculations in the title, header, group header, etc.

    e. To perform calculations based on the results of other
    calculations use the Calc Order dialog of the band. To access
    the Calc Order dialog, right click over the Band component
    and select the Calc Order... option from the speed menu.


    By using TppVariable components ReportBuilder will take care of caching
    intermediate results of accumlated calcs that cross pages.

    There are a number of calculation examples in the main demos.dpr
    project.

    ---

    Additional Notes:

    1. Do NOT use Band.BeforePrint or Band.AfterPrint. These events fire
    multiple times and therefore should not be used for calculations.

    2. Do NOT store results in variables that exist outside of the reports.
    For example - form level variables.


    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited November 2005
    Nico,

    Thanks for the explanation and tech tip.

    It works now!

    Karen
  • edited December 2005
    What version of RB you use Karen?




This discussion has been closed.