Filtered form gets run-time error 2001 on "Esc" (1 Viewer)

Danick

Registered User.
Local time
Yesterday, 20:18
Joined
Sep 23, 2008
Messages
351
I have a continuous form that I'm using with various filters. There is a command button to filter the form based on what the user selects in a few combo boxes. The "Apply Filter" command button is the default so when the user presses "Enter" on the keyboard, the form gets filtered. There is also a "Cancel filter - show all records" button.

If the user starts to enter data in the filter boxes but presses "Esc" before applying the filter, then the form undoes the entries and resets to unfiltered. That's fine. But if the user presses the "Esc" button after the form is filtered (either by pressing "Enter" or pressing the "Apply Filter" button, then Access produces a run-time error 2001 (You canceled the previous operation).

How can I trap this error for Access to disregard it or to mimic the "Cancel Filter" button if the user presses "Esc".
 

isladogs

MVP / VIP
Local time
Today, 01:18
Joined
Jan 14, 2017
Messages
18,209
In the error handling section try
Code:
if err=2001 Then Resume Next
 

Danick

Registered User.
Local time
Yesterday, 20:18
Joined
Sep 23, 2008
Messages
351
In the error handling section try
Code:
if err=2001 Then Resume Next

Where do I put this error handling? I tried putting it in the form On Current event, but still getting the error.
 

isladogs

MVP / VIP
Local time
Today, 01:18
Joined
Jan 14, 2017
Messages
18,209
You could try the Form_Error event but I've never had this issue in my apps so I can't say that with real confidence
Otherwise, you need to step through the code and determine where it is triggered.
From your description, possibly in two places??
 

Danick

Registered User.
Local time
Yesterday, 20:18
Joined
Sep 23, 2008
Messages
351
You could try the Form_Error event but I've never had this issue in my apps so I can't say that with real confidence
Otherwise, you need to step through the code and determine where it is triggered.
From your description, possibly in two places??

Well Form_Error event didn't work either.

I believe that Access has the "Escape" keyboard button built in so that it always tries to perform a "Cancel / Undo" event. So there isn't anywhere in my code that is triggering it.
 

Danick

Registered User.
Local time
Yesterday, 20:18
Joined
Sep 23, 2008
Messages
351
Well after a little google searching, I found a way of doing this using the KeyDown event. solution found here:
Code:
http://www.vbforums.com/showthread.php?367165-VB6-Using-the-KeyDown-Event

For anyone interested, here is how you do it.

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    'make sure KeyPreview is True on Form Properties
    On Error Resume Next
    Select Case KeyCode
        Case vbKeyEscape 

            'add you code here.  I added my cancel event VBA to mimic pressing the "View All Records" button.  


    End Select
End Sub

Seems to be working for me...
 

isladogs

MVP / VIP
Local time
Today, 01:18
Joined
Jan 14, 2017
Messages
18,209
Ironically, I was also going to suggest using the KeyDown event to block the use of Enter or Esc in this case. Adapting your posted code below

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    'make sure KeyPreview is True on Form Properties
    On Error Resume Next
    Select Case KeyCode
        Case vbKeyEscape, vbKeyReturn
            'don't allow use of Esc / Enter
             KeyCode=0
        
    End Select
End Sub
 

Users who are viewing this thread

Top Bottom