Solved

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

Posted on 2009-03-31
12
407 Views
Last Modified: 2013-11-28
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?
0
Comment
Question by:cklosko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24030747
Have you tried looking at  .OldValue ?

0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 24030790
An example, say you had a field called myField in your form

Private Sub MyField_BeforeUpdate(Cancel As Integer)
    MsgBox "Before Update it is " & MyField.OldValue
    MsgBox "Going to be updated to " & MyField.Value
End Sub

To capture it in the afterupdate event, you could maybe hold this value in a temp variable then use it in the afterupdate
eg

'Defined globally in form
Dim sOldValue as String

Private Sub MyField_BeforeUpdate(Cancel As Integer)
    sOldValue = MyField.OldValue
End Sub

Private Sub MyField_AfterUpdate()
     Msgbox "Old Value was " & sOldValue & vbcrlf & "New Value is " & MyField.Value
end sub
0
 

Author Comment

by:cklosko
ID: 24030797
Would I use this in BeforeUPdate?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75
ID: 24030810
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
0
 

Author Comment

by:cklosko
ID: 24030828
That worked. Thank you.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24030837
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.
0
 
LVL 75
ID: 24030880
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
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24030904
mx, Should be okay if sOldValue defined as variant?
0
 
LVL 75
ID: 24030937
Better test that ...

mx
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24031024
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.
0
 
LVL 75
ID: 24031091
Humm ... does not seem to be a problem  ... I SWEAR this used to be an issue.

mx

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24031281
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question