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!

DBGrid - Summing a row 1

Status
Not open for further replies.

hbez

Instructor
Mar 25, 2003
49
0
0
ZA
I have an ADOQuery populating a DBGrid. I'm trying to do
for n := 0 to qry1.RecordCount-1 //200 records
begin
Total := Total + qry1.FieldValues['TAX'];
Next;
end;
This simply adds the first record in the query 200 times!
Could someone pse tell me what I'm doing wrong?

Also, if I don't want to sum using qry1, how could I add the values in column 5 (200 rows) of the DBGrid?

Hannes
 
Apologies, that caption should read DBGrid - summing a column
 
Got it right, just forgot a few things :)

qry1.First;
Then do the for loop and setting qry1.Next at the appropriate place.
 
You could also get the total tax using SQL function SUM(), something like this:
Code:
  qry1.SQL.Clear;
  qry1.SQL.Add('SELECT SUM(Tax) as TaxTot FROM mytbl');
  qry1.Open;
  if qry1.FindField('TaxTot').AsInteger > 0 then
    Total:= qry1.FieldValues['TaxTot'];
I've been trying to train myself to let SQL do more of the work. You should too. :)

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top