Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate sum of column in Table

Status
Not open for further replies.

PiotrG

Programmer
Jun 4, 2001
10
0
0
GB
I am using Delphi 5, and would like to know how to calculate the sum of a column in a table. I have got a grid and done this 'sum:=DBGrid2.Columns[6].Field.value;' but all this does is do the sum for the highlighted row?
Anybody help
Regards Pete
 
Whats the database back-end ?
You could use an SQL Command with the SUM(Field1) function to acquire the total.
Alternatively (but not ideal) you could run code like :

tpRunTotal := 0;
with Table1 do
begin
First;
while not EOF do
begin
tpRunTotal := tpRunTotal + Table1Field1.AsFloat;
Next;
end; // while not EOF
end; // with Table1 do

Hope that this helps.
Steve
 
Thanx for the quick reply, will try.

The back end id access DB 97, will this sum a column at run time and live?

so if i have this data in a DBGrid

name age

bob 12
sarah 14
ted 92

the result will be = 118
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top