Find a solution to "undo command only run 1 time" (1 Viewer)

indian818

New member
Local time
Today, 16:30
Joined
Mar 30, 2021
Messages
15
Hi!
I tried to make a command button which functions as Ctrl+Z (Word shortcut key), I used

Private Sub Command332_Click()
DoCmd.RunCommand acCmdUndo
End Sub

It works well but only for the first time. The next time I clicked the (undo) command button, it sent me a message

Capture.PNG



Capturek.PNG


Is there any code that can create an undo command button in Access functioning as Ctrl+Z in Word?
If there is, how can I use that by pressing the very popular shortcut key Ctrl+Z?
Thank for any upcoming help.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
Hi. In Access, hitting the Esc key performs the Undo command. Is that acceptable for you?
 

indian818

New member
Local time
Today, 16:30
Joined
Mar 30, 2021
Messages
15
The Esc key only works while I'm modifying the field content, but I have a save button, if I hit save button then hit Esc, it changes nothing. In the meantime, undo command button can throw thing back before I click save button, but unfortunately it can use for 1 time only, cannot send me back further.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
The Esc key only works while I'm modifying the field content, but I have a save button, if I hit save button then hit Esc, it changes nothing. In the meantime, undo command button can throw thing back before I click save button, but unfortunately it can use for 1 time only, cannot send me back further.
Okay, try changing your code to this one and let us know if it helps or not.

Code:
Private Sub Command332_Click()
    Me.Undo
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
The Esc key only works while I'm modifying the field content, but I have a save button, if I hit save button then hit Esc, it changes nothing. In the meantime, undo command button can throw thing back before I click save button, but unfortunately it can use for 1 time only, cannot send me back further.
Oh wait, did I understand it correctly? You want to "undo" the record after you already clicked on the Save button?
 

indian818

New member
Local time
Today, 16:30
Joined
Mar 30, 2021
Messages
15
Oh wait, did I understand it correctly? You want to "undo" the record after you already clicked on the Save button?
Yeah, I wish to create an undo command button which works the same way as Ctrl+Z of Word does.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
Yeah, I wish to create an undo command button which works the same way as Ctrl+Z of Word does.
Unfortunately, that won't be an easy task. You would have to keep track of each change to undo them.

I would recommend taking a look at some audit trail demo to see how you can track user changes and store them in a table.

Once you have that, you can then create something to reverse the changes.
 
Last edited:

Isaac

Lifelong Learner
Local time
Today, 02:30
Joined
Mar 14, 2017
Messages
8,738
Ultimately you can't do what you are asking without custom code that logs all actions in order to reverse them. Escape, Me.Undo, they all just undo one thing, or a few things on one level, so to speak
 

indian818

New member
Local time
Today, 16:30
Joined
Mar 30, 2021
Messages
15
Unfortunately, that won't be an easy task. You would have to keep track of each change to undo them.

I would recommend taking a look at some audit trail demo to see how you can track user changes and store them in a table.

Once you have that, you can then create something to reverse the changes.
May be that won't be an easy task to you, but it will definitely be impossible job to me, I mean it's like I'm trying invent a time machine.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
May be that won't be an easy task to you, but it will definitely be impossible job to me, I mean it's like I'm trying invent a time machine.
Did you take a look at any of the available audit trail demoes? I was hoping that would give you some ideas.
 

Rene vK

Member
Local time
Today, 10:30
Joined
Mar 3, 2013
Messages
123
My thought with this is: you probably have to go in a different direction and create another way to input data. Maybe an unbound form with a button that transfers the record when somebody is really shore.....
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:30
Joined
Feb 19, 2002
Messages
42,970
Have you considered checking the dirty flag before executing the undo?

If Me.Dirty = True
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:30
Joined
Feb 28, 2001
Messages
26,996
it's like I'm trying invent a time machine

Not "like" - EXACTLY what you are asking to do. Once you perform an action to save a record's contents, you cannot go backwards strictly within Access. As noted, you COULD do it through complex code that includes saving each set of changes in an audit-table or history table.

What you want to do in Access is more like making changes in Word, then SAVING it and exiting, then re-launching Word on the same file. Your CTRL/Z operation won't work in that case either.

Access's UNDO command isn't changing saved records. It is changing UNSAVED information only. So your mental image was based on an imperfect understanding of the differences between Word and Access. For what it's worth, if there WAS such a functionality in Access, I know of more than a few times I would have used it happily. But like the old rock song, "You Can't Always Get What You Want."
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:30
Joined
May 21, 2018
Messages
8,463
@indian818
Can you explain this better? I can see this in a word processor. But why would anyone want to roll a record back multiple times? I cannot think of a real world application. I could maybe see this for a large memo field, but if I have a database where the user needs to roll back records many times then a best there is a serious problem with the database. But like @theDBguy said this could be done with an audit trail to save any modifications to records, and you could roll it back.

If you look at most audit trail demos they store the value for each field that is modified. So you could pretty easily swap the existing value with the previous value. I think this is pretty doable, but not sure of real world utility. Are you wanting to do this for just one field or all fields?

Here is a link to one example
@theDBguy, do you have an audit trail that you recommend? I see you referenced in the link above. I see some of the discussion is about using data macros and the pros and cons.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
@theDBguy, do you have an audit trail that you recommend? I see you referenced in the link above. I see some of the discussion is about using data macros and the pros and cons.
@MajP As you said, there are plenty of samples and different approaches to creating/implementing an audit trail. Some people use a class, others use a standard module. And yet, others like the one you referenced above, use data macros. I don't recommend any particular approach, because I know an audit trail can sometimes be daunting to successfully implement, so I often advise to go with what's comfortable to them. I may have been involved in some discussions where an audit trail application requires some modifications to also work with subforms. One particular con, I vaguely remember, about using data macros, is how to record the user information about who modified the data. Cheers!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:30
Joined
Feb 19, 2002
Messages
42,970
I guess I missed the original point. An audit trail is the best option.
 

Users who are viewing this thread

Top Bottom