防止关闭按钮将记录保存在MS Access中 [英] Preventing close buttons from saving records in MS Access

查看:77
本文介绍了防止关闭按钮将记录保存在MS Access中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Microsoft Access表单中,只要当前记录发生更改,绑定控件中的任何更改都将以静默方式保存到数据库表中.很好,但是我不希望在用户关闭表单时发生这种情况,因为它与许多人的期望正好相反.

In a Microsoft Access form, whenever the current record changes, any changes in bound controls are silently saved to the database tables. This is fine, but I don't want it to happen when a user closes a form, because it is the direct opposite of what many people would expect.

最好的例子是,当您尝试关闭具有未保存更改的excel文件时,它询问是否应放弃更改.这正是我要在Access中实现的目标,但是找不到在VBA中捕获关闭按钮事件的任何方法.

The best example is when you try to close an excel file with unsaved changes, it asks whether the changes should be discarded. This is exactly what I'm trying to achieve in Access, but can't find any way to trap the close button's event in VBA.

该表单的Unload事件是当某人单击关闭按钮时触发的第一个事件,但此时更改已被写入数据库.

The form's Unload event is the first event that is triggered when someone clicks the close button, but by then the changes are already written to the database.

这是否完全可能,还是我必须创建自己的关闭按钮?我为这种琐碎的事情编写大量代码感到很自在,但我讨厌必须弄乱GUI.

Is this at all possible, or do I have to create my own close buttons? I'm comfortable with writing large amounts of code for trivial things like this but I hate having to clutter the GUI.

推荐答案

您必须使用Form_BeforeUpdate事件.下面是一个例子;但是,它的确会创建一条典型的警告消息:您目前无法保存此记录.MicrosoftAccess在尝试保存记录时可能遇到错误...."-取决于您的数据库设置.您可以在下面使用简单的解决方法来避免显示该消息.

You have to work with Form_BeforeUpdate event. Below is an example; however it does create a typical warning message: "You can't save this record at this time. Microsoft Access may have encountered an error while trying to save a record. ..." - depending on your database settings. You can use simple workaround below to avoid displaying of that message.

Private Sub Form_BeforeUpdate(Cancel As Integer)
   Cancel = True
   'Or even better you can check certain fields here (If Then...)

End Sub


Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 2169 Then 
        Response = True
    End If
End Sub

这篇关于防止关闭按钮将记录保存在MS Access中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆