I have a textbox control in a subForm which is a singleForm. However, when I press enter key it adds a new record and the previous record hides away. I have the form property already set to current record.

I used the code below to trap enter key, that works.

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

However, now I cannot add a new line using Ctrl + Enter within the textbox since the code fires as soon as I press enter. Can someone help how to change the code above so that it only traps enter key and keeps the default Access behavior of adding new line on pressing Ctrl + Enter.

3 Answers

3
Lee Mac On Best Solutions

You will need a conditional expression testing that:

  • The Enter key (ASCII character 13) has been pressed
  • The Shift argument of the event handler is zero, indicating no Ctrl, Shift, Alt have been pressed in conjunction with the Enter key.

The code might look something like this:

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
Brian M Stafford On

You need to check for the Shift argument like this:

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
0
Gustav On

Try checking for the key code:

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