Board index » delphi » Database totalling a field of a found set

Database totalling a field of a found set

Hi! I

 am new to Delphi and would appreciate a little help on databases.

I can create a found set of records that meet certain criteria eg.
select all invoices for clients dated June 1998 and place them on a
tGrid, but how can I total them ?

I want a DBText box showing the total that is in the grid.

Any help would be really appreciated. Thanks in advance.

Regards, John
--
John Matthews

 

Re:Database totalling a field of a found set


In article <8miZRPAfnfB2E...@johnmatthews.demon.co.uk>, John Matthews
<J...@johnmatthews.demon.co.uk> writes
Quote

>Hi! I

> am new to Delphi and would appreciate a little help on databases.

>I can create a found set of records that meet certain criteria eg.
>select all invoices for clients dated June 1998 and place them on a
>tGrid, but how can I total them ?

>I want a DBText box showing the total that is in the grid.

>Any help would be really appreciated. Thanks in advance.

>Regards, John

Something like this should do it.

Table1.First;
while not Table1.EOF do
begin
  total := total + Table1.FieldByName('Amount').AsFloat;
  Table1.Next;
end;{}

--
Andrew James Johnson

Re:Database totalling a field of a found set


Hi!

I am grateful to you for your contribution, but surely this gives the
total of all the fields in the Table.  If it also works on a found set
of records, how can I know which set of records (the found set or all
the table) it is working on?

rgds, john

In article <NavwuBAlkgB2I...@sharry.demon.co.uk>, Andrew James Johnson
<a...@sharry.demon.co.uk> writes

Quote
>Table1.First;
>while not Table1.EOF do
>begin
>  total := total + Table1.FieldByName('Amount').AsFloat;
>  Table1.Next;
>end;{}

--
John Matthews

Re:Database totalling a field of a found set


In article <9KBg5BAFg0B2E...@johnmatthews.demon.co.uk>, John Matthews
<J...@johnmatthews.demon.co.uk> writes
Quote

>Hi!

>I am grateful to you for your contribution, but surely this gives the
>total of all the fields in the Table.  If it also works on a found set
>of records, how can I know which set of records (the found set or all
>the table) it is working on?

>rgds, john

>In article <NavwuBAlkgB2I...@sharry.demon.co.uk>, Andrew James Johnson
><a...@sharry.demon.co.uk> writes
>>Table1.First;
>>while not Table1.EOF do
>>begin
>>  total := total + Table1.FieldByName('Amount').AsFloat;
>>  Table1.Next;
>>end;{}

This will give a total of the field called, in this snippet, 'Amount'
and it will include all records in Table1.  It will also work if you use
a query component in place of table1.
--
Andrew James Johnson

Re:Database totalling a field of a found set


In article <9KBg5BAFg0B2E...@johnmatthews.demon.co.uk>, John Matthews

Quote
<J...@johnmatthews.demon.co.uk> writes:
>I am grateful to you for your contribution, but surely this gives the
>total of all the fields in the Table.

Not if you've already limited or filtered the dataset, which you must have done
if the grid is showing you the result set you require.

Charles Johnson

Other Threads