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

Control.OldValue 属性 (访问)Control.OldValue Property (Access)

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

OldValue属性可用于确定绑定控件未编辑的值。只读变量You can use the OldValue property to determine the unedited value of a bound control. Read-only Variant.

语法Syntax

表达式OldValueexpression. OldValue

表达式_一个表示控件对象的变量。_expression A variable that represents a Control object.

注解Remarks

OldValue属性包含的是绑定控件中未编辑的数据,则在所有视图中以只读的。The OldValue property contains the unedited data from a bound control and is read-only in all views.

Microsoft Access 使用OldValue属性来存储绑定控件的原始值。编辑窗体上的绑定的控件时,才能移动到另一个记录时保存所做的更改。OldValue属性包含的基础数据的未编辑的版本。Microsoft Access uses the OldValue property to store the original value of a bound control. When you edit a bound control on a form, your changes aren't saved until you move to another record. The OldValue property contains the unedited version of the underlying data.

您可以通过为控件指定OldValue属性设置提供恢复数据的功能。下面的示例显示如何恢复对窗体上的文本框控件的任何更改:You can provide your own undo capability by assigning the OldValue property setting to a control. The following example shows how you can undo any changes to text box controls on a form:

Private Sub btnUndo_Click() 

 Dim ctlTextbox As Control 

 For Each ctlTextbox in Me.Controls 
 If ctlTextbox.ControlType = acTextBox Then 
 ctlTextbox.Value = ctl.OldValue 
 End If 
 Next ctlTextbox 

End Sub

如果控件尚未编辑,该代码不起作用。当移动到其他记录时,记录源将更新,这样当前值和OldValue属性是相同的。If the control hasn't been edited, this code has no effect. When you move to another record, the record source is updated, so the current value and the OldValue property will be the same.

OldValue属性设置具有相同的数据类型作为该控件所绑定到的字段。The OldValue property setting has the same data type as the field to which the control is bound.

示例Example

下面的示例检查以确定在字段中输入的新数据是否在原始数据值的 10%。如果更改为大于 10%, OldValue属性用于还原为原始值。无法从包含您要验证的数据的控件的更新前事件中调用该过程。The following example checks to determine if new data entered in a field is within 10 percent of the value of the original data. If the change is greater than 10 percent, the OldValue property is used to restore the original value. This procedure could be called from the BeforeUpdate event of the control that contains data you want to validate.

Public Sub Validate_Field() 

 Dim curNewValue As Currency 
 Dim curOriginalValue As Currency 
 Dim curChange As Currency 
 Dim strMsg As String 

 curNewValue = Forms!Products!UnitPrice 
 curOriginalValue = Forms!Products!UnitPrice.OldValue 
 curChange = Abs(curNewValue - curOriginalValue) 

 If curChange > (curOriginalValue * .1) Then 
 strMsg = "Change is more than 10% of original unit price. " _ 
 &; "Restoring original unit price." 
 MsgBox strMsg, vbExclamation, "Invalid change." 
 Forms!Products!UnitPrice = curOriginalValue 
 End If 

End Sub

另请参阅See also

概念Concepts

控件对象Control Object

© 2018 Microsoft