Recent

Author Topic: Вычисляемое поле SQLQuery1  (Read 2641 times)

maxika

  • Newbie
  • Posts: 2
Вычисляемое поле SQLQuery1
« on: September 08, 2018, 06:35:34 pm »
Hello, tell me please how to make an automatic calculation of the calculated field SQLQuery1, when changing another numeric field.
When I do this:

procedure TForm1.SQLQuery1Поле1Change(Sender: TField);
begin
SQLQuery1.FieldByName('ВычислПоле') .AsInteger:=SQLQuery1.FieldByName(' Поле1').AsInteger * 2;
end;


The auto save function stops working:

procedure TForm1.SQLQuery1AfterPost(DataSet: TDataSet);
begin
SQLQuery1.ApplyUpdates;
end;


Please tell me how to do it correctly.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Вычисляемое поле SQLQuery1
« Reply #1 on: September 08, 2018, 08:18:26 pm »
Shouldn't ApplyUpdates be called much earlier? IIRC, in AfterPost the dataset is already updated. Maybe you should try to set the calculated field(s) and call ApplyUpdates in an OnBeforePost handler?
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

maxika

  • Newbie
  • Posts: 2
Re: Вычисляемое поле SQLQuery1
« Reply #2 on: September 09, 2018, 04:24:14 pm »
thank you very much for the help
the following methods create an auto-calculation field and autosave data

procedure TForm1.SQLQuery1BeforeScroll(DataSet: TDataSet);
begin
  SQLQuery1.Edit;
  SQLQuery1.FieldByName('AAA').AsInteger:=SQLQuery1.FieldByName('BBB').AsInteger*2;
  SQLQuery1.Post;
end; 

procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TDBNavButtonType);
begin
  if Button = nbPost then
   begin
     SQLQuery1.Edit;
     SQLQuery1.FieldByName('AAA').AsInteger:=SQLQuery1.FieldByName('BBB').AsInteger*2;
     SQLQuery1.Post;
   end;
end;   

procedure TForm1.SQLQuery1AfterPost(DataSet: TDataSet);
begin
  SQLQuery1.ApplyUpdates;
end;   

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Вычисляемое поле SQLQuery1
« Reply #3 on: September 10, 2018, 12:08:04 am »
Usually for calculated fields (TFieldKind = fkCalculated) they use OnCalcFields.

But seems like you're using regular fields, so:

don't (ever) use for such purpose

BeforeScroll
DBNavigatorClick

use

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SQLQuery1BeforePost(DataSet: TDataSet);
  2. begin
  3.   SQLQuery1.FieldByName('AAA').AsInteger := SQLQuery1.FieldByName('BBB').AsInteger*2;
  4. end;  
  5.  
  6. procedure TForm1.SQLQuery1AfterPost(DataSet: TDataSet);
  7. begin
  8.   SQLQuery1.ApplyUpdates;
  9. end;  
  10.  


p.s. Заголовки лучше писать по Английски.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

 

TinyPortal © 2005-2018