【发布时间】:2012-05-30 14:56:18
【问题描述】:
我确定这很简单,但我找不到。在访问表单的关闭事件中,如何取消关闭表单?我有一个计算表中记录的测试。如果该表有记录,我想询问用户是否要关闭或返回并使用它们。那么如何取消关闭事件呢?
【问题讨论】:
我确定这很简单,但我找不到。在访问表单的关闭事件中,如何取消关闭表单?我有一个计算表中记录的测试。如果该表有记录,我想询问用户是否要关闭或返回并使用它们。那么如何取消关闭事件呢?
【问题讨论】:
您可以使用 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
【讨论】:
Cancel As Integer。我希望那里有一个布尔值。但我猜 VBA 会自动将 TRUE 和 FALSE 转换为 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
【讨论】:
使用“Form_BeforeUpdate(cancel as integer)”事件并将cancel设置为True。
请注意,除非您添加一些逻辑以实际允许更新数据库,否则您根本无法关闭。
【讨论】: