Help Required With Barcode Being Read Into A Userform...

sxhall

Board Regular
Joined
Jan 5, 2005
Messages
237
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a simple userform (for now!) that has two fields/textboxes on it that need to populate when a barcode is scanned.

Problem I have is that I need to populate both with a barcode, mainly for speed, although a user could still type in if they wanted or needed to. One scan would be barcode from a sheet of paper related to the task an employee is doing and the other would be an ID for the user. As I controll the user ID's I am going to prefix them all with 000 as the other barcodes will not have this as a prefix.

What I have tried unsuccesfully to do is to populate Textbox2 ('Scan Read') with the barcode scanner and from there determine if the data should be entered in to either Textbox1 or Textbox3.

I have removed the automatically added 'Tab' from the barcode reader so it stays in the textbox and tried adding in a carriage return to move the cursor but this did not work either. Was looking to see if there is a way to catch the barcode scan instead of populating a textbox but was not able to find anything along this avenue...!

Any help with the code below on how to capture the barcode reading and then determine what textbox the result needs to go in to would be greatly appreciated.

VBA Code:
Private Sub UserForm_Initialize()
TextBox2.SetFocus
End Sub

Private Sub TextBox2_Update()

Dim ScanRead As String
ScanRead = CStr(TextBox2.Value)

MsgBox ScanRead

If ScanRead = "" Then

    Else

    If Left(ScanRead, 3) = "000" Then
    TextBox3.Value = ScanRead
    Else
    TextBox1.Value = Right(ScanRead, Len(ScanRead) - 3)
    End If

End If

TextBox2.Value = ""
SetFocus.TextBox2

End Sub

My simple userform showing the top two textboxes that I need to populate and the scan read textbox I am using to capture the barcode scan.
1698237541023.png



Thanks in advance for any help.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What happens if you leave the automatically added 'Tab' from the barcode reader and use _BeforeUpdate instead of _Update ?
I believe once the textbox events get to update focus has to leave the textbox, but Cancel=True in BeforeUpdate maintains the focus.

Try something along the lines of
VBA Code:
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    ' if blank
    If Me.TextBox2 = "" Then Exit Sub
    ' if too short
    If Len(Me.TextBox2) < 4 Then
        Cancel = True
        Exit Sub
    End If
    ' ok, deal with scan
    If Left(Me.TextBox2, 3) = "000" Then
        Me.TextBox1 = Me.TextBox2
    Else
        Me.TextBox3 = Me.TextBox2
    End If
    ' reset to blank
    Me.TextBox2 = ""
    ' stay there
    Cancel = True

End Sub
 
Upvote 1
Solution
What happens if you leave the automatically added 'Tab' from the barcode reader and use _BeforeUpdate instead of _Update ?
I believe once the textbox events get to update focus has to leave the textbox, but Cancel=True in BeforeUpdate maintains the focus.

Try something along the lines of
VBA Code:
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    ' if blank
    If Me.TextBox2 = "" Then Exit Sub
    ' if too short
    If Len(Me.TextBox2) < 4 Then
        Cancel = True
        Exit Sub
    End If
    ' ok, deal with scan
    If Left(Me.TextBox2, 3) = "000" Then
        Me.TextBox1 = Me.TextBox2
    Else
        Me.TextBox3 = Me.TextBox2
    End If
    ' reset to blank
    Me.TextBox2 = ""
    ' stay there
    Cancel = True

End Sub

Superb NoSparks, this works 100% correctly exactly what I was trying to achieve 👏

Would never have thought of this approach! Still shows I have more learning to do :)

I can now move on and build this in to the file I've already built previously which was all typed in manually.

Thank you so much.

Steven
 
Upvote 0
You're welcome, glad I could assist and thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,932
Members
452,539
Latest member
delvey

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