访问VBA - 防止关闭表单

时间:2016-04-24 12:04:03

标签: vba ms-access access-vba

我在Before_Update事件中有一个代码,用于保存更改。为此,我使用带有YesNoCancel提示的Msgbox,但是当我点击“取消”按钮时,我无法阻止关闭表单。以下是我的代码的简短示例:

Option Compare Database
Public SomeVariable As Integer

    Private Sub Form_BeforeUpdate(Cancel As Integer)

             If MsgBox("There has been done some changes. You wish to save these changes ?", vbQuestion + vbYesNoCancel, "Save changes") = vbYes Then

              'do nothing and Access saves automatically

             ElseIf vbNo Then
              DoCmd.RunCommand acCmdUndo

             ElseIf vbCancel Then
              SomeVariable = 1
             End If

     End Sub

Private Sub Form_Unload(Cancel As Integer)

If SomeVariable = 1 Then
 SomeVariable=0
 Cancel = True
End If

End Sub

有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您必须保存MsgBox的返回值,然后相应地执行:

        Dim vbAnswer
        vbAnswer= MsgBox("There has been done some changes. You wish to save these changes ?", vbQuestion + vbYesNoCancel, "Save changes")

        if vbAnswer = vbYes Then

          'do nothing and Access saves automatically

         ElseIf vbAnswer= vbNo Then
               ....
         ElseIf vbAnswer= vbCancel Then