Ms访问表单关闭请求保存是否取消



我已经把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

如果数据改变了,那么上面的代码工作正常,然后是保存&关闭,不撤销&关闭和取消可取消事件并保留在窗体上但是当数据不变时,Yes按钮工作正常,但是NO按钮不关闭表单

你的问题太模糊了,因为你只是说"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

On Me.Undo from Allen Browne:

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

相关内容

  • 没有找到相关文章

最新更新