Close and reopen a userform but force the focus to same text box that trigered the VBA?

Mick Peters

Board Regular
Joined
May 18, 2015
Messages
93
Hi I have a user form with 3 input text boxes which is only allowed to use inputs from a barcode scanner , no keyboard no mouse. For the most part it works and people scan in and out without issue. Sometime however it is as if the scanner forgets to send the Enter that is programed at the end of each scan. This leaves the user unable to scan anything into the next text field and anything else scanned just adds on to the end of the first scan and still no enter. I thought I had a solution, using the on change event, as the on exit event is not triggered, by counting the number of letters in the text box and if it exceeds a certain amount then close and re open the user form. This is all I have to do to fix it when I am called to it and attach the keyboard and mouse. I have used the below code and it closes and re opens the form OK but the focus is in text box 2 (txtLocation)not text box 1 (txtName) as per the (now commented out) code. So I changed it to the below so it would tab twice once opened, this would move it from Textbox 2 to text box 1 but that also does not work. I have a Private Sub UserForm_Initialize() that is used at the first start of the form when the spread sheet is opened but that fails if I do an Application.Run saying that the values are no optional. Given the choice I would prefer to call the Private Sub UserForm_Initialize() after closing as that has more detail about the form settings in it

Code:
Private Sub txtName_Change()
If Len(TxtName.Text) > 9 Then
Unload Ureg
Call Save
Application.WindowState = xlMinimized
Ureg.Show
'Ureg.txtName.SetFocus
Ureg.Repaint
Application.SendKeys "{TAB 2}", True
'Ureg.txtName.SetFocus
Else
Exit Sub
      End If
End Sub

Private Sub UserForm_Initialize()
Code:
Private Sub UserForm_Initialize()
With Application
        .WindowState = xlMaximized
        Zoom = Int(.Width / Me.Width * 100)
        Width = .Width
        Height = .Height
    End With
txtDate.TextAlign = fmTextAlignCenter
TxtName.TextAlign = fmTextAlignCenter
txtLocation.TextAlign = fmTextAlignCenter
txtDirection.TextAlign = fmTextAlignCenter
TxtLastScan.TextAlign = fmTextAlignCenter
txtDate.Value = Format(Date, "dd/mm/yyyy")
Ureg.TxtName.SetFocus
End Sub

Thank you for reading , Any help would be greatly appreciated.
Thanks
Mick.
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I would appear that no further code is being processed after the UReg.Show until something is scanned is there a continue step I need to add in?
Thanks
Mick/
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,528
Members
449,316
Latest member
sravya

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