PDA

View Full Version : Userform Enter key



nbrown6
09-03-2019, 11:00 AM
Hi all,

Again i come cap in hand after not being able to work something out.


I have a userform which i am trying to use with a hand scanner input. As the scanner effectively presses the CR at the end of the string, im using the textbox1_exit event to trigger the code to deal with the string.

I then have it setting the textbox.value to "" to clear the box.

But i would like it to then become the focus again. Using the userform.TextBox1.setfocus it works, but only if i step through the code. As soon as the _exit event finishes, it then selects the next control.

Im trying to find a control that acts on the enter key being pressed?

Cheers All

paulked
09-03-2019, 11:23 AM
You could use the Tag in this fashion:



Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Do your stuff
'....
'Stay here = yes
Tag = 1
'Stay in here = no
Tag = 0
End Sub


'Then whichever control gets focus next use...

Private Sub TextBox2_Enter()
If Tag = 1 Then TextBox1.SetFocus
End Sub

'or...


Private Sub CommandButton1_Enter()
If Tag = 1 Then TextBox1.SetFocus
End Sub

Kenneth Hobs
09-03-2019, 12:16 PM
If commandbutton1 is the next taborder, then:

Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'If KeyCode = Asc(vbCr) Then
If KeyCode = 13 Then
TextBox1 = ""
TextBox1.SetFocus
End If
End Sub

nbrown6
09-04-2019, 07:22 AM
If commandbutton1 is the next taborder, then:

Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'If KeyCode = Asc(vbCr) Then
If KeyCode = 13 Then
TextBox1 = ""
TextBox1.SetFocus
End If
End Sub

Thanks Kennith, that works a treat.

大灰狼1976
09-06-2019, 11:32 PM
Hi nbrown6!
Maybe this example might help you a little.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1 = "" Then Exit Sub
Sheets(1).Cells(Rows.Count, 1).End(3).Offset(1) = TextBox1
TextBox1 = ""
Cancel = True
End Sub