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~
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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 1
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
Glad to have been able to help. Good thing too because I was at the end of my rope.
 
Upvote 1
This is on a userform?
Works for me in Excel 2016 (Office 365). If Len = 0 then of course it won't set focus but I don't expect that is your case. Note that if you're changing textbox via vba code then this event won't run until you move off of the control and even then it may not function as expected. I mention that only because if I alter textbox value via immediate window, the code runs but does not set the cursor in the textbox. That should not be an issue either since you say you're hitting Enter key.

Since you are using BeforeUpdate event you could try
VBA Code:
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    Cancel = True
End If
 
Upvote 0
Try setting your TextBox1 as the ActiveControl (if your form name is not "UserForm1" change according to your naming convention.)
e.g.
VBA Code:
   UserForm1.ActiveControl = TextBox1
   TextBox1.SetFocus

Here is a quick example. I have 3 TextBoxes and 3 corresponding OptionButtons.

The option buttons change which control is Active and has Focus (both are needed in Excel 2016)

1690658620677.png


Test Code:
VBA Code:
Private Sub OptionButton1_Click()
  SetActiveControlAndFocus TextBox1
End Sub
Private Sub OptionButton2_Click()
  SetActiveControlAndFocus TextBox2
End Sub
Private Sub OptionButton3_Click()
  SetActiveControlAndFocus TextBox3
End Sub

Sub SetActiveControlAndFocus(ctl As Control)
  UserForm1.ActiveControl = ctl
  ctl.SetFocus
End Sub
 
Upvote 0
Yes, you are correct on userform.
on userform after type something on textbox ---- ENTER --- then print out the label ---- after print out, i must click again on textbox then start typing again.


This is on a userform?
Works for me in Excel 2016 (Office 365). If Len = 0 then of course it won't set focus but I don't expect that is your case. Note that if you're changing textbox via vba code then this event won't run until you move off of the control and even then it may not function as expected. I mention that only because if I alter textbox value via immediate window, the code runs but does not set the cursor in the textbox. That should not be an issue either since you say you're hitting Enter key.

Since you are using BeforeUpdate event you could try
VBA Code:
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    Cancel = True
End If
 
Upvote 0
This one also not working, sorry since i am very newbie on VBA
Can i share the xlsm file here and then help me which code i make it wrong ?

https://www.mediafire.com/file/4m02cdrtiwafr83/Scanning-LABEL.xlsm/file

Try setting your TextBox1 as the ActiveControl (if your form name is not "UserForm1" change according to your naming convention.)
e.g.
VBA Code:
   UserForm1.ActiveControl = TextBox1
   TextBox1.SetFocus

Here is a quick example. I have 3 TextBoxes and 3 corresponding OptionButtons.

The option buttons change which control is Active and has Focus (both are needed in Excel 2016)

View attachment 96216

Test Code:
VBA Code:
Private Sub OptionButton1_Click()
  SetActiveControlAndFocus TextBox1
End Sub
Private Sub OptionButton2_Click()
  SetActiveControlAndFocus TextBox2
End Sub
Private Sub OptionButton3_Click()
  SetActiveControlAndFocus TextBox3
End Sub

Sub SetActiveControlAndFocus(ctl As Control)
  UserForm1.ActiveControl = ctl
  ctl.SetFocus
End Sub
 
Upvote 0
This one also not working, sorry since i am very newbie on VBA
Can i share the xlsm file here and then help me which code i make it wrong ?

https://www.mediafire.com/file/4m02cdrtiwafr83/Scanning-LABEL.xlsm/file
I can't download the file:
This site can’t be reached
Check if there is a typo in scanning-label.


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
Where did the focus go after you press Enter?
 
Upvote 0

Forum statistics

Threads
1,215,839
Messages
6,127,204
Members
449,368
Latest member
JayHo

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