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

时间:2012-05-30 14:56:18

标签: ms-access vba access-vba

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

3 个答案:

答案 0 :(得分:12)

您可以使用卸载事件:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用“Form_BeforeUpdate(取消为整数)”事件并将取消设为True。

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