Tobias.Schwuerzinger
Tobias.Schwuerzinger

Reputation: 31

Expression with aggregate function not working if set within script

I want to print this total sum in an memo field within the ReportSummary-Section of my Report

[ SUM( <DETAILDATASET."IntegerField">, DetailData1)]

This works fine if I put this code directly into the the text of the memo field. However if I set the code using the "OnBeforePrint"-handler

procedure ReportSummary1OnBeforePrint(Sender: TfrxComponent);
begin  
  Memo1.Text  := '[ SUM( <DETAILDATASET."IntegerField">, DetailData1)]';
end;

nothing is displayed. The expression evalutes to NULL.

It seems to be some problem with aggregate functions since other expressions (e.g. [date] or [1 + 1]) work when set in the "OnBeforePrint"-handler.

The fast report documentation does not mention any restrictions on using aggregate functions within the report script.

Is it possible to use aggregate functions using the script? Am I missing some setting to make it work?

I am using FastReport Version 2024.2.8 with Delphi 10.4.2.

Upvotes: 0

Views: 69

Answers (1)

Tobias.Schwuerzinger
Tobias.Schwuerzinger

Reputation: 31

The problem was indeed caused by the handling of aggregate functions specifically. Those are added to the report engine's aggregatelist (Engine.Aggregate) if they are directly entered into the text of a memo field. Only aggregates contained in this list are calculated when you print the report. If you set an aggregate function using the report script it is however not added to that list automatically.

The solution was therefore to manually add the aggregate function to the aggregatelist in the report script:

procedure ReportSummary1OnBeforePrint(Sender: TfrxComponent);
begin  
  Memo1.Text  := '[ SUM( <DETAILDATASET."IntegerField">, DetailData1)]';  
  
  TFrxMyEngine( Engine).GetAggregates.AddItems( Page1);
end;

As the required functions (TFrxMyEngine.GetAggregates and TfrxCustomAggregateList.AddItems) are not available in report scripts out of the box they first have to be made available for the report as custom methods. I followed the official fast report dokumentation for that.

Upvotes: 0

Related Questions