I have reviewed questions on here and googled the issues but can't seem to get my code to work.
I have 2 textboxes on an Excel user form. Textbox1 is used to scan a barcode into, Textbox2 is to display a message. What I would like to happen is each time a barcode is scanned, Textbox2 displays a status, depending on the barcode scanned into Textbox1, Textbox1 gets focus and the barcode is highlighted ready for a new barcode to be scanned.
Here's example code for Textbox1:
Private Sub Textbox1_AfterUpdate()
Dim strBC As String
'Get barcode value
strBC = Me.Textbox1
'Check if BC is good
If strBC = "000000000000111" Then ' I'm using an example BC here
Me.TextBox2.BackColor = RGB(0, 255, 0) 'Green - good
Me.TextBox2 = "Good"
End If
'Check if BC is Bad
If strBC = "000000000000222" Then ' I'm using an example BC here
Me.TextBox1.BackColor = RGB(255, 0, 0) 'Red - bad
Me.TextBox2 = "Bad"
End If
End Sub
Here's the code for Textbox2:
Private Sub Textbox2_AfterUpdate()
'Private Sub Textbox2_Change()
'Private Sub Textbox2_GotFOcus()
'Private Sub Textbox1_LostFocus
'Check if event fired
Debug.Print "Fired"
With Textbox1
.SetFocus ' Set focus to the TextBox
.SelStart = 0 ' Start selection at the first character
.SelLength = Len(.Text) ' Select the entire length of the text
End With
So when the first barcode is scanned I see "Fired" in the intermediate window, the correct message appears in Textbox2 and the Barcode in Textbox1 is highlighted ready for another barcode. The problem occurs when I scan a second barcode (in this case it's the same as the first). The message in Textbox2 highlights this time, Textbox1 doesn't get focus and the barcode isn't highlighted. The after update event in Textbox2 doesn't fire either. I think this is because I am scanning the same barcode again, however; the good/bad barcodes could Bec scanned in any order e.g. good, good, bad, bad, good, bad etc.
Does anyone have any ideas how I can get Textbox1 to get focus and have its barcode highlighted irrespective of the barcode scanned?
Many thanks in advance!