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

CalcFields events

edited May 2004 in General
Var
balance: Real;

procedure TForm1.Query1CalcFields(DataSet: TDataSet);
Var
amount:real;
begin
amount:=Query1.FieldByName('AMOUNT').AsFloat;
if Query1amount.Value>0 then begin
Query1debit.Value:=amount;
balance:=balance+amount;
end
else begin
Query1credit.Value:=abs(amount);
balance:=balance-abs(amount);
end;
Query1balance.Value:=balance;

end;

the debit, credit and balance is calculated field, when I try this coding
and display with the DBGrid, I can get the correct amount for the balance
field, but when I try to display it in report builder, I was unable to get
the correct balance. it look like because this events was fired many times
in report builder, because when i switch to page 2 and switch back to page
1, the value for the balance will be increase. so is any way to limit the
code only fired once which can let me to get the correct balance?

Comments

  • edited May 2004

    OnCalcFields will fire each time the record position changes in the dataset.
    You should only use this event to perform a calculation that depends on data
    field values in the same record. Never use this event to perform
    calculations across records. If you build an examples that scrolls back and
    forth thru the dataset, you will see that the calculations break.
    ReportBuilder does not simply traverse the dataset from front to back. Check
    out the Calculations tutorial in the Developers Guide and the Calculations
    thread of the Tech Tips newsgroups for details on performing calculations.


    --
    Nard Moseley
    Digital Metaphors Corporation
    http://www.digital-metaphors.com



    Best regards,

    Nard Moseley
    Digital Metaphors
    www.digital-metaphors.com
  • edited June 2004

    Thanks!
    I was try to use ppVariable1.OnCalc to calculate the balance, the result
    look like ok, but the problem for me is sometime end user may accidently
    delete the ppVariable during designer mode (designer.show), so is any way
    for me to lock the ppVariable which let the end user unable to delete? and
    if I want to using ppVariable to calculate balance, than end user may unable
    to select the balance for the data field like other DBtext, because it was
    fix with ppVariable.

    Rgds
    CH



  • edited June 2004

    Try using a summary dataset. Either use SQL to create a summary query that
    contains the calculated results or perhaps pre-process the data to build a
    temp table that contains the summarized calcs. Then use DBText inside the
    report.

    --

    Nard Moseley
    Digital Metaphors Corporation
    www.digital-metaphors.com


    Best regards,

    Nard Moseley
    Digital Metaphors
    www.digital-metaphors.com
This discussion has been closed.