When I try to access the value of an aggregate field (SUM_TOTAL) as the following line:

DM.cdsTOTAL.AsCurrency := DM.cdsItemSUM_TOTAL.AsCurrency;

The following error occurs:

Cannot access field 'SUM_TOTAL' as type float.

My temporary solution is this:

DM.cdsTOTAL.AsCurrency := StrToCurrDef(DM.cdsItemSUM_TOTAL.AsString, 0);

Any other suggestions?

有帮助吗?

解决方案 2

You can implement Class Helpers for TAggregateField and if needed for TAggregate

type
  TAggregateHelper=Class Helper for TAggregate
  private
    function GetCurrency: Currency;
  published
    Property asCurrency:Currency read GetCurrency;
  End;

  TAggregateFieldHelper=Class Helper for TAggregateField
  private
    function GetCurrency: Currency;
  published
    Property asCurrency:Currency read GetCurrency;
  End;


implementation



{ TAggregateHelper }

function TAggregateHelper.GetCurrency: Currency;
begin
  if not VarIsNull(Value) then
     Result := Value
  else Result := Value;
end;
{ TAggregateFieldHelper }

function TAggregateFieldHelper.GetCurrency: Currency;
begin
  if not VarIsNull(Value) then
     Result := Value
  else Result := Value;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Showmessage(FloatToStr(CDSTotal.AsCurrency) + '-' + FloatToStr(CDS.Aggregat[0].asCurrency));
end;

其他提示

that's how TAggregateField is underdesigned. see implementation details in db.pas to get the idea that it doesn't override base TField.GetAs... family that provokes aforementioned AccessError

Just a shot in the dark:

begin
  If DM.cdsItemSUM_TOTAL.IsNull then
  begin 
    DM.cdsTOTAL.AsCurrency := 0;
  end 
  else
  begin
     DM.cdsTOTAL.AsCurrency := Currency(DM.cdsItemSUM_TOTAL.Value);
  end;
end;

Since the value is a float (probably a double), I believe it can cast to a currency variable.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top