Navigation

9/21/2012

VBA code to ESC from Access Form entry

In Microsoft Access 2010 database, wanted to create button that did same as pressing ESC key;

Found blog post on Use code to "undo" things in Access 

DoCmd.RunCommand acCmdUndo

If there is nothing in the form, nothing can be UNdone, get error 2046.  Added error handling,  just close the form, and exit the subroutine.


Private Sub Button_Click()
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdUndo
DoCmd.Close acForm, "Faculty Purchases - FY 2012", acSaveYes
Exit Sub
ErrorHandler:

Select Case Err

         Case 2046:    ' Error 2046: "Nothing can be UNDOne"
                       
         DoCmd.Close acForm, "Faculty Purchases - FY 2012", acSaveYes
         Exit Sub
        
         Case Else:      ' An error other than 2046 has occurred.
            ' Display the error number and the error text.
            MsgBox "Error # " & Err & " : " & Error(Err)

      End Select
End Sub