I’m working on a UserForm that displays product information including the barcode, product name, and price, all sourced from a ‘Products’ tab. Initially, I managed to get the barcode scanning and display of product name and price working for the first item. However, I encountered issues when scanning a second item – the product name and price labels didn’t update. Additionally, upon attempting to scan a third item, a new TextBox failed to appear as expected. Below is the code I have so far:
Private Sub BarcodeTextBox_Change()
Dim scannedCode As String
Dim foundProduct As Range
' Get the scanned barcode from the TextBox
scannedCode = BarcodeTextBox.Text
' Search for the scanned product code in the "Products" worksheet
Set foundProduct = Worksheets("Products").Columns(1).Find(What:=scannedCode, LookIn:=xlValues, LookAt:=xlWhole)
If Not foundProduct Is Nothing Then
' Product found, retrieve information
Dim productName As String
Dim productPrice As Double
' Retrieve product name and price from adjacent cells
productName = foundProduct.Offset(0, 1).Value
productPrice = foundProduct.Offset(0, 2).Value
' Update labels to display product information
ProductNameLabel.Caption = "Product Name: " & productName
ProductPriceLabel.Caption = "Price: $" & Format(productPrice, "0.00")
' Add a new TextBox below BarcodeTextBox for continuous scanning
AddNewBarcodeTextBox
SendKeys "{TAB}", False
Else
' Product not found, clear labels
ProductNameLabel.Caption = ""
ProductPriceLabel.Caption = ""
End If
End Sub
Private Sub AddNewBarcodeTextBox()
' Add a new TextBox below BarcodeTextBox
Dim newTextBox As MSForms.TextBox
Set newTextBox = Me.Controls.Add("Forms.TextBox.1")
With newTextBox
.Top = BarcodeTextBox.Top + BarcodeTextBox.Height + 5 ' Position below the BarcodeTextBox
.Left = BarcodeTextBox.Left
.Width = BarcodeTextBox.Width
.Height = BarcodeTextBox.Height
.Name = "NewBarcodeTextBox"
.TabStop = True
.SetFocus
End With
End Sub
Let me know if you need any more information. Thank you in advanced!