【问题标题】:How to cancel a form close in Close Event?如何取消关闭事件中的表单关闭?
【发布时间】:2012-05-30 14:56:18
【问题描述】:

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

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    您可以使用 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

    【讨论】:

    • 我不确定我是否理解,我应该在关闭事件中向 Form_Unload 事件传递一些东西吗?
    • 哦,我明白了,把测试放在 Form_unload 中,然后设置 Cancel = True。 msdn.microsoft.com/en-us/library/aa211464%28v=office.11%29.aspx
    • 我已经添加了很多关于如何使用它的注释。
    • 真正让我困惑的是Cancel As Integer。我希望那里有一个布尔值。但我猜 VBA 会自动将 TRUE 和 FALSE 转换为 1 和 0?
    • 这会使表单在 Unload 中显示我的 msgbox 之前闪烁。当关闭 msgbox 时,我得到一个运行时错误,“没有当前记录。”。通常,您的表格上有记录。 ;) 那么你知道如何解决这个问题吗?
    【解决方案2】:

    学习并尝试这段代码,它对我有用。用您选择的名称替换必要的变量名称。将代码粘贴到表单的 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
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-04
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2014-06-17
        • 1970-01-01
        • 2019-01-12
        • 1970-01-01
        相关资源
        最近更新 更多