How To…Use a Look Ahead Value in a Calculation

Question

“How do I make a calculation based on a total (such as a percentage) inside a report using a look ahead value?”

Solution

Retrieve the look ahead value using the DBCalc.GetLookAheadValue routine and convert it to a numerical type.  Then use this value in a new calculation elsewhere in the report.

Download: CalcUsingLookAheadValue.zip

CalcUsingLookAheadValuesRAP.zip

Sample Delphi code:

function TForm1.GetLookAheadTotal: Variant;
var
  lsTotal: String;
begin

  {retrieve the look ahead total and convert from Text to Float value.
    note: the lookahead values are cached as Strings. The content of the string
          is exactly what is rendered to the page. Therefore it is a formatted value }
  lsTotal := ppDBCalc1.GetLookAheadValue(ppDBCalc1.GetLookAheadKey);

  lsTotal := StringReplace(lsTotal, CurrencyString, '', [rfReplaceAll]);
  lsTotal := StringReplace(lsTotal, ThousandSeparator, '', [rfReplaceAll]);

  Result := StrToFloat(lsTotal);

end;

procedure TForm1.ppVariable1Calc(Sender: TObject; var Value: Variant);
begin
  {use look ahead total to calc the percentage for this order}
  if ppReport1.SecondPass then
    Value := (ppDBPipeline1['ItemsTotal'] / GetLookAheadTotal) * 100;

end;