How to find out which field was changed in a delphi 6 TZTable via ZeosLib

2.4k Views Asked by At

I have a TZTable (ZEOSlib) bound to a DBGrid now I need to know which particular TField was changed by the user.

I tried with

if NOT (taPositionenArtNrGH.NewValue = taPositionenArtNrGH.OldValue) then
    ShowMessage('ArticleNumber changed');

I placed the code in

  • BeforePost, OnUpdateRecord, AfterPost

But in the Debugger OldValue is always NewValue. How do I check which field was changed?

3

There are 3 best solutions below

0
On BEST ANSWER

You can use UpdateStatus : TUpdateStatus for this. For example:

  1. Set ZTable.CachedUpdates to true;
  2. Create new calculated field named "Status".
  3. To show old value for example of field "FNAME" create new calculate field named "FNameOldValue"
  4. In OnCalcFields event use:

    procedure TDM1.ZTable1CalcFields(DataSet: TDataSet);
    begin
      if ZTable1.UpdateStatus in [usModified] then
        begin
          ZTable1Status.value := 'Modified';
          ZTable1FNameOldValue.value := ZTable1FNAME.OldValue;
        end
      else
        ZTable1Status.value := 'UnModified'
    end;
    

Result :

enter image description here

Edit:

You can detect field level changes like:

if ZTable1.UpdateStatus in [usModified] then
  begin
    for I := 0 to ZTable1.Fields.Count - 1 do
      begin
        if ZTable1.Fields[i].OldValue <> ZTable1.Fields[i].NewValue  then
          -- do something with this field
      end;
   end; 
5
On

As per documentation:

The NewValue property is only usable when the data is accessed using a TClientDataSet component or cached updates is enabled.

1
On

If you just want to know what fields have been changed, why not use TField.OnChange event? You could fill a list of field names in this event and clear it in OnAfterPost. But the Modified property would be very useful indeed; it's odd that it haven't been implemented yet.