Re: Keeping The Focus From Changing. How?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




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

.



Relevant Pages