简体   繁体   English

Delphi 和 ADODataset.Cancel

[英]Delphi and ADODataset.Cancel

I'm using Dephi 10.1 Berlin and Access 2013.我正在使用 Dephi 10.1 Berlin 和 Access 2013。

My problem is related to TADODataSet.Cancel() .我的问题与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:TADODataSet.BeforePost事件中,我添加了以下代码:

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

If the user click on btnNo , something unexpected happens.如果用户单击btnNobtnNo发生意外情况。

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.如果我通过TDBNavigator的取消按钮取消修改,则一切正常。

If I simulate a click of the Cancel button of the TDBNnavigator in the BeforePost event:如果我在BeforePost事件中模拟单击TDBNnavigator的取消按钮:

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: TADODataSet.BeforePost 的帮助部分说明:

call Abort to cancel the Post operation (Delphi) or throw an exception (C++).调用 Abort 取消 Post 操作 (Delphi) 或抛出异常 (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.请注意,这是为了防止发布未通过验证(BeforePost 的常见用途)的更改。 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.通常,这是 UI 中的一个单独功能,因此用户不必在每次通过在 BeforePost 中调用 abort 拒绝发布时重新输入编辑中的所有更改数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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