Search code examples
vbauserformbarcode-scannerscanning

Creating a Userform to compare 4 scanned items


I apologize if this seems very simple. I am new to VBA and am struggling to determine where to begin.

I am trying to create a Userform where you scan a sample, and then 3 tubes that should match the first sample, and if they do, it will send that data to a spreadsheet, and if they do not, then it will highlight Red and make you restart.

I am running into a few problems. I am not sure how to auto enter to the next text box after the first sample is scanned. For ease of use, I would like the user to just be able to scan, scan, scan, scan without interaction with the spreadsheet.

I also have tried to make use of the AutoTab function by putting in an If/Else statement, saying if value of the textbox is NOT null, it will AutoTab to the next box in the tab index, but that does not seem to be working

Please let me know of any advice you may have.


Solution

  • Basically, the input of the barcode scanner is the same as if someone enters the data manually - just all characters come at once (as if it is pasted). Usually, the scanner should be able to send some End-sign, in that case you should be fine as TAB or ENTER will jump to the next field automatically.

    If the scanner really send the data, you have to deal with the Change-Event of the text boxes and check if the data is valid. The following example checks for a length of 8 characters. Adapt the CheckBarcodeInput to your needs. If a valid barcode was entered, the focus is set to the next textbox.

    Private Sub TextBox1_Change()
        If CheckBarcodeInput(Me.TextBox1.Text) Then Me.TextBox2.SetFocus
    End Sub
    
    Private Sub TextBox2_Change()
        If CheckBarcodeInput(Me.TextBox2.Text) Then Me.TextBox3.SetFocus
    End Sub
    (...)    
    
    Private Function CheckBarcodeInput(s As String) As Boolean
        ' Check if barcode is valid. In this example, it simply checks the length.
        If Len(s) >= 8 Then CheckBarcodeInput = True
    End Function