Results 1 to 5 of 5
  1. #1
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,484

    Undo button for unlimited number of form changes to control data, one control at a time.

    UndoStack-davegri-v01.zip

    I couldn't find this Access functionality addressed anywhere on the internet, so I decided to try it myself. Here's the result.

    PROBLEM:
    Access built-in Undo will undo the entire form at one click, or force you to manually set the focus to each target control one by one to undo.
    Access also does not provide undo methods or events at all for checkboxs, radio buttons or option groups.

    SOLUTION:
    The attached DB has a form with an undo button that will back out changes to form controls in LIFO order.
    The undo stack is simulated by an Access global Collection that holds the change data. Each edit change to form data is "Pushed" onto the stack. Clicking the UNDO button will reverse ("Pop") the last made change, continuing with each button click until no changes remain to be reversed. A counter field on the form displays the number of changes remaining in the stack. Once changes are committed to the table, undo is unavailable.
    The code is for a main form only. Subforms are not addressed.

    The below form shows that 7 unsaved changes have been made that can be backed out. As each is backed out, the count decreases by one until at zero, the Undo button is grayed out.



    Click image for larger version. 

Name:	stack.png 
Views:	30 
Size:	26.9 KB 
ID:	43482
    Last edited by davegri; 11-21-2020 at 10:22 PM. Reason: format

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    15,129

  3. #3
    accesstos's Avatar
    accesstos is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    361
    Dave,

    Nice work and very nice challenge for the lock down!

    I did replicate your achieve (just for fun) with my favorite, class-ic way, following the reusability principles.
    I created a class (clsMyStackControl) that provides the desired functionality of the form's controls and an other class (clsMyStack) that provides the necessary functionality for an "Undo Stack Unlimited". Those two classes, takes over the whole work of the undo procedure and leave the least for the forms that use them. So, for each form that needs this feature, all the necessary code is this:
    Code:
    Option Compare Database
    Option Explicit
    
    Private mStack As clsMyStack
    
    Private Sub Form_Close()
        If mStack Is Nothing Then
        Else
            Set mStack.Parent = Nothing 
            Set mStack = Nothing
        End If
    End Sub
    
    Private Sub Form_Current()
        If mStack Is Nothing Then
            Set mStack = New clsMyStack
            Set mStack.Display = Me.txtUndos
            Set mStack.Button = Me.cmdUndo
        End If
        Set mStack.Parent = Me
    End Sub
    Is always the same for any form, independent of the quantity of the controls or their names, either in a single, a continuous or a subform, as long as there are some updatable controls in the form, an undo commandbutton and a label (or a textbox) to display the remaining undoes.

    So, we have two class modules that we can import in any project and use their functionality in any form, almost without any adaptation at the existing code.

    Check the small sample database in attachment and I hope that you will find it useful and interesting.

    Cheers,
    John
    Attached Files Attached Files

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,484
    Quote Originally Posted by accesstos View Post
    Dave,
    Check the small sample database in attachment and I hope that you will find it useful and interesting.
    Very nice and easy to implement.
    I was able to incorporate your code into my example form changing only 2 lines of your code; the name of the undo button and the name of the textbox to display the stack count.
    Your solution handles subforms the same as the main form, with the subform requiring the class definitions and a separate undo button. From a user's perspective that might be confusing and unworkable. That's why I didn't address subforms in the original. Of course, developers have the choice in your solution to include or not include the subform undo. Good work. You get a star.
    Last edited by davegri; 11-23-2020 at 11:23 AM. Reason: clarif

  5. #5
    accesstos's Avatar
    accesstos is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    361
    Hi Dave, thanks a lot for the star and for your time.

    Maybe your are right about the multi buttons in a form, but, alternatively, we able to use directly the Undo method of the class via the form's code, for example, in the code of the KeyDown event as follows:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyEscape Then
            If mStack Is Nothing Then
            Else
                If mStack.Count Then
                    mStack.Undo 'Undo the last change
                    KeyCode = 0 'Cancel form's Undo
                End If
            End If
        End If
    End Sub
    So, this makes the code of the form even more reusable.

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

Similar Threads

  1. Replies: 15
    Last Post: 05-12-2016, 02:27 PM
  2. Replies: 4
    Last Post: 02-18-2016, 12:06 PM
  3. Replies: 2
    Last Post: 11-27-2014, 01:06 PM
  4. Replies: 4
    Last Post: 06-30-2012, 02:01 AM
  5. Replies: 7
    Last Post: 07-15-2011, 08:14 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 - Senior Forums