I’m working on this store checkout system where I can scan barcodes into TextBoxes on a UserForm. Next to each TextBox, there’s supposed to be a Label showing the product name and price. I’ve got it set up so I can keep adding more TextBoxes as I scan more barcodes. But here’s the problem: the order the barcodes show up in the TextBoxes is all over the place. The sequence on which TextBox the new barcode will appear is 1st, 2nd, 1st, 2nd, 3rd, 1st, 2nd, 3rd, 4th, 1st… and so on.
Regarding the labels, they’re only showing info from the first TextBox. I want it so that when I scan a new barcode, a new TextBox pops up automatically, and the new barcode shows up there.
Below is the code I’m currently using:
Private count As Integer
Private Sub BarcodeTextBox_Change()
Dim scannedCode As String
Dim foundProduct As Range
' Get the scanned barcode from the TextBox
scannedCode = BarcodeTextBox.Value
' 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")
' Populate the initial TextBox with the scanned barcode
If BarcodeTextBox.Value = "" Then
BarcodeTextBox.Value = scannedCode
Else
' Add a new TextBox below BarcodeTextBox for continuous scanning
AddNewBarcodeTextBox scannedCode, productName, productPrice
SendKeys "{TAB}", False
End If
Else
' Product not found, clear labels
ProductNameLabel.Caption = ""
ProductPriceLabel.Caption = ""
End If
End Sub
Private Sub AddNewBarcodeTextBox(ByVal scannedCode As String, ByVal productName As String, ByVal productPrice As Double)
' Add a new TextBox and Label below BarcodeTextBox
Dim newTextBox As MSForms.TextBox
Dim newLabelName As MSForms.Label
Dim newLabelPrice As MSForms.Label
Set newTextBox = Me.Controls.Add("Forms.TextBox.1")
Set newLabelName = Me.Controls.Add("Forms.Label.1")
Set newLabelPrice = Me.Controls.Add("Forms.Label.1")
count = count + 1
With newTextBox
.Top = BarcodeTextBox.Top + (count * (BarcodeTextBox.Height + 5)) ' Position below the previous TextBox
.Left = BarcodeTextBox.Left
.Width = BarcodeTextBox.Width
.Height = BarcodeTextBox.Height
.Name = "NewBarcodeTextBox" & count
.TabStop = True
.SetFocus
End With
With newLabelName
.Top = newTextBox.Top
.Left = newTextBox.Left + newTextBox.Width + 5
.Width = 100 ' Adjust label width as needed
.Height = newTextBox.Height
.Caption = "Product Name: " & productName
End With
With newLabelPrice
.Top = newLabelName.Top
.Left = newLabelName.Left + newTextBox.Width + 5
.Width = 100 ' Adjust label width as needed
.Height = newLabelName.Height
.Caption = "Price: $" & Format(productPrice, "0.00")
End With
' Increase UserForm height to accommodate new TextBox and Label
Me.Height = Me.Height + newTextBox.Height + 5
' Center the UserForm on the screen
CenterUserForm
End Sub
Private Sub CenterUserForm()
' Center the UserForm on the screen
Me.Top = (Application.Height - Me.Height) / 2
Me.Left = (Application.Width - Me.Width) / 2
End Sub
Below is an image of the Userform name
Let me know if there is any more information I should provide. Thank you in advanced!