Board index » delphi » Help: Running total field in a database app...

Help: Running total field in a database app...

Here's my situation: I have a database app which uses a paradox 5.0 table (I'm
using delphi 1.0). I have a number field which holds the number of minutes
spent on a task. What I would like is to have a second field which holds the
accumulated time spent up to and including the current record. For example, a
record midway through the database might look like:

Time Spent (min): 50
Accumulated time spent since start of file (min): 4367

This setup would have to accommodate deletion of records (it would have to
update all the following records to correct the accumulated values), and also
similarly the insertion of new records.

My question to you all is: how can this be best done? Speed is important, but
right now, I could use ANY solution...

Any help would be greatly appreciated,

Kris Wilk
ReefNet Software

 

Re:Help: Running total field in a database app...


Quote
reef...@interlog.com wrote:

> Here's my situation: I have a database app which uses a paradox 5.0 table (I'm
> using delphi 1.0). I have a number field which holds the number of minutes
> spent on a task. What I would like is to have a second field which holds the
> accumulated time spent up to and including the current record. For example, a
> record midway through the database might look like:

> Time Spent (min): 50
> Accumulated time spent since start of file (min): 4367

> This setup would have to accommodate deletion of records (it would have to
> update all the following records to correct the accumulated values), and also
> similarly the insertion of new records.

> My question to you all is: how can this be best done? Speed is important, but
> right now, I could use ANY solution...

> Any help would be greatly appreciated,

The TTable object has a heap of events - BeforeDelete, AfterPost.  Place code in these events
to spin through each record, recalculating the totals.  I suggest you have another table, with
just these time fields and a key to link back, so that the physical file size of the table is
minimised, thus increasing the speed of the app.

--
Mark.

Re:Help: Running total field in a database app...


In article <4vq4vf$...@news.interlog.com>, From reef...@interlog.com,
the following was written:

Quote
> Here's my situation: I have a database app which uses a paradox 5.0
> table (I'm using delphi 1.0). I have a number field which holds the
> number of minutes spent on a task. What I would like is to have a
> second field which holds the accumulated time spent up to and
> including the current record. For example, a record midway through the

  > database might look like:

Quote

> Time Spent (min): 50
> Accumulated time spent since start of file (min): 4367

> This setup would have to accommodate deletion of records (it would
> have to update all the following records to correct the accumulated
> values), and also similarly the insertion of new records.

> My question to you all is: how can this be best done? Speed is
> important, but right now, I could use ANY solution...

Add a Timer to your app(on System tab) and a variable to hold
seconds(MySeconds in example).  You will need to initialize it to zero
somewhere.  Here is a possible sample of the OnTimer event code:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
     MySeconds := MySeconds+1;
     if (Table1.State <> dsEdit) and (Table1.State<>dsInsert) then
     begin
        Table1.Edit;
        Table1Seconds.Value := MySeconds;
        Table1.Post;
     end;
     else
         Table1Seconds.Value := MySeconds;
     {User editing Post handled elsewhere}
end;

God Bless,

--
Jay Schwisow j...@weldnet.com
08/26/96 00:42
---------
Using: OUI PRO 1.5.0.2 from http://www.dvorak.com

Re:Help: Running total field in a database app...


Quote
reef...@interlog.com wrote:
>Here's my situation: I have a database app which uses a paradox 5.0 table (I'm
>using delphi 1.0). I have a number field which holds the number of minutes
>spent on a task. What I would like is to have a second field which holds the
>accumulated time spent up to and including the current record. For example, a
>record midway through the database might look like:
>Time Spent (min): 50
>Accumulated time spent since start of file (min): 4367
>This setup would have to accommodate deletion of records (it would have to
>update all the following records to correct the accumulated values), and also
>similarly the insertion of new records.
>My question to you all is: how can this be best done? Speed is important, but
>right now, I could use ANY solution...
>Any help would be greatly appreciated,
>Kris Wilk
>ReefNet Software

You could have an additional TTable whose order is set to whatever
order the cumulative values apply to. In the tables where the new
values are put, let the BeforePost event check the difference between
the new value or the old value in the case of an update, or the value
entered in the case of a new record etc. If the post is successful use
the GoToCurrent method to sync the second TTable to the first and scan
to the end updating the cumulative values with the Delta.

Other Threads