简体   繁体   中英

MS-Access VBA: How to set field value back to old value if Form_Error event is triggered

I've had a request to return the value entered in a field by user to the previous value if it does not pass data validation criteria.

Where I can trap this in the BeforeUpdate event I am using a global variable to store the .oldvalue and then reassigning in the AfterUpdate event.

However data validation triggered by table structure exceptions (such as text in a number field) trigger the Form_Error event before the BeforeUpdate event and the Form_Error event does not relate to a specific field that I can store as an old value.

I could get around this by setting up the table as completely free text in all fields with nulls allowed, such that there would be no Form_Error event triggered and all validation handled in BeforeUpdate

Is this really the way I need to go about this? It seems odd to have to set up a nonsense table structure to handle errors but I'm struggling to come up with another method

I'm turning this comment into an answer. This is the gist of how to revert from the Form_Error .

Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Undo
    Response = 0
End Sub

You could do more like display a message on the screen saying what you did to you user then hide it on a timer event. Just something so their not sure why their data went away.

Set yourglobal on the gotfocus event of the control. Then for the error event use

If err.number = 2107 then
yourfield = yourglobal
err.clear
end if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM