code with old value

rainbows

Registered User.
Local time
Today, 01:30
Joined
Apr 21, 2017
Messages
428
hi this is some code someone helped me with a long time ago, and i am trying to go thro all the codes i have been given help with to try and understand how they work for future

this code works great but i am total confused how it gets the oldvalue as i have no feild anywhere calling it up

steve





Code:
Private Sub StockQty_AfterUpdate()

Dim OldValue As String
Dim NewValue As String
Dim MaterialID As Double
MaterialID = Me.MaterialID

OldValue = Me.StockQTY.OldValue
NewValue = Me.StockQTY

DoCmd.RunSQL "INSERT INTO TblDataChanges ( MaterialID, ChangeDate, ControlName, OldValue, NewValue ) VALUES ('" & MaterialID & "',Now(),'stockqty' ,'" & OldValue & "', '" & NewValue & "');"
Exit Sub

End Sub
 
To understand this, you could visit the object catalog, which can be accessed using the F2 key in the VBA editor, for example.

Objects have properties, text boxes as controls of a form are objects of this form, and they have a number of properties, including Value and OldValue.
 
@rainbows - Since you want to understand where .OldValue comes from, I'll explain a bit. This is a rough overview and I might have left out some things.

On a bound form, you can expect to have bound controls. If you make a change to a control on the form, one of the possible things you can do is a DoCmd.UnDo to reset the form to its original values. Another thing is to save the updated form. But how does that work?

Each bound control has three property-style values of note that describe its value. The first and most obvious property is .Value which is the visible value shown on the form (if the control is visible at the time). For bound controls, the second property is .OldValue, which holds the value that the control had as a result of the Form_Current event. That is because the underlying .Recordsource table/query supplied values to each bound control during _Current, and the values went TWO places... in .Value and in .OldValue, and this happens because of what happens with .Save vs. .UnDo methods.

When you do a .Save, the form updates the current record from the bound controls' .Value properties to the .ControlSource for that control, and then writes the updated record back to the .RecordSource, whatever it is. Then a _Current event starts that whole cycle over again.

When you do an .UnDo, the form updates each control by copying the .OldValue to overlay the .Value property, thus restoring the form's data to the values that were based on the most recent _Current event. An update of the .RecordSource is not necessary because after the .UnDo, the form's controls and the .RecordSource match again. Therefore, no .Save occurs.

The third property is ONLY for whatever control is currently in focus and is called .Text - the text of what is being manually entered into the control at the moment. The .Value property doesn't actually change right there. You can use backspace or other text editing shortcuts. When you tab out of that control, though, .Value becomes the "official" value and you can no longer reference that control's .Text property. This may be somewhat of an oversimplification of the .Text property, but it comes close.

For an unbound form or unbound control, there IS no .RecordSource or .ControlSource to supply an original value. In that case, no .OldValue property exists that you can reference, and an .UnDo has no effect. However, the .Text property IS available even for unbound controls when they are in focus.
 

Users who are viewing this thread

Back
Top Bottom