Recent

Author Topic: Query + calculated field  (Read 4000 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2092
    • Lazarus and Free Pascal italian community
Query + calculated field
« on: January 09, 2012, 09:05:43 am »
Hello guys, I have a query with a calculated field (product) which is the product of the fields field1 and field2.
This query I want to show it in a DBGrid and if I change the values ​​of the cell that you want to update the value of the product. How do I manage this calculated field?
Code: [Select]
select campo1, campo2, (campo1*campo2) as prodotto FROM Tabella;
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Query + calculated field
« Reply #1 on: January 09, 2012, 10:34:37 am »
To do this you need to use calculated fields in the dataset. Change your query to
Code: [Select]
select campo1, campo2 FROM Tabella;
double click on the TSQLQuery and add a calculated field named 'prodotto', type Float (don't forget to add the 2 existing fields campo1, campo2 if they aren't already defined. Click on the + sign to get them from the fielddefs.). In TSQLQuery.OnCalcFields add
Code: [Select]
SQLQuery1.FieldByName('prodotto').AsFloat:=SQLQuery1.FieldByName('campo1').AsFloat * SQLQuery1.FieldByName('campo2 ').AsFloat

xinyiman

  • Hero Member
  • *****
  • Posts: 2092
    • Lazarus and Free Pascal italian community
Re: Query + calculated field
« Reply #2 on: January 09, 2012, 11:06:54 am »
Thank you  ;)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018