Results 1 to 5 of 5
  1. #1
    Waubain is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2011
    Posts
    25

    Undo problem

    I have a subform(wihtin a main form) that opens in read only. There is a EDIT button that opens separate formr to edit the subform records. On this second form there is a Cancel button that will undo any changes and closes the form. This is the basic code for the Cancel button.

    Code:
    Private Sub Command46_Click()
    ' Undo the changes
    DoCmd.RunCommand acCmdUndo
    ' Close the form
    DoCmd.Close acForm, Me.Name
    End Sub
    The problem is that if a person clicks the Cancel button before any changes are made then they get an error(2046, Undo isn't available now), probably because there is nothing to undo. I would like to undo and close if changes were made, or close if no changes made.



    I know I need some type of error trapping, but would appreciate any help.

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    How about going to this instead:

    Code:
    Private Sub Command46_Click()
    ' Undo the changes
    If Me.Dirty Then
       Me.Undo
    End If
    ' Close the form
    DoCmd.Close acForm, Me.Name
    End Sub

  3. #3
    Waubain is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2011
    Posts
    25
    This works great. Thank you.

    Just as there are many ways to complete a task in VBA, there is probably not a single correct answer to this question. As a novice it is unclear if there is a "best practice" in solving a task? In my first attempt at creating the Cancel(Undo) button I used this statement I found on the web.

    DoCmd.RunCommand acCmdUndo

    In your reply this worked just as well (within the IF statement).

    Me.Undo

    Short and direct. My philosophy in most cases is the least complex answer is usually the best. Many how-to books are written but few address why ,in general, one command might be better than another.

    Thank you again.

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    The DoCmd commands can be helpful but, as you have mentioned, sometimes there are better ways (or at least more direct ways) to accomplish the same thing.

    Using Me.Undo instead is more specific than using the RunCommand as it may be possible for the form to lose focus for some reason and the DoCmd.RunCommands deal with the active object at the time. So, by using Me.Undo instead, you have now made it more specific to that specific form and it will work on that specific form regardless of what has the focus.

  5. #5
    Waubain is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2011
    Posts
    25
    Thank you for the explanation.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to undo acCmdPaste ???
    By focosi in forum Programming
    Replies: 12
    Last Post: 09-25-2011, 01:48 PM
  2. Undo one action only
    By zoooza84 in forum Access
    Replies: 5
    Last Post: 08-21-2011, 03:04 PM
  3. How do I undo the requirement to login every mdb
    By garymkrieg in forum Security
    Replies: 3
    Last Post: 07-11-2011, 03:53 PM
  4. Conditional Formatting through VBA: On Undo
    By Remster in forum Programming
    Replies: 6
    Last Post: 04-13-2011, 09:04 AM
  5. Undo Table Design Changes
    By MelindaP in forum Database Design
    Replies: 6
    Last Post: 07-27-2010, 09:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums