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

OptionGroup.BeforeUpdate 事件 (访问)OptionGroup.BeforeUpdate Event (Access)

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

在控件或记录中的更改数据得到更新之前发生 BeforeUpdate 事件。The BeforeUpdate event occurs before changed data in a control or record is updated.

语法Syntax

表达式更新前(* 取消)expression. **BeforeUpdate( ** Cancel* )

表达式_一个表示选项组中的对象的变量。_expression A variable that represents an OptionGroup object.

参数Parameters

名称Name必需/可选Required/Optional数据类型Data Type说明Description
CancelCancel必需RequiredIntegerInteger该设置可确定是否发生 BeforeUpdate 事件。将 Cancel 自变量设置为 True (?1) 可取消 BeforeUpdate 事件。The setting determines if the BeforeUpdate event occurs. Setting the Cancel argument to True (?1) cancels the BeforeUpdate event.

注解Remarks

更改控件中的数据通过使用 Visual Basic 或宏中包含 SetValue 操作不会触发这些事件的控件。但是,如果再移至其他记录或保存记录时,窗体的更新前事件发生。Changing data in a control by using Visual Basic or a macro containing the SetValue action doesn't trigger these events for the control. However, if you then move to another record or save the record, the form's BeforeUpdate event does occur.

若要在此事件发生时运行宏或事件过程,请将 BeforeUpdate 属性设置为宏名称或 [事件过程]。To run a macro or event procedure when this event occurs, set the BeforeUpdate property to the name of the macro or to [Event Procedure].

该事件不适用于选项组中的选项按钮、复选框或切换按钮。它仅适用于选项组本身。This event does not apply to option buttons, check boxes, or toggle buttons in an option group. It applies only to the option group itself.

当控件或记录被更新时触发该事件。在记录,当控件失去焦点或用户按 ENTER 或 TAB 时,每个控件中更改的数据都将更新。当焦点离开记录或用户在记录菜单上单击保存记录,整条记录会更新,并将数据保存到数据库中。The BeforeUpdate event is triggered when a control or record is updated. Within a record, changed data in each control is updated when the control loses the focus or when the user presses ENTER or TAB. When the focus leaves the record or if the user clicks Save Record on the Records menu, the entire record is updated, and the data is saved in the database.

当在窗体上的控件中输入新的或更改过的数据然后移动到另一个记录或通过单击记录菜单上的保存记录保存记录时,在更新后立即发生窗体的更新后事件控件的的事件。当您移动到其他记录,该控件的ExitLostFocus事件发生,跟您将移到,该记录的Current事件,并在该记录中的第一个控件的EnterGotFocus事件.若要运行更新后的宏或事件过程,而不会退出LostFocus宏或事件过程,请使用记录菜单上的保存记录命令保存该记录。When you enter new or changed data in a control on a form and then move to another record or save the record by clicking Save Record on the Records menu, the AfterUpdate event for the form occur immediately after the AfterUpdate event for the control. When you move to a different record, the Exit and LostFocus events for the control occur, followed by the Current event for the record you moved to, and the Enter and GotFocus events for the first control in this record. To run the AfterUpdate macro or event procedure without running the Exit and LostFocus macros or event procedures, save the record by using the Save Record command on the Records menu.

只有当你在控件中更改数据时, BeforeUpdate 宏和事件过程才会运行。当计算控件中的值发生更改时,此事件不会发生。只有当你在记录中更改一个或多个控件的数据时,窗体的 BeforeUpdate 宏和事件过程才会运行。BeforeUpdate macros and event procedures run only if you change the data in a control. This event does not occur when a value changes in a calculated control. BeforeUpdate macros and event procedures for a form run only if you change the data in one or more controls in the record.

对于窗体,可以使用 BeforeUpdate 事件在移到另一记录之前取消对记录的更新。For forms, you can use the BeforeUpdate event to cancel updating of a record before moving to another record.

如果用户在控件中输入了新值,在保存数据(更新记录)前, OldValue 属性设置不会发生更改。如果你取消更新,那么 OldValue 属性值会替换控件中的现有值。If the user enters a new value in the control, the OldValue property setting isn't changed until the data is saved (the record is updated). If you cancel an update, the value of the OldValue property replaces the existing value in the control.

通常使用 BeforeUpdate 事件验证数据,特别是在执行复杂验证时,例如在下列情况下:You often use the BeforeUpdate event to validate data, especially when you perform complex validations, such as those that:

  • 一个窗体上有多个值的情况。Involve conditions for more than one value on a form.

  • 对输入的不同数据显示不同的错误消息。Display different error messages for different data entered.

  • 可以被用户重写。Can be overridden by the user.

  • 包含对其他窗体上控件的引用或包含用户定义的函数。Contain references to controls on other forms or contain user-defined functions.

注释 若要执行简单或复杂的验证(例如,要求在字段中填写值或在窗体上验证多个控件),可使用控件的 ValidationRule 属性以及表中字段和记录的 ValidationRuleRequired 属性。Note To perform simple validations, or more complex validations such as requiring a value in a field or validating more than one control on a form, you can use the ValidationRule property for controls and the ValidationRule and Required properties for fields and records in tables.

如果您试图修改以下控件中包含的数据,则发生运行时错误:在事件过程中触发 BeforeUpdate 事件的控件。A run-time error will occur if you attempt to modify the data contained in the control that fired the BeforeUpdate event in the event's procedure.

示例Example

下面的示例演示如何使用更新前事件过程来检查是否已输入产品名称在数据库中。用户在产品名称框中键入产品名称后,到产品表中的产品名称字段值是进行比较。如果在产品表中没有匹配值,则显示一条消息,通知用户已输入该产品。The following example shows how you can use a BeforeUpdate event procedure to check whether a product name has already been entered in the database. After the user types a product name in the ProductName box, the value is compared to the ProductName field in the Products table. If there is a matching value in the Products table, a message is displayed that informs the user that the product has already been entered.

若要尝试运行该示例,请将以下事件过程添加到一个名为 Products(产品)的窗体中,该窗体包含一个名为 ProductName(产品名称)的文本框。To try the example, add the following event procedure to a form named Products that contains a text box called ProductName.

Private Sub ProductName_BeforeUpdate(Cancel As Integer) 
 If(Not IsNull(DLookup("[ProductName]", _ 
 "Products", "[ProductName] ='" _ 
 &; Me!ProductName &; "'"))) Then 
 MsgBox "Product has already been entered in the database." 
 Cancel = True 
 Me!ProductName.Undo 
 End If 
End Sub

另请参阅See also

概念Concepts

选项组对象OptionGroup Object

© 2018 Microsoft