Solved

# Calculate using a field in current & prev record

Posted on 2000-05-17
157 Views
Hi,

Is there an easy way to calculate the difference (movement) between a field value in my current record and the value of the same field in a either the previous or the next record. This should be done with all the records in the table (like a calculated field).

i.e.
field         calculated field
record 12        1090
record 13        1096        6(movement 13-12)

Thanks

0
Question by:Surv

Expert Comment

would this work for you?

function Difference(Direction: string): integer;
var
Current: integer;
begin
Current := table.fieldByname('calculated').AsInteger; // get current value;

if Direction = 'Next' then
begin
table.Next;
result := table.fieldbyname('calculated').AsInteger - current;
table.previous;
end
else if Direction = 'Previous' then
begin
table.previous;
result := current - table.fieldbyname('calculated').AsInteger;
table.next;
end
else
result := 0;
end;

assuming your table is called table etc.

Hope that helps,

Dave.
0

Author Comment

Adjusted points from 40 to 100
0

Author Comment

Edited text of question.
0

LVL 15

Expert Comment

What database server are you using?

Alex
0

Author Comment

Hi Simonet,

No server, just a small paradox 7 table, stand alone pc.

Is my question clear ? It almost seems as if I wrote it a bit confusing.

I have a field which stores a value. All I would like to do is create an extra field or whatever to display the movements of the difference between this field's values between records It should display this "movement" field in a dbgrid preferably.

I am quite new to delphi / Paradox etc.
If you know FileExpress for example, you could enter a function which will calculate the movement between fields in different records by accessing the next or previous records without moving the focus to them i.e.

field_z := field_3 - field_3(prev)

Can you possibly help me to an answer.

Cheers
0

LVL 2

Accepted Solution

IPCH earned 100 total points
Hi Surv

If you use Paradox I'll propuse you some kind of decision. Use 2 TTable components to connect to one Paradox table. Create calculated field , let say
Table1Calc and take a look at this code:

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
begin
If Table1.RecNo=1
Then Table1Calc.Value:=0
Else Begin
Table2.First;
table2.MoveBy(Table1.RecNo-2);
Table1Calc.Value:=Table1AmountPaid.Value-table2.FieldByName('AmountPaid').Value;
end;
end;

It's not very elegant, but it's working I checked it with Delphi demo database table orders.db.

Regards
Ivan
0

Author Comment

0

Author Comment

Thanks Ivan,

I can use it, it works ok.

cheers - Surv
0

LVL 2

Expert Comment

You well come.

Ivan
0

## Featured Post

### Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.