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
LVL 1
Mik MakConsultantAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
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
Err:
    '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
https://www.experts-exchange.com/articles/31204/How-to-close-an-Access-report-by-pressing-Escape-Esc-key.html
0
 
Ryan ChongCommented:
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...
0
 
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 ?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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, ...so when you open it (again, ....in Print Preview),...you 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, ...my 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, ...you 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.

;-)

JeffCoachman
0
 
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, ...it can sometimes cause more problems than it solves.

Finally, ...look at all the code involved to do this...
:-O
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...

JeffCoachman
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.