Enter pressed in Textbox

  • Thread starter Thread starter embarrased
  • Start date Start date
E

embarrased

How do I signal my code to run when the user presses the
ENTER button in the Text Box TextBoxCustNum?

Private Sub TextBoxCustNum_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
?????????????????????????????
End Sub
 
Hi embarrased,

You need to use the KeyDown event for that, as the Enter key will never
trigger the KeyPress event:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
'/ ENTER key pressed
MsgBox "Done"
End If
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
Private Sub TextBoxCustNum_Exit(ByVal Cancel As MSForms.ReturnBoolean)

End Sub

is the event you want


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks Guys!

I couldn't get a msgbox to return the Value of
the 'Enter' Key; I guess it was because I was using the
KeyPress Event instead of the KeyDown Event. Dangit!

It's amazing how the stupid things sometimes stump me.
Thanks.
 
Hi Jake,

Thanks for the post and I applied your code to my
project. However, I now receive a fatal error and Excel
must close. My error handling notwithstanding, why would
the code execute when I trigger the button event with my
mouse, but not when I hit enter in the text box?

Here's the code for both events:

Private Sub CommandButtonExecute_Click()
RunWorksheet
End Sub

Private Sub TextBoxCustNum_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)

MsgBox KeyCode
If KeyCode = 13 Then ' ENTER key pressed
RunWorksheet
End If
End Sub

Thanks in advance.
-----Original Message-----
Hi embarrased,

You need to use the KeyDown event for that, as the Enter key will never
trigger the KeyPress event:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
'/ ENTER key pressed
MsgBox "Done"
End If
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

How do I signal my code to run when the user presses the
ENTER button in the Text Box TextBoxCustNum?

Private Sub TextBoxCustNum_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
?????????????????????????????
End Sub

.
 
Hi,

I don't know; I haven't seen that one before. If you'd like, you can email
the workbook directly to me at mvp<at>longhead[dot]com, and I'll see what I
can do.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

Hi Jake,

Thanks for the post and I applied your code to my
project. However, I now receive a fatal error and Excel
must close. My error handling notwithstanding, why would
the code execute when I trigger the button event with my
mouse, but not when I hit enter in the text box?

Here's the code for both events:

Private Sub CommandButtonExecute_Click()
RunWorksheet
End Sub

Private Sub TextBoxCustNum_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)

MsgBox KeyCode
If KeyCode = 13 Then ' ENTER key pressed
RunWorksheet
End If
End Sub

Thanks in advance.
-----Original Message-----
Hi embarrased,

You need to use the KeyDown event for that, as the Enter key will
never trigger the KeyPress event:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
'/ ENTER key pressed
MsgBox "Done"
End If
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

How do I signal my code to run when the user presses the
ENTER button in the Text Box TextBoxCustNum?

Private Sub TextBoxCustNum_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
?????????????????????????????
End Sub

.
 
Back
Top