View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Doug Glancy Doug Glancy is offline
external usenet poster
 
Posts: 770
Default Keeping The Focus From Changing. How?

Iain,

I couldn't get what you suggest to work the way abxy wants. There is no
cancel event in KeyDown event and SetFocus didn't bring it back to TextBox1.
I'd be interested to see the code you'd use.

Doug

"Iain King" wrote in message
oups.com...

Doug Glancy wrote:
abxy

You need a global variable to track whether enter was pressed. If it
was,
then do the actions as Tom described, else behave normally, i.e., you can
click or tab out of the textbox:

Dim enter_pressed As Boolean ' this is above all subroutines
Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If enter_pressed Then 'if it was set in the keydown event below
Cancel = True
Me.TextBox1 = ""
enter_pressed = False 'clear it, so back to normal behavior
End If
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal
Shift As Integer)
If KeyCode = 13 Then ' enter key
enter_pressed = True
End If
End Sub



or more simply, just put the code you want to run when enter is pushed
in the KeyDown event.

Iain