Link to home
Start Free TrialLog in
Avatar of cklosko
cklosko

asked on

How do I access a field's old value before it is updated to a new value?

I am creating a change history of updates to fields. I need to capture the old value of the field before it was changed. Within BeforeUpdate, the field already contains the updated value, not the original value. How do I access the field's original value, before the update occurs, in order to be able to capture the old value and the new value, when a field is updated?
Avatar of rockiroads
rockiroads
Flag of United States of America image

Have you tried looking at  .OldValue ?

ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cklosko
cklosko

ASKER

Would I use this in BeforeUPdate?
Private Sub YourControl_BeforeUpdate(Cancel As Integer)

Dim x
x=  Nz(Me.YourControl.OldValue,"")
If x = "" Then  ' no old value
   ' whatever
Else
   ' has old value - do what ever
End If

End Sub

mx
Avatar of cklosko

ASKER

That worked. Thank you.
Yes, you would. See the example I gave in using variables to hold the old value.

The auditing should take place after the record has been saved.
You need to test OldValue for Null - because there will not be an OldValue for a new record - and you will get a run time error.

mx
mx, Should be okay if sOldValue defined as variant?
it should be fine so as long as you use a recordset to insert. using sql then its a issue. Probably easier all round just to use NZ without having to phaff about.
Humm ... does not seem to be a problem  ... I SWEAR this used to be an issue.

mx

its what I thought, variants handle null. The issue is if you generate sql then it becomes a issue due to the null, but adding via recordsets is fine