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~
 
Did you try the edited code I posted? AFAIK, if you cancel the event, focus should stay where it is. If it doesn't work for you and the file you posted is a copy of the file you're working with, did you check the copy to ensure this problem was also in the copy?

If the situation was that this copy behaved the same way for me, I might be able to help. Since it's not failing for me I can't suggest anything else.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Yes, i already replaced the code with this one

I will try and check on another computer
I work with the same file that i posted at the moment

Thank you so much for your help, i will inform you if i can resolve this issue on my side (y)

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

Did you try the edited code I posted? AFAIK, if you cancel the event, focus should stay where it is. If it doesn't work for you and the file you posted is a copy of the file you're working with, did you check the copy to ensure this problem was also in the copy?

If the situation was that this copy behaved the same way for me, I might be able to help. Since it's not failing for me I can't suggest anything else.
 
Upvote 0
What about the suggestion in post 3 to make the textbox the active control?
 
Upvote 0
Didn't work on my side too for post in #3 :(

I will send you the video that is shown my problem
and here is the updated file

Updated File
Video

Please check if i make the mistakes :cry:
I need the cursor always focus on the textbox, because I always doing continuous scan for many items on the warehouse.
So i can always focus on the barcode reader without click on textbox

Thank you 🙏
What about the suggestion in post 3 to make the textbox the active control?
 
Upvote 0
I can replicate the issue if I allow your printout sub to run (but I have to disconnect from my printer). I think your problem is due to several factors:
- beforeupdate isn't the best event for this. I'd either use command button click (which you don't have) or textbox afterupdate.
- when you set tb to vbNullString, the event runs again. Now that the control contains no value, this
If Len(TextBox1.Text) <> 0 Then
causes the rest of the code to be skipped. At least that's my theory for now. I'm going to move stuff around and test. BTW, Me is an invalid reference in a standard module:
Private Sub UserForm_Initialize()
Me.TextBox1.SetFocus
End Sub
Also, you don't need a module for every procedure. Is there a reason why you don't just have the printout somewhere in the userform code? Unless you plan to call the printout sub from other form actions, it doesn't need to be on its own either. Code is easier to work with if it's consolidated more.
 
Upvote 0
Hello Micron,

Sorry for my bad code, I just learn VBA a week ago and I think my foundation is wrong.
Just learning from the google.

So, could you help me to repair the code ?

My plan is just make the simple code for warehousing purpose
Textbox -> for input using barcode scanner
then VLOOKUP the data that match with the QR Code and printout the details

Thank you so much for your help
Appreciate your desire to help me and others who struggle with VBA code~

I can replicate the issue if I allow your printout sub to run (but I have to disconnect from my printer). I think your problem is due to several factors:
- beforeupdate isn't the best event for this. I'd either use command button click (which you don't have) or textbox afterupdate.
- when you set tb to vbNullString, the event runs again. Now that the control contains no value, this
If Len(TextBox1.Text) <> 0 Then
causes the rest of the code to be skipped. At least that's my theory for now. I'm going to move stuff around and test. BTW, Me is an invalid reference in a standard module:
Private Sub UserForm_Initialize()
Me.TextBox1.SetFocus
End Sub
Also, you don't need a module for every procedure. Is there a reason why you don't just have the printout somewhere in the userform code? Unless you plan to call the printout sub from other form actions, it doesn't need to be on its own either. Code is easier to work with if it's consolidated more.
 
Upvote 0
Now that I've moved your code around there is code running that wasn't before. Result is that I now have no application window and if I close the form, no easy way to expose excel app window. Are you really wanting to hide the app window?
It did work for me now, but I want to experiment to see if the change is due to one or a combination of changes. Going to be a bit of a pain having to go to Task Manager to shut down Excel and maybe lose changes I made. I'll try to show the app window first but don't know how.
 
Upvote 0
at my side the app window is open in front of the excel worksheet, still the same like before nothing change
Screenshot_19.png

sorry for the trouble cause on your side
Thank you so much~
You've been really helpful for me
Now that I've moved your code around there is code running that wasn't before. Result is that I now have no application window and if I close the form, no easy way to expose excel app window. Are you really wanting to hide the app window?
It did work for me now, but I want to experiment to see if the change is due to one or a combination of changes. Going to be a bit of a pain having to go to Task Manager to shut down Excel and maybe lose changes I made. I'll try to show the app window first but don't know how.
 
Upvote 0
I would like to make the textbox always focus after pressing enter, so after scanning the barcode i don't need to click the mouse again on the textbox
SCAN -- PRINT --- SCAN -- PRINT
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
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

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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