Hi friends, I need help with counting rows in database table.
The result should appear in the Edit.
for example:
Col1 = AsString
ROW1 = 345,50
ROW2 = 28,90
row3 = 13,60
row
.
.
row
rowx = 1,08
Code:
TForm1.Button1Click procedure (Sender: TObject);
boiling
i: integer
y: double;
begin
for i: = -1 to Table1.DataSet.RecordCount -1
begin
y: = Calculate (Table1.Rows);
end;
Edit1.Text: = FloatToStr(y);
end;
Sorry, I can't see your problem. Or better said: I can see you have a lot of problems with understanding Delphi.
I think you have put a table component on your form which represents the contents of the table in your database. I don't know which table component has got a property named
dataset ... What is the name of your database components?
To count means to find out how much rows a table has. You already know the property RecordCount, it reprsents the count of rows.
When you try to make a loop through all records in a table, never use RecordCount, because it only shows you the count of the already fetched records. Better you ask for "End of file":
Delphi-Quellcode:
Dataset.First;
WHILE NOT Dataset.Eof DO
BEGIN
{do something}
Dataset.Next;
END;
But I think you don't want to count rows. You want to calculate a sum or compute average. For the last posibility you first have to calculate the sum of the fields and store it in a local variable. After ending the loop you divide the sum with the count of records as divisor:
Delphi-Quellcode:
PROCEDURE MakeAverage;
VAR
MyAverage,
MySum : Currency;
MyCount : Integer;
BEGIN
MyCount := 0;
MySum := 0;
Dataset.First;
WHILE NOT Dataset.Eof DO
BEGIN
INC(MyCount);
MySum := MySum + Dataset.FieldByName('SumField').AsCurrency;
Dataset.Next;
END;
MyAverage := MySum / MyCount;
END;