简体   繁体   中英

Delphi and ADODataset.Cancel

I'm using Dephi 10.1 Berlin and Access 2013.

My problem is related to TADODataSet.Cancel() .

I want to show my user a message box asking for a confirmation before posting, in case data has been modified.

In the TADODataSet.BeforePost event, I added the following code:

if Application.MessageBox('Save changes?', '', 52) = idNo then
    ADODataSet1.Cancel;

If the user click on btnNo , something unexpected happens.

Changes are canceled from the current record, but a new record with all fields empty is created.

The only field with some data is the one that was previously modified by the user.

If I cancel the modification via the cancel button of TDBNavigator , everything is fine.

If I simulate a click of the Cancel button of the TDBNnavigator in the BeforePost event:

if Application.MessageBox('Save changes?', '', 52) = idNo then
  DBNavigator1.BtnClick(nbCancel);

I have the same behaviour, so a new empty record is created.

Any suggestion?

The help for TADODataSet.BeforePost says in part:

call Abort to cancel the Post operation (Delphi) or throw an exception (C++).

So:

if Application.MessageBox('Save changes?', '', 52) = idNo then
    abort;

Note this is meant for preventing changes that don't pass validation (the common use for BeforePost) from being posted. It doesn't reset the edit buffers like cancel does. Usually that is a separate function in the UI so the user doesn't have to reenter all the changed data in the edits each time posting is rejected by calling abort in BeforePost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM