haci aliyev haci aliyev - 6 months ago 15
SQL Question

How can I calculate all field and post (SQL/or onCalcFields)

I want to calculate all fields and to total field. How can I do this. I did this with sql command. but I don't know how to update dbgrid everytime. 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 first time working second time changes doesn't appear db grid.

Second method:

I did this with adoquery oncalcfield event but there is a problem. I know how to calculate field but don't know how to multiplication(*) all field after (+)

my codes:

ADOQuery1.FieldValues['total'] :=
ADOQuery1.FieldValues['big1'] + ADOQuery1.FieldValues['small1'] +
ADOQuery1.FieldValues['black1'] + ADOQuery1.FieldValues['big2'] +
ADOQuery1.FieldValues['small2'] + ADOQuery1.FieldValues['big3'];


sorry for bad English

Answer

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 (+)

is a different technical point which needs to be in a separate, new question, and you'll need to explain a bit more about what it is that you are having difficulty doing.