VBA Undo Command not Undoing

Jason1971

Registered User.
Local time
Today, 23:16
Joined
Jan 19, 2009
Messages
46
Hi,

After OK is clicked to close a custom error msg dialog box, I want to perform two Undos followed by a switch focus.
When I perform this manually with my mouse it works fine, however, my code below to perform the same task using Vba doesn't work, specifically the Undos don't undo. I'm trying to use the undos to fully clear a textbox1 (or checkbox1) on a subform1 because I'm getting the dreaded "Update or CancelUpdate without AddNew or Edit" error. Any help would be really appreciated, thnx.

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Const conErrCode = 3314

If DataErr = conErrCode Then
Beep
MsgBox ("Which Patient? Please enter the Patient UR.")
Response = acDataErrContinue
DoCmd.RunCommand acCmdUndo
DoCmd.RunCommand acCmdUndo
Form_Form1.UR.SetFocus

Else
Response = acDataErrDisplay
End If

End Sub
 
You can use the .OldValue property of the text box to get it back to what it previously was.

Me.Undo will undo all changes to the current record.
 
Thanks for your helpful comments, I am most grateful. Unfortunately Me.Undo didn't work either and .OldValue will not work because I have too many textboxes on the form which could be dirty, to use the .OldValue
I just noticed the following...Me.Undo and DoCmd.RunCommand acCmdUndo both work perfectly when I use the Subform in isolation (as its own main form). The problem now only occurs when this subform is nested into a Form1, which is the way I need it to be. So I guess the code above is ok but I need to do some sort of rearranging/placing it in the main Form1 maybe?? Or, how can I call a main form1 undo (using code) from within my SubForm On Error script section. Any ideas? Thanks in Advance.
Jason.

PS. I have now also found that the problem is related to the Linking of the Subform to the Main Form. When I unlink the two forms the undo works fine.
 
Last edited:
I'm really stuck on this one. It seems to be related to the fact that I have form1.field1 linked to subform1.field2 and subform1.field2 is a compulsory/required field. When a user enters data into subform1.field3 this triggers an error "no data in subform1.field2" which I capture with the code above and then try and do a Me.Undo. Thats when I get the "Update or CancelUpdate without AddNew or Edit" error.
Any Ideas Anyone? Thanks in Advance.
 
When I downloaded your sample, I got messages that it was corrupt.
 

Users who are viewing this thread

Back
Top Bottom