How to return the focus to a textbox on a Userform?

dodgebros

New Member
Joined
Feb 9, 2016
Messages
2
I have a userform with a button and a textbox; on the form activate event the focus is set to the textbox. The user will be using a barcode scanner to scan data into the textbox. On the AfterUpdate event of the textbox a routine runs to verify the data they scanned in. If all is well, the routine then prints a label with more barcodes to be verified.

I have the button and textbox tabstop set to true; these the only two controls on the userform with their tabstop property set to true.

The goal it to have the user scan a barcode, then the AfterUpdate event code runs, then the focus is returned to the textbox for it receive the next bit of data. BTW, the only purpose of the button is to offer the user a way to manually set the focus back to the textbox.

After several of the barcodes are scanned a label is printed, then the barcodes on these labels are also scanned; i.e. the label is printed in the middle of the routine). Everything works great as in the focus gets returned to the textbox, until it prints the label. I have tried everything I can find on the web to get the focus back to the textbox after the label prints but have not been successful.

I have tried Sendkeys, calling the click event of the button at the end of the textbox AfterUpdate routine, plus other things.

What always seems to happen is the focus hangs at the button. Manually clicking the button moves the focus to the textbox but calling the same click event from the AfterUpdate event DIRECTLY AFTER PRINTING THE LABEL just leaves the focus at the button, other times it does move the focus to the textbox.

I cannot post all of the code for propriety reasons but ask and I'll post what I can. Any help will be greatly appreciated!
TD



BUTTON CODE:

Me.txtInput.SetFocus



AfterUpdate Label printing code:

Call PrintLabel()

Module ModMain:

PrintLabel ():

'print label code
'set label sheet as activesheet
ThisWorkbook.Sheets("ABC").Activate
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(0.1)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0.15)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
End With
Application.PrintCommunication = True
Sheets("ABC").PrintOut
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi and welcome to the board.

I would try "DoEvents" after the .SetFocus or after Call PrintLabel ()

Gary
 
Upvote 0
Hi and welcome to the board.

I would try "DoEvents" after the .SetFocus or after Call PrintLabel ()

Gary


Hi Gary and thanks for trying to help. I had already tried putting DoEvents after the "Call PrintLabel" and just now I tried putting DoEvents before then after the "Me.txtInput.SetFocus" under the button and none of these solved the problem.

HOWEVER, right after that I was reading a post from another forum and the person mentions setting the "Default" property of buttons on userforms, so I set this property for my button to True and now it works!!!

So, for the next person with this issue, here is what I now have that appears to work:

At the end of the routine under the textbox AfterUpdate event:

Application.SendKeys "{TAB}"
Application.SendKeys "{BACKSPACE}"


Under the button:

Application.SendKeys "{TAB}"
Application.SendKeys "{BACKSPACE}"


Thanks!
TD
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
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