Page 1 of 1

Executing OnCalcFields without refreshing non-calced-fields

Posted: Sun 24 Jul 2011 09:21
by BigAl66
Hi,

I try to recalc all fields of a TMSQuery without refreshing the data from the database server. The background is, that I have a field, that is dependet from the current time. Example:

DB-Field 1: TStart: TDateTime;
DB-Field 2: TStop: TDateTime;
Calc-Field: Duration: TDateTime;

In the OnCalc-Event the Duration will be calced as follows ():

Code: Select all

if not TStop.IsNull then
  Duration.AsDateTime := TStop.AsDateTime - TStart.AsDateTime
else
  Duration.AsDateTime := Now - TStart.AsDateTime;
Calling a Refresh of the query will update all fields but will allways execute also a complete refresh to the SQL-Server... My data is displayed in a TDBCtrlGrid. Therefore I need a dataaware component to display the field...

Any idea?

Alex

Posted: Mon 25 Jul 2011 12:06
by AndreyZ
Hello,

The TMSQuery component inherits calculated fields' functionality from the TDataSet component. There is the GetCalcFields protected virtual method in TDataSet that refreshes calculated fields. You can create your own query class inherited from TMSQuery, override this method, and make it public. After this you will be able to refresh calculated fields without refreshing data from the server by calling the GetCalcFields method.

Posted: Mon 25 Jul 2011 16:59
by BigAl66
Hi Andrey,

thank you very much for you reponse. I will try your solution.

Best Regards

Alex

Posted: Tue 26 Jul 2011 06:02
by BigAl66
Hi Andrey,

again. I tried it and it works :D - but also just calling Resync([]) do work? The calculated fields are updated and no access to the database is monitored. Is there any reason why not use just Resync?

Regards

Alex

Posted: Tue 26 Jul 2011 10:45
by AndreyZ
You can use both variants.