ESC not closing report after zoom

I have a problem with Access 2010/2013 ignoring the ESC button to close a report, if the report has been zoomed while in preview

I've tried to put in the code below in the report, but that throws a code 2585 after the zoom has been used

Private Sub Report_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
        DoCmd.Close acReport, Me.Name
End If
End Sub

Open in new window

Theres kind of a workaround here, but I was hoping for something more elegant :)
Possible workaround
Mik MakConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I've tried to put in the code below in the report, but that throws a code 2585 after the zoom has been used
I think that's By Design it behaves like that to prevent a report to be closed...
Mik MakConsultantAuthor Commented:
Besides me trying to find a workaround by deploying that code, why should it be By Design to prevent a report being closed after a Zoom, but not before a Zoom ?
Jeffrey CoachmanMIS LiasonCommented:
This behavior applies to reports opened in "Print Preview" mode.
If you open a report in "Report View' you cannot close it by pressing esc.
A Report "typically" does not allow interaction, when you open it (again, Print Preview), are really not intending to "do anything" to the report.
This is my guess as to why you can press esc to close the report right after you open it.
Note here that, Tables, Queries and Forms do not allow you to press esc to close them, ...(again, guess is ) because they allow "interaction".

Going a bit deeper.
Print Preview is a separate "View" that can exist on top of Report View.
Notice that when you open a report in Print Preview, the entire Ribbon changes.  (as opposed to the ribbon staying virtually the same when you open any other object.)
Also note that if you open a Report in Report View, can also open it in Print Preview on top of Report view.
Press esc and Print Preview closes, and Report View is still on the screen.

Finally, ...the thought process is that you open the report in Print Preview to see what the report will actually look like when you print it.
If you like the way it looks,...simply click the Print Icon/Button
If you don't like the way it looks (or you simply don't want to print it,...just press esc.
With other objects "interaction" is expected at some point/level

Final thoughts.
This whole matter was a bit confusing when MS first introduced it (and still is, to a certain extent).
In a future release or patch, MS may change this behavior.


IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Jeffrey CoachmanMIS LiasonCommented:
Oh, ...

If it were me, ...I would not try to circumvent this functionality.
1. It will create a situation where users wonder why only reports can be closed by pressing esc.
(and worse,...ask for the same functionality for tables, forms or reports (wreaking havoc if esc is pressed the middle of an edit/update)

Experience has taught me that whenever you try to "Fight" the default Access functionality, can sometimes cause more problems than it solves.

Finally, ...look at all the code involved to do this...
Using Timers and three different batches of code on three different events!?

Is this really this much of an issue? (to go through all of that)
...and what about any unforeseen consequences...?)
This Q rarely every comes up here...

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I kinda found a solution by calling Windows API

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10

Private Sub Report_KeyDown(KeyCode As Integer, Shift As Integer)
    On Error GoTo Err
    If KeyCode = 27 Then 'Detect the escape key
        DoCmd.Close acReport, Me.Name
    End If
    Exit Sub
    'To capture the error number
    If Err.Number = 2585 Then
        PostMessage Me.hwnd, WM_CLOSE, CLng(0), CLng(0)
    End If
End Sub

Open in new window

I did some tests in this article.

How to close an Access report by pressing Escape (Esc) key

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mik MakConsultantAuthor Commented:
Although I fully appreciate the comments from Jeffrey - Ryan came up with an elegant workaround to the problem - just as I was looking for :) Thank you very much everyone
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.