Creating Visual Basic Program that reads barcode is very simple.
The source code for
Visual Basic barcode sample program is not a complex one. What you need is a barcode
reader or scanner and a barcode that is already printed in the products to be sold. To illustrate
further how to make a sample program in Visual Basic that reads a barcode the example is
provided below. Let assumes that we have already a barcode reader or scanner and a product
with a barcode printed on it. The next step is to create a folder in drive C, name the folder as
“barcode” we are going to save all the files related to barcode on this folder. Next open the
Microsoft Access; we need to store the data of the product with a barcode on a database.
After creating a database in Microsoft Access, save it in the folder you created (barcode) and
give the filename “barcodedata” to MS Access database file. Close the Microsoft Access
application and also the folder and open the Microsoft Visual Basic Application. Create the
following Visual Basic objects:
Form to be use in Data Entry
1. Form
Name property: frmAddItem
Caption property: Add Item to Database
2. Label
Name property: lblBarcode
Caption property: Barcode
3. TextBox
Name property: txtBarcode
Text property: <blank>
4. Label
Name property: lblDescription
Caption property: Description
5. TextBox
Name property: txtDescription
Text property: <blank>
6. Label
Name property: lblPrice
Caption property: Price
6. TextBox
Name property: txtPrice
Text property: <blank>
7. Command Button
Name property: cmdAdd
Caption property &ADD
8. Command Button
Name property: cmdClear
Caption property &CLEAR
Source code for command button add:
Private Sub cmdAdd_Click()
Set rsProduct = New ADODB.Recordset
Set cnData = New Connection
cnData.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\barcode\barcodedata.mdb;Persist Security Info=False”
cnData.Open
rsProduct.Open “SELECT * FROM product”, cnData, 1, 3
rsProduct.AddNew
rsProduct(“barcode”) = txtBarcode.Text
rsProduct(“description”) = txtDescription.Text
rsProduct(“price”) = Val(txtPrice.Text)
rsProduct.Update
txtBarcode.Text = “”
txtDescription.Text = “”
txtPrice.Text = “”
MsgBox “1 item added.”
txtBarcode.SetFocus
End Sub
Source code for command button clear:
Private Sub cmdClear_Click()
txtBarcode.Text = “”
txtDescription.Text = “”
txtPrice.Text = “”
txtBarcode.SetFocus
End Sub
Sample Bar Code Program
Let assume that the barcode is attached to the computer and you have a product that has a
barcode printed on it. Run the program, let assume that we have Cup Noodles with a bar code
as our product. To add it to the database, point the barcode reader or scanner to barcode
printed on the Cup Noodles and click the trigger button of the barcode reader, what will
happen is the data stored in the barcode will appear automatically to the textbox. Next type
the description (Cup Noodles) to the description textbox, type also 2.5 in the price textbox
and click the add button.
Form to be use to retrieve the information about the product and display it on screen.
1. Form
Name property: frmBarcode
Caption property: Sample Barcode Program
2. Label
Name property: lblBarcode
Caption property: Barcode
3. Textbox
Name property: txtBarcode
Text property: <blank>
4. Label
Name property: lblProductDescription
Caption property: <blank>
5. Command button
Name property: cmdClear
Caption property: &CLEAR
Source code for txtBarcode Change Event:
Private Sub txtBarcode_Change()
Set rsProduct = New ADODB.Recordset
Set cnData = New Connection
cnData.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\barcode\barcodedata.mdb;Persist Security Info=False”
cnData.Open
rsProduct.Open “SELECT * FROM product WHERE barcode = ‘” & txtBarcode.Text & “‘”,
cnData, 1, 3
lblProductDescription.Caption = rsProduct(“description”) & ” ” & “@ ” & “$ ” &
rsProduct(“price”)
cnData.Close
Set rsProduct = Nothing
End Sub
Source code for clear button:
Private Sub cmdClear_Click()
txtBarcode.Text = “”
lblProductDescription.Caption = “”
txtBarcode.SetFocus
End Sub
Note: before running the program make sure that you add a product to database so that there
is an item to be retrieve and show it on screen the product description and price of the item
using the barcode reader.