AnyDAC
ContentsIndexHome
PreviousUpNext
TADDataSet.OnUpdateRecord Event

Occurs when record update is applying to a database.

Group
Links

Use an OnUpdateRecord event handler to process immediate or cached updates that cannot be correctly or easily handled:

This event is useful for applications that require:

  • additional control over parameters and macros substitution in update components;
  • multi SQL statement updates, when DBMS does not support batches or blocks;
  • non-SQL updates posting;
  • etc

ASender is the dataset to which updates are applied. 

ARequest indicates whether the current update is the insertion, deletion or modification of a record. 

AAction indicates the action taken by the OnUpdateRecord handler before it exits. On entry into the handler, AAction is always set to eaDefault. If OnUpdateRecord is successful, it should set AAction to eaApplied before exiting.

AAction value 
Description 
eaFail 
Mark the update as failed and return an error. 
eaSkip 
Skip current update and do not mark it as applied. 
eaRetry 
Retry the current operation. 
eaApplied 
Mark current update as applied. 
eaDefault 
Take default action. For successful update it is eaApplied, for failed update it is eaFail. 
eaExitSuccess 
Stop to process the cached updates, return success. 
eaExitFailure 
Stop to process the cached updates, return failure. 

The OnUpdateRecord event handler code should read the dataset field values, including TField.NewValue, OldValue and CurValue. There:

  • NewValue is the new field value before posting updates.
  • OldValue is the original field value, as it was after record fetching or after last updates applying.
  • CurValue is the current field value, the same as Value property.

Note, the OnUpdateRecord handler code must not call the methods that change the current dataset position.

property OnUpdateRecord: TADUpdateRecordEvent;
procedure TForm1.ADQuery1UpdateRecord(ASender: TDataSet; ARequest: TADUpdateRequest;
 var AAction: TADErrorAction; AOptions: TADUpdateRowOptions);
begin
  if ARequest = arInsert then begin
    // set the SQL command to insert new record
    ADQuery2.SQL := 'insert into mytab (id, code, name) values (:id, :code, :name) returning tmstamp into :ts';

    // set parameter values
    ADQuery2.Params[0].Value := ASender['id'];
    ADQuery2.Params[1].Value := ASender['code'];
    ADQuery2.Params[2].Value := ASender['name'];

    // specially define TS parameter
    ADQuery2.Params[3].DataType := ftDateTime;
    ADQuery2.Params[3].ParamType := ptOutput;

    // insert new record
    ADQuery2.ExecSQL;

    // move TS output parameter value back to dataset
    ASender['tmstamp'] := ADQuery2.Params[3].Value;

    // return 'Ok' status
    AAction := eaApplied;
  end;
end;

See demos:

What do you think about this topic? Send feedback!