Table of contents
TOC
折叠目录
展开目录

Form.Open 事件 (访问)Form.Open Event (Access)

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

当窗体已打开,但第一条记录尚未显示时, Open事件发生。The Open event occurs when a form is opened, but before the first record is displayed.

语法Syntax

expression.expression.打开(* 取消,)Open( ** Cancel*, )

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

参数Parameters

名称Name必需/可选Required/Optional数据类型Data Type说明Description
CancelCancel必需RequiredIntegerInteger该设置确定是否打开窗体或报表将出现。The setting determines if the opening of the form or report occurs.将 Cancel 参数设置为 True (? 1) 取消打开窗体或报表。Setting the Cancel argument to True (?1) cancels the opening of the form or report.

注解Remarks

通过在窗体的Open事件发生时运行宏或事件过程,可以关闭另一个窗口或将焦点移到窗体上的特定控件。By running a macro or an event procedure when a form's Open event occurs, you can close another window or move the focus to a particular control on a form.您也可以运行宏或事件过程,询问有关信息需要在窗体之前或报表被打开或打印。You can also run a macro or an event procedure that asks for information needed before the form or report is opened or printed.

例如,打开的宏或事件过程可以打开一个自定义对话框,用户在其中输入条件以筛选要显示在窗体或报表中包含的日期范围的记录集。For example, an Open macro or event procedure can open a custom dialog box in which the user enters the criteria to filter the set of records to display on a form or the date range to include for a report.

激活已经打开的窗体时, Open事件不会发生?The Open event doesn't occur when you activate a form that's already open ?例如,当您从 Microsoft Access 中的另一个窗口中切换到窗体或宏中使用 OpenForm 操作将打开窗体的顶部。for example, when you switch to the form from another window in Microsoft Access or use the OpenForm action in a macro to bring the open form to the top.但是,在以下情况下会激活事件。However, the Activate event does occur in these situations.

当打开一个基于基础查询的窗体时,Microsoft Access 先运行该窗体的基础查询,然后才运行 Open 宏或事件过程。When you open a form based on an underlying query, Microsoft Access runs the underlying query for the form before it runs the Open macro or event procedure.

如果您的应用程序可以有多个窗体加载一次,使用而不是打开事件的激活停用事件来显示和隐藏自定义工具栏,当焦点移动到另一种形式。If your application can have more than one form loaded at a time, use the Activate and Deactivate events instead of the Open event to display and hide custom toolbars when the focus moves to a different form.

Open 事件发生在Load事件中,当窗体打开并且显示其中的记录时触发之前。The Open event occurs before the Load event, which is triggered when a form is opened and its records are displayed.

首次打开窗体时,下列事件将按如下顺序发生:When you first open a form, the following events occur in this order:

打开负载调整激活当前OpenLoadResizeActivateCurrent

Close 事件发生后Unload事件,触发关闭该窗体之后,但从屏幕上删除之前。The Close event occurs after the Unload event, which is triggered after the form is closed but before it is removed from the screen.

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

卸载停用关闭UnloadDeactivateClose

当 Close 事件发生时,可以打开另一个窗口或要求输入用户名来产生日志项,以表明使用该窗体或报表的用户。When the Close event occurs, you can open another window or request the user's name to make a log entry indicating who used the form or report.

如果您正在尝试来决定是否要使用宏或事件过程,其中一个明显的区别是, Open事件可被取消,但Load事件不能打开或加载事件。If you're trying to decide whether to use the Open or Load event for your macro or event procedure, one significant difference is that the Open event can be canceled, but the Load event can't.例如,如果动态生成窗体的Open事件的事件过程在窗体的记录源,可以取消打开窗体,如果没有要显示的记录。For example, if you're dynamically building a record source for a form in an event procedure for the form's Open event, you can cancel opening the form if there are no records to display.同样, Unload事件可被取消,但不能关闭的事件。Similarly, the Unload event can be canceled, but the Close event can't.

示例Example

下面的示例用于显示在用户单击“否”按钮时,如何取消窗体的打开操作。消息框提示用户输入订单的详细信息。如果用户单击“否”,则“订单明细”窗体将不打开。The following example shows how you can cancel the opening of a form when the user clicks a No button. A message box prompts the user to enter order details. If the user clicks No, the Order Details form isn't opened.

要试用该示例,请将以下事件过程添加到窗体中。To try the example, add the following event procedure to a form.

Private Sub Form_Open(Cancel As Integer) 
 Dim intReturn As Integer 
 intReturn = MsgBox("Enter order details now?", vbYesNo) 
 Select Case intReturn 
 Case vbYes 
 ' Open Order Details form. 
 DoCmd.OpenForm "Order Details" 
 Case vbNo 
 MsgBox "Remember to enter order details by 5 P.M." 
 Cancel = True ' Cancel Open event. 
 End Select 
End Sub

另请参阅See also

概念Concepts

Form 对象Form Object

© 2018 Microsoft