Useform Focus Event after scans

Akw47

Board Regular
Joined
Nov 6, 2020
Messages
90
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,
Is it possible to write for me a code such that after scanning in numbers into textbox.1, it will directly set focus to textbox.2 Then after the number is scanned again and it correspond to the number in textbox.2. The focus will go back to the original textbox.1.

Thank you excel users.
 
Which barcode setting are you talking about in excel
It is not in Excel, but in the barcode scanner's settings application that is installed with its driver in Windows (or Mac or another OS that you are using). Like a printer's settings. As I said, I cannot make sure if you scanner supports it or not, but worth a try.

TabStop and TabIndex properties are the form control's properties that you can set in the VBE. To see the project properties, in edit mode, select the text box on the user form, and hit F4 if it is not already displayed in the left pane.

Screen Shot 2020-11-16 at 1.13.09 AM.png
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Okay but how to know what number to put in tabstop and tabindex
It is not in Excel, but in the barcode scanner's settings application that is installed with its driver in Windows (or Mac or another OS that you are using). Like a printer's settings. As I said, I cannot make sure if you scanner supports it or not, but worth a try.

TabStop and TabIndex properties are the form control's properties that you can set in the VBE. To see the project properties, in edit mode, select the text box on the user form, and hit F4 if it is not already displayed in the left pane.

View attachment 26128
 
Upvote 0
TabStop property controls whether a user can press Tab (or Enter for textbox control as I tried to explain) to move the focus to the next control which has the next TabIndex value. It is True as default, but you can set it as False if you don't want a particular control to not have focus.

For example, in your case, you can set tab stop for only text boxes, then the barcode scanner loop through text boxes on each scan.

TabIndex defines the next control that will have the focus when the user press Tab.

So, if you have two text boxes on the user form, you can set the first one's (TextBox1) TabIndex property to be 0 (zero), and the next one's (TextBox2) to be 1. In this case, TextBox1 will have the focus initially when you open the user form, and TextBox2 will take the focus as soon as you leave TextBox1.

Hope I was able to explain it. If not, then the links I provided could help better.
 
Upvote 0
TabStop property controls whether a user can press Tab (or Enter for textbox control as I tried to explain) to move the focus to the next control which has the next TabIndex value. It is True as default, but you can set it as False if you don't want a particular control to not have focus.

For example, in your case, you can set tab stop for only text boxes, then the barcode scanner loop through text boxes on each scan.

TabIndex defines the next control that will have the focus when the user press Tab.

So, if you have two text boxes on the user form, you can set the first one's (TextBox1) TabIndex property to be 0 (zero), and the next one's (TextBox2) to be 1. In this case, TextBox1 will have the focus initially when you open the user form, and TextBox2 will take the focus as soon as you leave TextBox1.

Hope I was able to explain it. If not, then the links I provided could help better.
smozgur it works, however it does not go back to textbox1 as soon as I leave textbox 2. I was trying to see if it can loop.
 
Upvote 0
however it does not go back to textbox1 as soon as I leave textbox 2. I was trying to see if it can loop.

It is because there is another control which has TabStop = True on the user form.

To make a loop as you want, having focus from one text to another, only the text boxes must have TabStop = True. The easiest way to make sure about it, selecting all controls on the user form, setting TabStop = False (you can do that, because TabStop property is a shared control property), then deselect all controls and select only text boxes, and finally set them TabStop = True.

This is the VBA free way. So you won't even need to write a single line code.

However, this will restrict you to use mouse to click the command buttons or access the other controls on the user form.

It is also possible to do this with VBA since you can now detect Enter key code in the TextBox_KeyDown event procedure.
In that case, you need to use the following codes:

VBA Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 If KeyCode = 13 Then
    With TextBox2
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
 End If
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 If KeyCode = 13 Then
    With TextBox1
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
 End If
End Sub

Setting the .SelStart and .SelLength makes sure highlighting the text in the text boxes.

The same thing can be also accomplished by checking the text length in the same event procedure, but if the entry text is not supposed to be a fixed length, then it won't help.

I tried to provide possible solutions for each situation. I hope it helps.
 
Upvote 0
It still don't return to the textbox.1 even with your code above. But i have to check with you what does keydown means. It is because I am actually using enter to register data in my Excel. & when clicked tab, it does not move to the next textbox (interestingly).
 
Upvote 0
You can catch keystrokes with the KeyDown event, and you can catch Enter key entry by using only KeyDown (or KeyUp but no need to get confused with that event). The code I wrote above checks the key code pressed, and executes the necessary action if it is Enter key - KeyCode = 13.

Unfortunately your project depends on an external device that I have no chance to test, so this is the all information I can provide, and all should work in normal conditions. Perhaps you should start with a brand new user form, and test my suggestions separately. You can test the VBA method only first. Try testing it by entering numbers from the keyboard first, so first make sure how it works with keyboard. This you can also get familiarized with the VBA code, and events. When you see how it work with keyboard, then you can try with the barcode scanner. Normally it should fire the KeyDown event.
 
Upvote 0
You can catch keystrokes with the KeyDown event, and you can catch Enter key entry by using only KeyDown (or KeyUp but no need to get confused with that event). The code I wrote above checks the key code pressed, and executes the necessary action if it is Enter key - KeyCode = 13.

Unfortunately your project depends on an external device that I have no chance to test, so this is the all information I can provide, and all should work in normal conditions. Perhaps you should start with a brand new user form, and test my suggestions separately. You can test the VBA method only first. Try testing it by entering numbers from the keyboard first, so first make sure how it works with keyboard. This you can also get familiarized with the VBA code, and events. When you see how it work with keyboard, then you can try with the barcode scanner. Normally it should fire the KeyDown event.
Hi, just checking with you, I need to take 4 tabs to go back to textbox.1. Not sure why? but after first tab goes to textbox2, then tab 2 more times, not sure where cursor goes before going back to textbox.1
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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