Excel VBA Keep Focus on TextBox

hendyhellen

New Member
Joined
Jul 29, 2023
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hello all,

How to make the cursor always focus on the textbox after user pressing enter on textbox ?

Using this code, the cursor not focus on textbox after input and pressing enter

VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    Me.TextBox1.SetFocus
    Cancel = True
End If
End Sub

Thank you all~
 
The details of code that i want is, open the textbox for user input the barcode -> then excel find the data on other sheet -> data match the label will be print out -> focus again on text box 🙏
Could you explain what you're trying to do in more detail?
I'm not familiar with scanning barcode in Excel.
When you scanning the barcode, where the barcode should go? in the textbox? and in order to do that you need the focus at the textbox first?
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
PERFECT!!, now the code works as what i needed

Thank you so much for the code.
You help me out as much as you can.
It really means a lot

You have my deepest thanks. All the very best for you
Have a great day Micron~🙏
In a test copy of your file try this:
- delete Module1, 3 and 4
- change Module2 to:
Option Explicit '<< don't duplicate this line anywhere

Public Product As String

VBA Code:
Sub Print_Label()

On Error GoTo ErrHandler
With Sheets("Label")
    .Range("A4") = Product
    .PrintOut
    .PrintOut
End With

Exit Sub
ErrHandler:
MsgBox "BARCODE DOES NOT EXIST", vbInformation, ""

End Sub
In ThisWorkbook module at the top:
Option Explicit

In same module, create ThisWorkbook.Open event and paste in:
VBA Code:
Private Sub Workbook_Open()
    'Application.Visible = False '<< you can cycle this on/off as it suits you
    UserForm1.Show vbModeless
End Sub

Comment out all the Textbox1 BeforeUpdate code, or delete the whole sub
Add CommandButton1.click event, then paste this code into it:
VBA Code:
If Len(TextBox1.Text) <> 0 Then
     Product = UCase(TextBox1.Value)
     Print_Label
     Me.TextBox1.Text = "" 'vbNullString
     Cancel = True
End If

Me.TextBox1.SetFocus
That works for me.
The reason you see a different view is because as I noted, not all of your code runs for you the way you have it.
 
Upvote 0
Sorry to interrupt you again,
the code works, but i must click the button then print out label works
is there a way to make event like this
Input Code (my barcode scanner already set ENTER after scanning) - ENTER - PRINT

the vba code that you make now is
Input Code - click the command button - PRINT

Thanks once again~
Glad I could help & thanks for the recognition!
 
Upvote 0
Based on research and lack of experience with scan guns I did this, which seems to work. I suggest you read all before attempting.
In form design view...
Add another textbox, anywhere, any size. I first put it under command button then moved it over top of button
Set tab stop property to False for all controls
For both textboxes
- set EnterKeyBehavior property to false
- set TabStop property to false
Right click on Textbox1, choose View Code from context (right click) menu.
Select KeyUp from events list
1690897522602.png


Put this line in the KeyUp event
If KeyCode = 13 Then Call CommandButton1_Click
Change button click code to this:
VBA Code:
change to this
Private Sub CommandButton1_Click()

If Len(TextBox1.Text) <> 0 Then
     Product = UCase(TextBox1.Value)
     Print_Label
     Me.TextBox1.Text = "" 'vbNullString
     Cancel = True
End If

Me.TextBox2.SetFocus
Me.TextBox1.SetFocus
'Me.TextBox1.SelStart = 1 'try enabling this line if focus doesn't go to textbox1

End Sub

Now you can drag Textbox2 over top of button and hide it under button:
1690896990442.png


If you need to get at textbox2, pick it from this list
1690897024248.png


Hopefully I didn't forget anything all that will fix your problem.
 
Upvote 1
Solution
Based on research and lack of experience with scan guns I did this, which seems to work. I suggest you read all before attempting.
In form design view...
Add another textbox, anywhere, any size. I first put it under command button then moved it over top of button
Set tab stop property to False for all controls
For both textboxes
- set EnterKeyBehavior property to false
- set TabStop property to false
Right click on Textbox1, choose View Code from context (right click) menu.
Select KeyUp from events list
View attachment 96370

Put this line in the KeyUp event
If KeyCode = 13 Then Call CommandButton1_Click
Change button click code to this:
VBA Code:
change to this
Private Sub CommandButton1_Click()

If Len(TextBox1.Text) <> 0 Then
     Product = UCase(TextBox1.Value)
     Print_Label
     Me.TextBox1.Text = "" 'vbNullString
     Cancel = True
End If

Me.TextBox2.SetFocus
Me.TextBox1.SetFocus
'Me.TextBox1.SelStart = 1 'try enabling this line if focus doesn't go to textbox1

End Sub

Now you can drag Textbox2 over top of button and hide it under button:
View attachment 96368

If you need to get at textbox2, pick it from this list
View attachment 96369

Hopefully I didn't forget anything all that will fix your problem.
This works 100%

Thank you so much once again for spending your time helps me with this code (y) 🙏
 
Upvote 0
Glad to have been able to help. Good thing too because I was at the end of my rope.
 
Upvote 1

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top