【问题标题】:How to trap vbKeyReturn only in VBA Access如何仅在 VBA Access 中捕获 vbKeyReturn
【发布时间】:2019-04-10 16:49:11
【问题描述】:

我在subForm 中有一个textbox 控件,这是一个singleForm。但是,当我按下enter 键时,它会添加一个new record,而之前的record 会隐藏起来。我已经将form property 设置为current record

我使用下面的代码来捕获enter 键,这很有效。

Private Sub txt_1_KeyDown (KeyCode As Intger, Shift as Integer)

Select case KeyCode
    Case vbKeyCode
        KeyCode = 0
        Me.parent.Combo.SetFocus
End Select

End Sub

但是,现在我无法在textbox 中使用Ctrl + Enter 添加new line,因为一旦我按下enter,代码就会触发。有人可以帮助如何更改上面的代码,使其仅捕获enter 键并保持默认的访问行为,即在按Ctrl + Enter 时添加新行。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    您将需要一个条件表达式测试:

    • Enter 键(ASCII 字符 13)已按下
    • 事件处理程序的 Shift 参数为零,表示没有与 CtrlShiftAlt >Enter 键。

    代码可能如下所示:

    Private Sub txt_1_KeyDown(KeyCode As Intger, Shift As Integer)
        If KeyCode = 13 And Shift = 0 Then
            KeyCode = 0
            Me.Parent.Combo.SetFocus
        End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      您需要像这样检查Shift 参数:

      Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
         If KeyCode = vbKeyReturn Then
            If Not Shift = acCtrlMask Then
               MsgBox "Enter"
               KeyCode = 0
               Me.parent.Combo.SetFocus
            End If
         End If
      End Sub
      

      【讨论】:

      • 感谢您的简单提示。请通过将vbCtrlMask 替换为acCtrlMask 对您的代码进行轻微更正。那么它工作正常。
      【解决方案3】:

      尝试检查关键代码:

      Private Sub txt_1_KeyDown(KeyCode As Intger, Shift as Integer)
      
          Select Case KeyCode
              Case vbKeyEnter
                  KeyCode = 0
                  Me.parent.Combo.SetFocus
              Case Else
                  ' Leave key code as is.
          End Select
      
      End Sub
      

      【讨论】:

      • 我试过了,但没用。我找到了@Lee Mac 的答案作为解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2011-12-02
      • 2022-12-18
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多