ms访问表单关闭请求保存yesnocancel

时间:2014-06-07 12:38:46

标签: vba ms-access access-vba ms-access-2007 ms-access-2010

我已将VBA放入ms access 2010中的卸载表单事件

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

如果数据被更改,那么上面的代码工作正常,然后是保存&关闭,不撤消&关闭并取消取消活动并保留在表格上 但是当数据未更改时,“是”按钮工作正常,但“否”按钮不会关闭表单

我错误的地方?

1 个答案:

答案 0 :(得分:3)

你的问题太模糊了,因为你只是声明"没有按钮不会关闭表格。"

你可以这样做:

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命令。