ms 访问表单关闭询问保存 yesnocancel

Posted

技术标签:

【中文标题】ms 访问表单关闭询问保存 yesnocancel【英文标题】:ms access form closing ask save yesnocancel 【发布时间】:2014-06-07 12:38:45 【问题描述】:

我已将 VBA 放入 ms access 2010 中的 Unload Form Event 中

Private Sub Form_Unload(Cancel As Integer)

Dim strMsg As String
   Dim iResponse As Integer

   ' Specify the message to display.
   strMsg = "Do you wish to save the changes?" & Chr(10)
   strMsg = strMsg & "Click Yes to Save or No to Discard changes."

   ' Display the message box.
   iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")

   ' Check the user's response.
   If iResponse = vbYes Then

      ' Undo the change.
      DoCmd.RunCommand acCmdSave
   End If

   If iResponse = vbNo Then


      ' Undo the change.
      DoCmd.RunCommand acCmdUndo
      End If



   If iResponse = vbCancel Then

      ' Undo the change
   Cancel = True


   End If



End Sub

如果数据被更改,那么上面的代码工作正常,然后是保存和关闭,否撤消和关闭并取消取消事件并保留在表单上 但是当数据不变时,是按钮工作正常,但否按钮不关闭表单

我在哪里弄错了?

【问题讨论】:

在调试器中单步执行。 问题应该在funcionacCmdUndo尝试调试它。 如果没有任何变化,那么根本没有理由提示用户。只需在您的第一条语句之前添加:If Not Me.Dirty Then Exit Sub End If 但是当使用 me.dirty 时下面的代码不起作用 【参考方案1】:

您的问题太模糊了,因为您只是说“NO 按钮不关闭表单”。

你可以这样做:

DoCmd.Close

看这里:

Private Sub cmdCloseForm_Click() 
On Error GoTo Err_cmdCloseForm_Click 

 DoCmd.RunCommand acCmdUndo  'OR Me.Undo - test which works best for your situation
 DoCmd.Close 

Exit_cmdCloseForm_Click: 
 Exit Sub 

Err_cmdCloseForm_Click: 
 MsgBox Err.Description 
 Resume Exit_cmdCloseForm_Click 

End Sub

来自 Allen Browne 的 Me.Undo

Me.Undo cancels the edits in a specific form, so that it is no longer dirty. Once the form is no longer dirty, no further undo is possible, because it has reached the desired state. 但是,我。表示焦点所在的表单,因此您的表单必须具有焦点才能执行Me.Undo 命令。

【讨论】:

以上是关于ms 访问表单关闭询问保存 yesnocancel的主要内容,如果未能解决你的问题,请参考以下文章

MS Access - 使用子表单过滤时禁止保存提示

MS Access 将报告导出为 pdf

MS Access 2016:CTRL+A 保存表单?

关闭事件处理程序 C#

如何使用 VBA 在 ms 访问表单中创建自定义自动编号? [关闭]

MS Access DAO 连接在退出时放弃更改