Can Form_BeforeUpdate detect a specific control change?

D

Doug F.

In Form_BeforeUpdate event I want to check if a specific control had been
changed. This is for a business rule, if x has changed and y then CancelEvent.
Thanks.
 
R

Rick Brandt

Doug said:
In Form_BeforeUpdate event I want to check if a specific control had
been changed. This is for a business rule, if x has changed and y
then CancelEvent. Thanks.

If Me!ControlName <> Me!ControlName.OldValue Then...
 
D

David W. Fenton

If Me!ControlName <> Me!ControlName.OldValue Then...

..OldValue, like .Text, is only available when the control has the
focus, if I'm not mistaken.

You have to do this kind of validation in the controls themselves.

The one way to do this is to open a snapshot recordset when the
record is loaded, and compare the values to that. Or to populate an
array that stores the values in the controls before they've been
changed. Or to use a set of flags set in the events of the
individual controls that tell you if you the values have changed.
 
B

Brendan Reynolds

The control does not need to have the focus. You can test the OldValue
property in the Form_BeforeUpdate event as in Rick's example. You do need to
allow for the possibility of Null values, though ..

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Nz(Me.TestNum2.Value) <> Nz(Me.TestNum2.OldValue) Then
MsgBox "Oh, no you don't!"
Cancel = True
End If

End Sub
 
Top