1

I'm having problems with losing focus on textbox when pressing enter. I know there is a similar question already solved, but it just doesn't work for some reason.

This code detects an enter pressed while in the MeasuredDim textbox and then calls the NextDim_click sub. NextDim_click also works through a commandbutton and has also MeasuredDim.SetFocus included and it works fine through the button press, but when calling NextDim_click after Enter press, the focus is lost on the whole UserForm.

How is it not working for me?

 Private Sub MeasuredDim_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
    Call NextDim_Click
    KeyCode = 0
    MeasuredDim.SetFocus
    End If
    End Sub
Community
  • 1
  • 1
Jukkiz90
  • 13
  • 4

1 Answers1

0

This works for me:

Private Sub MeasuredDim_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
        Call NextDim_Click
        KeyCode = 0
        Me.MeasuredDim.SetFocus
    End If
End Sub

Private Sub NextDim_Click()
    MsgBox "NextDim_Click"
End Sub

Notice the line

Me.MeasuredDim.SetFocus

where the Me keyword fully qualifies the statement.

Miqi180
  • 1,670
  • 1
  • 18
  • 20