Reputation: 97
I want to calculate all fields and add a total field. How can I do this? I did this with sql command, but I don't know how to update dbgrid
every time.
My code:
procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text := 'select names,big1,small1,black1,big2,small2,big3,'+
'((big1+small1+black1+big2+small2+big3)*0.35) as total from adlar7v';
ADOQuery1.Open;
end;
when I run this code the first time it works, but the second time the changes don't appear in the grid.
Second method:
I did this with the TAdoQuery.OnCalcField
event but there is a problem.
I know how to calculate a field but I don't know how to multiply all field after addition
my code:
ADOQuery1.FieldValues['total'] :=
ADOQuery1.FieldValues['big1'] + ADOQuery1.FieldValues['small1'] +
ADOQuery1.FieldValues['black1'] + ADOQuery1.FieldValues['big2'] +
ADOQuery1.FieldValues['small2'] + ADOQuery1.FieldValues['big3'];
ADOQuery1.FieldValues['cemi']:=((ADOQuery1.FieldValues['boyuk1'] + ADOQuery1.FieldValues['boyuk2'] + ADOQuery1.FieldValues['boyuk3'])*0.35)+((ADOQuery1.FieldValues['kicik1'] + ADOQuery1.FieldValues['kicik2'])*0.25)+(ADOQuery1.FieldValues['qara1']*0.30);
Hi I want to post calc field(cemi) to table (sql). when I calc all field the last field doesn't post on sql table. because last field (cemi) type fkcalc how can I post fkcalc type field Thanks in advance!
Upvotes: 0
Views: 311
Reputation: 30715
when I run this code first time working second time changes doesn't appear db grid
Use the Object Inspector to set up an AfterPost handler on your AdoQuery1:
procedure TForm1.ADOQuery1AfterPost(DataSet: TDataSet);
begin
AdoQuery1.Refresh;
end;
Then, after you save a change to one of the numeric columns in the grid, the AdoQuery1.Refresh will cause the data to be retrieved again from the server, and that will cause the server to recalculate the "Total" column. The fact that it isn't doing that at the moment is why the Total column isn't updating.
Btw, if I've understood what you are asking about that correctly, then your second point
I know how to calculate field but don't know how to multiplication(*) all field after (+)
Use the following code for this
ADOQuery1.FieldValues['total'] := (
ADOQuery1.FieldValues['big1'] + ADOQuery1.FieldValues['small1'] +
ADOQuery1.FieldValues['black1'] + ADOQuery1.FieldValues['big2'] +
ADOQuery1.FieldValues['small2'] + ADOQuery1.FieldValues['big3'] )
* 0.35;
Upvotes: 3