OldValue Property Example

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.

Sub Validate_Field()
    Dim curNewValue As Currency
    Dim curOriginalValue As Currency
    Dim curChange As Currency, 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