TextBox focus 4

T

Tim Coddington

I have a userform with two option buttons and a textbox.
I want the text box to always have focus (except I need to
be able to select the check boxes) so a user can input one
string, press 'Enter' to process, and enter the next string
repeatedly. Here is the total amount of code ...

Private Sub OptionButton1_Click()
TextBox1.SetFocus
End Sub
Private Sub OptionButton2_Click()
TextBox1.SetFocus
End Sub

Every time I type something into the text box and press
'Enter', the cursor disappears from the text box and I have
to click on it (or one of the option buttons) to get it back.

Can someone tell me what is happening and how to solve it??

PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE????
 
R

RB Smissaert

How about this:

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

If KeyCode = 13 Then
MsgBox "doing processing here"
TextBox1.SetFocus
KeyCode = 0
End If

End Sub


RBS
 
T

Tim Coddington

Thanks. This appears to work. Will test it totally on Monday.
What does the 'KeyCode = 0' statement do?
 
B

Bob Phillips

Tim,

Use the change event

Private Sub OptionButton1_Change()
TextBox1.SetFocus
End Sub

Private Sub OptionButton2_Change()
TextBox1.SetFocus
End Sub

--

HTH

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

RB Smissaert

It just resets the Keycode to zero.
So the normal result of the enter key won't happen.
Actually you don't need to set the focus as you are already in the textbox.
So this will do the same:

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

If KeyCode = 13 Then
MsgBox "doing processing here"
KeyCode = 0
End If

End Sub


RBS
 
T

Tim Coddington

_Change() isn't fired after <cr> in a text box.
Tried it in _Enter(), but then, you can't change the option buttons.
I'll try RB Smissaert's solution Monday, but am aprehesive about
its ability to work..
 
Top