Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread How to assign an event an runtime
Tue, Oct 3 2017 5:08 AMPermanent Link

Paul Coshott

Avatar

Hi All,

I am using the default Database instance, and need to assign "AfterCommit" and "OnCommitError" event handlers at runtime.

Can anyone show me some example code on how to do this?

Thanks heaps,
Paul
Tue, Oct 3 2017 7:34 AMPermanent Link

Walter Matte

Tactical Business Corporation


Hope this gets you going in the right direction ... not tested.

If you are using AutoTransactions := False  and are doing your won StartTransaction / Commit then you can easily control when you assign these events.

If you are using AutoTransactions := True then you will need to decide where to assign the events.

In my code I an unassigning the events ... but there are more places to unassign.... AfterRollback and OnRollbackError....

If you add a TDatabase to your unit you can see the events on the TDatabase unit and what arguments are need for each event....


Private
 procedure MyLocalAfterCommit(Sender: TObject);
 procedure MyLocalCommitError(Sender: TObject; const ErrorMsg: String);



procedure TfrmForm1.MyLocalAfterCommit(Sender: TObject);
begin

// Code Here

 Database.AfterCommit := nil;
 Database.OnCommitError := nil;

end;

procedure TfrmForm1.MyLocalCommitError(Sender: TObject; const ErrorMsg: String);
begin

// Code here

 Database.AfterCommit := nil;
 Database.OnCommitError := nil;
 
end;


Procedure TfrmForm1.Button1Click(Sender : TObject);
begin
 Database.AfterCommit := .MyLocalAfterCommit;
 Database.OnCommitError := MyLocalCommitError;

 Database.Commit;

end;


Walter
Tue, Oct 3 2017 11:01 PMPermanent Link

Paul Coshott

Avatar

Hi Walter,

Excellent, thanks for the help.

Cheers,
Paul
Image