access escape_如何通过按Escape(Esc)键关闭Access报告

access escape

In this case, few things we need to figure it out.

在这种情况下,我们几乎不需要弄清楚什么。

1. How to detect a Escape (Esc) key press in the opened report?

1.如何检测打开的报告中的Escape(Esc)键?

2. How to close the report after the detection of Escape key pressed?

2.检测到按Escape键后如何关闭报告?

To resolve this issue, some macro scripts can be added to resolve this.

若要解决此问题,可以添加一些宏脚本来解决此问题。

As quoted in this article: Introduction to macros

如本文中所引用: 宏介绍

A macro is a tool that allows you to automate tasks and add functionality to your forms, reports, and controls. For example, if you add a command button to a form, you associate the button's OnClick event to a macro, and the macro contains the commands that you want the button to perform each time it is clicked.

是一种工具,可让您自动执行任务并向表单,报表和控件添加功能。 例如,如果将命令按钮添加到窗体,则将按钮的OnClick事件关联到 ,并且包含您希望按钮在每次单击时执行的命令。

Test 1: Adding macro code in the Report_KeyDown event

测试1:Report_KeyDown事件中添加宏代码

First of all, we need to know what's the Report's KeyDown event.

首先,我们需要知道什么是Report的KeyDown事件。

As quoted in this article: Report.KeyDown Event (Access)

如本文中所引用: Report.KeyDown事件(访问)

The  KeyDown event occurs when the user presses a key while a report has the focus. This event also occurs if you send a keystroke to a report by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.

当用户在报表具有焦点的情况下按下某个键时,将发生KeyDown事件。 如果您通过使用宏中的SendKeys操作或Visual Basic中的SendKeys语句将击键发送到报表,也会发生此事件。

To add the event, we can:

要添加事件,我们可以:

1. Open the report in design view

1.在设计视图中打开报告

2. Go to the Property Sheet and click on Event tab

2.转到属性表,然后单击事件选项卡

3. Look for On KeyDown and select [Event Procedure], and then click the "..." button.

3.查找On KeyDown并选择[事件过程],然后单击“ ...”按钮。

4. Enter the following code in the Visual Basic Editor

4.在Visual Basic编辑器中输入以下代码

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

5. Save the report

5.保存报告

6. Open the report.

6.打开报告。

7. You will see that once you pressed the Escape (Esc) key, the report is closed.

7.您将看到,一旦按Escape(Esc)键,该报告将关闭。

This is pretty cool!

这太酷了!

Test 2: Adding macro code in the Report_KeyPress event

测试2:Report_KeyPress事件中添加宏代码

Similarly for Test 1, we need to know what's the Report's KeyPress event.

同样,对于测试1,我们需要知道什么是Report的KeyPress事件。

As quoted in this article: Report.KeyPress Event (Access)

如本文中所引用: Report.KeyPress事件(访问)

The  KeyPress event occurs when the user presses and releases a key or key combination that corresponds to an ANSI code while a report has the focus. This event also occurs if you send an ANSI keystroke to a report by using the SendKeys action in a macro or the SendKeys statement in Visual Basic.

当用户在报表具有焦点的情况下按下并释放与ANSI代码对应的键或组合键时,就会发生KeyPress事件。 如果通过使用宏中的SendKeys操作或Visual Basic中的SendKeys语句将ANSI按键发送给报表,也会发生此事件。

To add the event, we can:

要添加事件,我们可以:

1. Open the report in design view

1.在设计视图中打开报告

2. Go to the Property Sheet and click on Event tab

2.转到属性表,然后单击事件选项卡

3. Look for On Key Press and select [Event Procedure], and then click the "..." button.

3.查找按键按下并选择[事件过程],然后单击“ ...”按钮。

4. Enter the following code in the Visual Basic Editor

4.在Visual Basic编辑器中输入以下代码

Private Sub Report_KeyPress(KeyAscii As Integer)
    If KeyAscii = 27 Then 'Detect the escape key
        DoCmd.Close acReport, Me.Name
    End If
End Sub 

5. Save the report

5.保存报告

6. Open the report.

6.打开报告。

7. You will see that once you pressed the Escape (Esc) key, the report is closed.

7.您将看到,一旦按Escape(Esc)键,该报告将关闭。

This code is working!

此代码有效!

But recently after viewing a question posted in the forum: ESC not closing report after zoom

但是最近在查看论坛上发布的问题后: ESC缩放后未关闭报告

The Problem:

问题:

It seems that Access will throw an error when the report is in Print Preview mode. 

当报表处于“ 打印预览”模式时,Access似乎会引发错误。

1. For the KeyDown event in Test 1, it will throw with an error stated below.

1.对于测试1中的KeyDown事件,它将引发以下错误。

2. For the KeyPress event in Test 2, if the report is in zooming (not 100%), the code is not responsive and the report is not closed.

2.对于测试2中的KeyPress事件,如果报表正在缩放(不是100%),则代码无响应,并且报表未关闭。

To resolve these issues, we will make some amendments to the existing code by introducing a Win API call to simulate the close action to the report.

为了解决这些问题,我们将通过引入Win API调用来模拟对报表的关闭操作,对现有代码进行一些修改。

Test 3: Adding macro code in the Report_KeyDown event with a Win API call

测试3:使用Win API调用在Report_KeyDown事件中添加宏代码

Repeat the same steps as stated in Test 1 and this time we include the following code:

重复测试1中所述的步骤,这次我们包含以下代码:

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 

Now, the error is gone and the report can be closed successfully in both Report View and Print Preview.

现在,错误消失了,可以在“报表视图”和“打印预览”中成功关闭报表。

Test 4: Adding macro code in the Report_KeyPress event with a Win API call

测试4:使用Win API调用在Report_KeyPress事件中添加宏代码

Before we do the same by adding the Win API call just like what we did in Test3, it seems that the KeyPress event failed to trigger in Print Preview mode.

在像在Test3中一样添加Win API调用进行相同操作之前,似乎KeyPress事件未能在“打印预览”模式下触发。

Private Sub Report_KeyPress(KeyAscii As Integer)
    On Error GoTo Err
    If KeyAscii = 27 Then 'Detect the escape key
        DoCmd.Close acReport, Me.Name
    End If
    Exit Sub
Err:
    MsgBox Err.Number & ":" & Err.Description
End Sub 

There is no error detected in Report_KeyPress event while the report is in Print Preview mode. Hence, it seems that the KeyPress event is not a good choice to close a report that currently in Print Preview mode.

当报表处于“打印预览”模式时,在Report_KeyPress事件中未检测到错误。 因此,看来KeyPress事件不是关闭当前处于“打印预览”模式的报表的好选择。

Summary

摘要

To draw a conclusion, it seems that the result of Test 3 is best to suit the resolution of closing an Access report, which involves the combination of code from the Report_KeyDown event with a Win API call.

得出结论,看来Test 3的结果最适合关闭Access报告的解决方案,该方案涉及Report_KeyDown事件中的代码与Win API调用的组合

What Else to Consider?

还有什么要考虑的?

Yes, you may know that the test in Layout View is not included and I will discuss this in my next article.

是的,您可能知道不包括“ 布局视图”中的测试,我将在下一篇文章中对此进行讨论。

翻译自: https://www.experts-exchange.com/articles/31204/How-to-close-an-Access-report-by-pressing-Escape-Esc-key.html

access escape

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值