Board index » delphi » Calculated Field needing Prior record's value

Calculated Field needing Prior record's value

I have a Table with one "normal" field and one calculated field.
The calculated field is supposed to be the diffence between the current
records value
and the prior records values.

Currently I have tried the code below but that puts causes an infinite loop
(when Prior is called the current record is commited, which causes another
call to the calculate function which then calls next and causes another
calculate function call, etc, etc, etc...).

  dValue := 0;
  if not DataSet.Bof then
  begin
    dValue := DataSet['V1'];
    DataSet.Prior;
    dPrior := DataSet['V1'];
    DataSet.Next;
    dValue := dValue - dPrior;
  end;
  DataSet['MovingRange'] := dValue;

So is there any way to do this without crashing my machine?

 

Re:Calculated Field needing Prior record's value


A simple way would be to use a second Table to read the prior record.
--
Finn Tolderlund

"Chris Brandsma" <cwbrand...@hotmail.com> skrev i en meddelelse
news:3cdaad96_2@dnews...

Quote
> Currently I have tried the code below but that puts causes an infinite
loop
> (when Prior is called the current record is commited, which causes another
> call to the calculate function which then calls next and causes another
> calculate function call, etc, etc, etc...).

Re:Calculated Field needing Prior record's value


You'll have to use Finn's suggestion of a second table - you can't move
between records during the OnCalcFields event handler because moving will
cause OnCalcFields to be fired again (as you've found out!)

-Dell

Re:Calculated Field needing Prior record's value


You could declare a private variable to hold the previous value in your form,
initialize it to 0 in the FormCreate event, and then use it in your
OnCalcFields event; something like...

MyForm = class(TForm)
private
  FPrevValue: integer;
    .
    .
    .
end;

procedure MyForm.FormCreate(Sender: TObject);
begin
  FPrevValue := 0;
end;

procedure MyForm.MyTableCalcFields(DataSet: TDataSet);
begin
  DataSet['MovingRange'] := DataSet['V1'] - FPrevValue;
  FPrevValue := DataSet['V1'];
end;

hth,
Michael

Quote
Chris Brandsma wrote:
> I have a Table with one "normal" field and one calculated field.
> The calculated field is supposed to be the diffence between the current
> records value
> and the prior records values.

> Currently I have tried the code below but that puts causes an infinite loop
> (when Prior is called the current record is commited, which causes another
> call to the calculate function which then calls next and causes another
> calculate function call, etc, etc, etc...).

>   dValue := 0;
>   if not DataSet.Bof then
>   begin
>     dValue := DataSet['V1'];
>     DataSet.Prior;
>     dPrior := DataSet['V1'];
>     DataSet.Next;
>     dValue := dValue - dPrior;
>   end;
>   DataSet['MovingRange'] := dValue;

> So is there any way to do this without crashing my machine?

Other Threads