How to create and print barcodes with Visual Basic 6 and Bytescout.BarCode SDK

The following sample code for Visual Basic 6 demonstrates how to use Barcode SDK in Visual Basic 6 to generate and print barcodes from VB6 code

VERSION 5.00
 
Begin VB.Form Form1 
   AutoRedraw      =   -1  'True
   Caption         =   "Form1"
 
   ClientHeight    =   4845
   ClientLeft      =   45
   ClientTop       =   435
 
   ClientWidth     =   9795
   LinkTopic       =   "Form1"
   ScaleHeight     =   4845
 
   ScaleWidth      =   9795
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1 
      Caption         =   "Print Barcode to the selected printer"
 
      Height          =   495
      Left            =   3840
      TabIndex        =   1
 
      Top             =   240
      Width           =   2895
   End
   Begin VB.ComboBox cboPrinterList 
      Height          =   315
 
      Left            =   120
      TabIndex        =   0
      Top             =   360
 
      Width           =   3135
   End
End
Attribute VB_Name = "Form1"
 
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
 
Attribute VB_Exposed = False
 ' IMPORTANT NOTE: you need to have .NET Framework 1.10 installed to use BarCode SDK from Visual Basic
 ' to download and install .NET Framework 1.10 please use this link: <a href="http://www.microsoft.com/downloads/details.aspx?familyid=262D25E3-F589-4842-8157-034D1E7CF3A3
 
Private" title="http://www.microsoft.com/downloads/details.aspx?familyid=262D25E3-F589-4842-8157-034D1E7CF3A3
 
Private">http://www.microsoft.com/downloads/details.aspx?familyid=262D25E3-F589-4...</a> Sub Command1_Click()
 
  ' set printer to the selected printer
  Call SelectPrinter(cboPrinterList.Text)
  ' call function to print barcode to Printer object
 Call PrintBarCode
 
End Sub
 
 ' this sample will print one single page with code 39 and Aztec 2D barcode on default printer
 
' populate list of printers on form load
Private Sub Form_Load()
 
Dim dev As Printer, Index As Integer, CurrentPrinter As Integer
 
CurrentPrinter = -1
Index = 0
For Each dev In Printers
cboPrinterList.AddItem dev.DeviceName
 
If Printer.DeviceName = dev.DeviceName Then
CurrentPrinter = Index
End If
 
       Index = Index + 1
Next
If CurrentPrinter <> -1 Then
 
 cboPrinterList.ListIndex = CurrentPrinter
End If
 
End Sub
 
 
'===========================
'Function to select printer
'===========================
 
Function SelectPrinter(ByVal printer_name As String) As Boolean
 
    Dim i As Integer
 
    SelectPrinter = False
    For i = 0 To Printers.Count - 1
 
        ' if the specified printer is found, select it and return True
        If Printers(i).DeviceName = printer_name Then
 
            Set Printer = Printers(i)
            SelectPrinter = True
 
            Exit For
        End If
    Next i
End Function
 
 
' print barcode to the Printer object
 
Private Sub PrintBarCode()
 
 ' IMPORTANT: call Printer.Print Space(1) to initialize the hDC of the Printer
 ' this is how Microsoft advises to do in their KB article (search for Printer.Print string): <a href="http://support.microsoft.com/kb/146022
 
" title="http://support.microsoft.com/kb/146022
 
">http://support.microsoft.com/kb/146022
 
</a> Printer.Print Space(1) ' initialize hDC of Printer object
 
 
 ' create barcode object
 Set bc = CreateObject("Bytescout.BarCode.Barcode")
 
 ' set symbology type
 bc.Symbology = 1 ' 1 = Code39
 
 ' set value to encode
 
 bc.Value = "012345"
 
 ' draw code 39 barcode on a page
 bc.DrawHDC Printer.hDC, 0, 0
 
 
 ' now drawing 2D Aztec barcode
 
 ' set symbology type
 bc.Symbology = 17 ' 17 = Aztec
 ' set value to encode
 
 bc.Value = "012345"
 
 ' draw Aztec 2D barcode on a page
 bc.DrawHDC Printer.hDC, 0, 300
 
 
 Printer.Print "This is a test barcode"
 
 ' finally send command to print the page
 Printer.EndDoc
 
 Set bc = Nothing
 
End Sub

Tutorials: