vba 如何在关闭事件中取消表单关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10818836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:14:57  来源:igfitidea点击:

How to cancel a form close in Close Event?

ms-accessvbaaccess-vba

提问by MAW74656

I'm sure this is very simple, but I can't find it. In the close event of an Access Form, how can I cancel closing the form? I have a test that counts the records in a table. If that table has records, I want to ask the user if they want to close or go back and work with them. So how do I cancel the close event?

我确定这很简单,但我找不到。在访问表单关闭事件中,如何取消关闭表单?我有一个测试来计算表中的记录。如果该表有记录,我想询问用户是否要关闭或返回并使用它们。那么如何取消关闭事件呢?

回答by Fionnuala

You can use the Unload event:

您可以使用 Unload 事件:

GlobalVar ButtonClicked

Private Sub Form_Open(Cancel As Integer)
     ButtonClicked = False
End Sub

Private ClickMe_Click(Cancel As Integer)
     ButtonClicked = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
     If Not ButtonClicked Then
         Cancel = True
     End if
End Sub  

Order of events for database objects

数据库对象的事件顺序

回答by user28864

Study and try this code, it worked for me. Replace necessary variable names with your chosen names. Paste the code in the form_unload Event of your form. WARNING!!!: After you perform this operation you will find it difficult to access your form in design and layout view

学习并尝试此代码,它对我有用。用您选择的名称替换必要的变量名称。将代码粘贴到表单的 form_unload 事件中。警告!!!:执行此操作后,您会发现很难在设计和布局视图中访问您的表单

    Private Sub Form_Unload(Cancel As Integer)
        userresponse = MsgBox("Are you sure you want close? All your work wouldn't be saved", vbYesNo, "Database Information")
        Select Case userresponse
        Case 6
            Cancel = False
            'this line opens another form in my own case
            DoCmd.OpenForm "EngMenu"  

        Case 7
            Cancel = True
            'this line keeps my own form open in my own case
            DoCmd.OpenForm "UpdateForm"


        Case Else:

            MsgBox "You are not allowed to perform this operation", vbInformation, "Database Information"
        End Select
    End Subenter code here

回答by rogsonl

Use the "Form_BeforeUpdate(cancel as integer)" event and set cancel to True.

使用“Form_BeforeUpdate(cancel as integer)”事件并将取消设置为True。

Notice that you simply will not be able to close at all unless you add some logic to actually allow updating the database.

请注意,除非您添加一些逻辑以实际允许更新数据库,否则您根本无法关闭。