Form. Unload 事件 (Access)Form.Unload event (Access)

Unload 事件发生在窗体关闭之后,但从屏幕上删除之前。The Unload event occurs after a form is closed but before it's removed from the screen. 当重新加载窗体时,Microsoft Access 将重新显示窗体并重新初始化其中所有控件的内容。When the form is reloaded, Microsoft Access redisplays the form and reinitializes the contents of all its controls.

语法Syntax

表达式Unload(取消)expression.Unload (Cancel)

expression:表示 Form 对象的变量。expression A variable that represents a Form object.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
CancelCancel 必需Required IntegerInteger 设置为 True 以取消 卸载 事件。Set to True to cancel the Unload event.

备注Remarks

若要在这些事件发生时运行宏或事件过程,请将 OnUnload 属性设为宏的名称或 [事件过程]。To run a macro or event procedure when these events occur, set the OnUnload property to the name of the macro or to [Event Procedure].

如以下的用户操作将引起 Unload 事件:The Unload event is caused by user actions such as:

  • 关闭窗体。Closing the form.
  • 在宏中运行 Close 操作。Running the Close action in a macro.
  • 通过右键单击应用程序的任务栏按钮, 然后选择 "关闭" 来退出应用程序。Quitting an application by right-clicking the application's taskbar button, and then choosing Close.
  • 在应用程序运行时退出 Windows。Quitting Windows while an application is running.

通过在窗体的 Unload 事件发生时运行宏或事件过程,可以验证窗体应被卸载或指定在窗体卸载时应该发生的操作。By running a macro or an event procedure when a form's Unload event occurs, you can verify that the form should be unloaded or specify actions that should take place when the form is unloaded. 您也可以打开另一个窗体或显示一个要求输入用户名的对话框,以创建指出窗体使用者的日志项。You can also open another form or display a dialog box requesting the user's name to make a log entry indicating who used the form.

当关闭窗体时,下列事件将按如下顺序发生:When you close a form, the following events occur in this order:

卸载停用关闭UnloadDeactivateClose

Unload 事件发生在 Close 事件之前。The Unload event occurs before the Close event. Unload 事件可以取消,但 Close 事件不能取消。The Unload event can be canceled, but the Close event can't.

备注

当您为与Unload事件相关的事件 (如DeactivateLostFocus) 创建宏或事件过程时, 请确保它们不会发生冲突 (例如, 确保不会导致一个宏发生问题或过程在另一个过程中被取消), 并且它们不会导致级联事件发生。When you create macros or event procedures for events related to the Unload event, such as Deactivate and LostFocus, be sure that they don't conflict (for example, make sure that you don't cause something to happen in one macro or procedure that is canceled in another) and that they don't cause cascading events.

示例Example

该示例将提示用户确认窗体是否应该关闭。This example prompts the user to verify that the form should close.

若要试用该示例,请将下列事件过程添加到窗体中。To try the example, add the following event procedure to a form. 在 "窗体" 视图中, 关闭窗体以显示对话框, 然后选择 "是" 或 " "。In Form view, close the form to display the dialog box, and then choose Yes or No.

Private Sub Form_Unload(Cancel As Integer) 
 If MsgBox("Close form?", vbYesNo) = vbYes Then 
 Exit Sub 
 Else 
 Cancel = True 
 End If 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.