Solved

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

Posted on 2009-03-31
12
369 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now