Solved

Calculate using a field in current & prev record

Posted on 2000-05-17
9
159 Views
Last Modified: 2010-04-04
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
Comment
Question by:Surv
9 Comments
 

Expert Comment

by:johnstoned
ID: 2820748
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

by:Surv
ID: 2821426
Adjusted points from 40 to 100
0
 

Author Comment

by:Surv
ID: 2821440
Edited text of question.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 15

Expert Comment

by:simonet
ID: 2829595
What database server are you using?

Alex
0
 

Author Comment

by:Surv
ID: 2830405
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

by:
IPCH earned 100 total points
ID: 2850358
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.

Hope it'll help you.

Regards
Ivan
0
 

Author Comment

by:Surv
ID: 2885624
Comment accepted as answer
0
 

Author Comment

by:Surv
ID: 2885625
Thanks Ivan,

I can use it, it works ok.

cheers - Surv
0
 
LVL 2

Expert Comment

by:IPCH
ID: 2889824
You well come.

Ivan
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question