vba 按 Enter 时防止移动到下一条记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/543147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:37:56  来源:igfitidea点击:

Prevent moving to next record when hitting Enter?

ms-accessvbaaccess-vbams-access-2003

提问by Michael Itzoe

I have a form in Access 2003 that should only be working with a single record. I can set the Cycleproperty to Current Record, but the form still jumps to the next record when I press Enter. My first thought was a KeyPreviewproperty, but I'm not seeing one. My other thought is maybe the KeyPressor KeyUpevent, but I thought I'd ask in case of unintended consequences. Any ideas?

我在 Access 2003 中有一个表单,它只能处理一条记录。我可以将Cycle属性设置为Current Record,但是当我按 Enter 时,表单仍然跳转到下一条记录。我的第一个想法是KeyPreview财产,但我没有看到。我的另一个想法可能是KeyPressKeyUp事件,但我想我会问,以防出现意外后果。有任何想法吗?

采纳答案by DJ.

The Cycle property only affects the TAB key.

Cycle 属性只影响 TAB 键。

To control the behaviour of the Enter Key that's a global property.

控制作为全局属性的 Enter 键的行为。

Go to Tools/Options - Keyboard tab and on "Move After Enter" select "Next Field"

转到工具/选项 - 键盘选项卡,然后在“输入后移动”上选择“下一个字段”

There are KeyPress and KeyDown events that you can use to trap the Enter key too but that's more work.

您也可以使用 KeyPress 和 KeyDown 事件来捕获 Enter 键,但这需要更多的工作。

回答by RubberDuck

This can also be done in vba.

这也可以在 vba 中完成。

Application.GetOption "Move After Enter" 'get current setting
Application.SetOption "Move After Enter", 0 'don't move
Application.SetOption "Move After Enter", 1 'Next Field
Application.SetOption "Move After Enter", 2 'Next Record

http://www.pcreview.co.uk/forums/enter-key-moving-next-field-t3454281.html

http://www.pcreview.co.uk/forums/enter-key-moving-next-field-t3454281.html

回答by Yusuf N.

The moment keys like TAB, Alt, PgUP, PgDn, Enterare pressed at the end of adding the record (after the last field mostly), the database auto saves all the info you entered on the form and wipes out the fields so that you can enter the next value. So what happens is that the data is saved but the form looks empty.

当下键,如TABAltPgUPPgDnEnter在添加记录(主要是最后一个字段后)结束按下时,数据库自动保存你的表单中输入的信息,并消灭了领域,这样就可以进入下一个值. 所以发生的情况是数据被保存但表单看起来是空的。

3 things to do:

要做的3件事:

  1. Form Cycle Property set to Current Record.
  2. Form Key Preview Property set to Yes.
  3. Add following code to the KeyDown Event of the form:

    '33 - PgUp; 34 - PgDown; 9 - Tab; 18=Alt; 13=Enter
    
    Select Case KeyCode
    Case 33, 34, 18, 9, 13
    KeyCode = 0    
    Case Else
    'Debug.Print KeyCode, Shift
    End Select
    

    I found this while scouring the web and don't take credit/responsibility for the code but I don't know where I found it. Works for me!

  1. 表单循环属性设置为当前记录。
  2. 表单键预览属性设置为是。
  3. 将以下代码添加到表单的 KeyDown 事件中:

    '33 - PgUp; 34 - PgDown; 9 - Tab; 18=Alt; 13=Enter
    
    Select Case KeyCode
    Case 33, 34, 18, 9, 13
    KeyCode = 0    
    Case Else
    'Debug.Print KeyCode, Shift
    End Select
    

    我在搜索网络时发现了这个,并且不承担代码的责任/责任,但我不知道我在哪里找到它。对我来说有效!

回答by PFreddy

If you go into Access Options on the file page, go to Client Settings and the first setting will let you choose where your focus changes to when you press enter. At least in Access 2013.

如果您进入文件页面上的访问选项,请转到客户端设置,第一个设置将让您选择按 Enter 时焦点更改的位置。至少在 Access 2013 中。

回答by BIBD

The Cycle property only works with the Tab key.

Cycle 属性仅适用于 Tab 键。

There are two options you could pursue.

您有两种选择。

You could trap the Enter key in KeyDown/KeyUp/KeyPressed
- OR -
You could filter the data source to the one record you want them editing, and disable adding new records through that form.

您可以在 KeyDown/KeyUp/KeyPressed 中捕获 Enter 键
- 或者 -
您可以将数据源过滤到您希望它们编辑的记录,并禁止通过该表单添加新记录。

回答by Vinate

You can add below code to your form 'BeforeUpdate' event. If use want to move to next record, it will ask user to save then close the form before they can move to another recorde.

您可以将以下代码添加到表单“BeforeUpdate”事件中。如果用户想移动到下一个记录,它会要求用户保存然后关闭表单,然后才能移动到另一个记录。

  Private Sub Form_BeforeUpdate(Cancel As Integer)
    Select Case MsgBox("Save?", vbYesNo)
      Case vbYes
        DoCmd.Close
      Case vbNo
        Cancel = True
    End Select
  End Sub