We have added intelligent buttons to provide visual clues to help the user navigate through the data as painlessly as possible. The user interface is not all about forms and buttons though. What happens if the user accidentally presses the Ctrl and Z keys? This has the same effect as selecting Edit Undo Current Field/Record from the menu. If the user has modified several fields in the record then all this work could be lost in a split second. If the form was large and complex then this data may have taken an hour or more to compile and input the user is not going to be happy even though, strictly speaking, it was "their fault". As a programmer you have a professional responsibility to try to make your applications as "foolproof" as possible.
We will add a confirmation dialogue to give the user a second chance if they inadvertently trigger an undo operation. Follow the steps below and then go through the explanation that follows .
frmCompany Design Undo On Undo Code Builder
Add the following code to the form's Undo event procedure:
Private Sub Form_Undo(Cancel as Integer) Dim intResult As Integer 'ask the user what to do intResult = MsgBox("Are you sure you want to discard all changes " _ & "you have made to this record?", vbYesNo) If intResult = vbYes Then 'user confirmed so go ahead and allow undo to continue Cancel = False Else 'user changed their mind - cancel the undo Cancel = True End If End Sub
Switch back to Access by hitting Alt+F11 . Close the frmCompany form and save it when prompted. Open the form again and make a few changes to a record. Now press Ctrl+ Z (or select Undo from the menu).
Note: you may have to activate undo more than once as the first Undo will simply undo typing changes you have made to that field only. This time, instead of simply throwing away the changes, we now get asked to confirm our actions.
If we select Yes then the undo action continues as normal. Select No and it is cancelled.
How It Works
Lets go through the code in detail:
Dim intResult As Integer
First be we declare an integer variable ready to hold the results of our dialog:
'ask the user what to do intResult = MsgBox("Are you sure you want to discard all changes " _ & "you have made to this record?", vbYesNo)
Then we go ahead and display the dialog. MsgBox is a standard Access function and saves us the chore of creating our own forms to do the same thing. The vbYesNo constant tells MsgBox to display two buttons for the user to press, one Yes and one No . After MsgBox returns (completes its task) the user's answer is placed into intResult .
You will see more of MsgBox in future chapters but if you want to check out some of its other possibilities feel free to look it up in Microsoft Visual Basic Help.
If intResult = vbYes Then 'user confirmed so go ahead and allow undo to continue Cancel = False Else 'user changed their mind - cancel the undo Cancel = True End If
Finally we set Cancel accordingly . You may have noticed that Cancel appeared in the procedure's declaration line as a parameter. Don't worry too much about this at the moment. It is enough to know that this is simply a mechanism for us to return a result back to Access. All we need to do is set Cancel correctly and Access takes care of the rest.
That's it! Obviously it is not possible to guard against every eventuality, nor is it always desirable. For example, if undo is legitimately required frequently in the course of normal data entry then it will quickly become tiresome for the user to have to confirm their actions each and every time. You will always have to balance inconvenience to some users against the risk of letting them mess up.