IDAutomation.com, Inc.

Your Source for Quality Symbology

[IDAutomation.com Home Page]

Home:  Products:  Visual Basic:
 

Visual Basic Barcode Integration Guide & Tutorial
Integrate barcoding into Microsoft® VB 6 and Visual Basic .NET

IDAutomation.com provides several products and options for implementing barcodes in VB with flexible license agreements to meet a variety of needs including single user licenses and royalty-free developer licenses. The following chart may be used to determine the best product needed:
Product(s):     Advantages and Disadvantages:
Self Checking Barcode Fonts
  • The easiest implementation option when the barcode type does not matter and the data is numbers and/or uppercase letters. It is suggested to use the Codabar Font for numbers only and the Code 39 Font when encoding numbers and uppercase letters.
  • Data is easily formatted to the barcode font by appending asterisks to the beginning and ending of the data being encoded; no other DLLs or source code are needed.
  • Tutorials are provided in this document for implementation with VB 6 and VB.NET.
.NET Forms Controls
ASP.NET Web Server Controls
VB Module Integration with Barcode Fonts
  • Easily integrates into the project, without the requirement for additional DLLs.
  • Supports most linear barcode types.
  • Modules and source code are included for VB 6, VBA and VB.NET.
  • Methods in the module calculate the start, stop and check digits and return a character string, that when combined with IDAutomation Barcode Fonts, create a correct barcode.
Native Windows Barcode DLL with Barcode Fonts or the Universal Barcode Font
PDF417 Font and Encoder or the Data Matrix Font and Encoder
Barcode ActiveX Controls
Crystal Reports Integration When printing with Crystal Reports, refer to the Crystal Reports Integration Guide for the best implementation method.
 
Self-Checking Fonts in VB 6:

Self-Checking Barcode Fonts (including MICR and OCR fonts) are compatible with Visual Basic and may be used with ease. To use a self-checking font such as Code 39, insert an asterisk "*" before and after the data encoded. Therefore, to encode TEST3OF9 in a barcode, the text of *TEST3OF9* would need to be present in a field with the Code 39 font selected. When using the Codabar Font for numbers only, use the 'A' character instead of the asterisks.

The following is a VB 6 example of printing the Code 39 bar-code font from Visual Basic, where variable is a string to encode in the barcode:
Printer.FontName = "IDAutomationHC39M"
Printer.CurrentY = 3000
Printer.FontSize = 12
Printer.Print  "*" & variable & "*"
Printer.FontSize = 10
Printer.FontName = "Helv"  'Switch to a text font
Printer.Print "The above barcode is code-39"
Printer.EndDoc 'This tells the printer to eject the page

Barcode may also be printed from a field in a database via the data control, for example:
Printer.Print  "*" & Data1.Recordset("field1") & "*"

The start and stop characters may also be appended to the variable, for example:
variable ="*" & DataString  & "*"
Printer.Print  variable

When printing with Crystal Reports, refer to the Crystal Reports Integration Guide for the best implementation method.

Self-Checking Fonts in VB .NET:

Data is easily formatted to the barcode font by appending asterisks to the beginning and ending of the data being encoded; no other DLLs or source code are needed. When using the Codabar Font for numbers only, use the 'A' character instead of asterisks. The following source code is an example of printing a barcode from VB.NET with the Code 39 Barcode Font, where VariableToPrint is a string that needs to be encoded:

Import the System.Drawing.Printing class and define a class level variable:
Imports System.Drawing.Printing
Private PrintFont As
Font                'Class level variable.
private VariableToPrint as string    'class level variable representing the data to print

Place code in an object event to generate the page(s) to be printing:
'****Code within the object generating the print, such as a toolbar button...
'Initialize the Print Document object that will be sent to the printer
Dim PrintDoc As New PrintDocument()
Dim VariableToPrint as string            'variable that we are printing

'Set the font of the font object and the data to encode
PrintFont = New Font("IDAutomationHC39L", 12)
VariableToPrint = "*" & "128-39-200-128" & "*"

'Add an event handler that will be called for each page that is generated
AddHandler PrintDoc.PrintPage, AddressOf Me
.PrintDoPrintPage

'Initiate the printing of the document
PrintDoc.Print()
'****


The following code should be placed in the event handler, which places the data on the page:
Dim linesPerPage As Single = 0        'Number of lines (bar codes) that you will be able to fit on the page
Dim yPos As Single = 0                    'yPos on the page of the bar code
Dim leftMargin As Single = ev.MarginBounds.Left    'left margin of bar code
Dim topMargin As Single = ev.MarginBounds.Top   'top margin of bar code
Dim line As String = Nothing        'String that font will be applied to and printed

'Calculate the number of lines to print per page that we can fit based on the
'height of the font

linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics)

'Set the y position of the font (how far down on the page it prints
yPos = 100

'Set the text that we will print
line = VariableToPrint

'Send the string to the printer. An empty string format variable is passed in. This
'variable can do any formatting on the string that is necessary
ev.Graphics.DrawString(line, PrintFont, Brushes.Black, leftMargin, yPos, New StringFormat())

When printing with Crystal Reports, refer to the Crystal Reports Integration Guide for the best implementation method.

Visual Basic Barcode Font Module Integration

The IDAutomation VB Font Module (or source code from the module) calculates the start, stop and check digits for the barcode and returns a text string, that when combined with the appropriate IDAutomation Barcode Font, creates a correct barcode. VB developers may add this module to their Visual Basic applications and access the functions directly. Using this module, there is nothing to distribute with the application except the barcode font because the functions of the module are compiled into the EXE file.

Note: Depending on the method used, a parameter may be required. For example, Code128("123456",0,True).

Function Notes
VB 6 module:
Code128(
DataToEncode,
Optional ReturnType,
Optional ApplyTilde)

VB.NET module:
Code128(
DataToEncode, ApplyTilde)

Code128HR(
DataToEncode, ApplyTilde)
The recommended and primary Code 128 method that will automatically encode any data from ASCII 1 to ASCII 127. This method contains many options.

(DataToEncode, 0) formats barcode output string to the Code 128 barcode fonts.
(DataToEncode, 0, True) formats barcode output string for the Code 128 barcode fonts with ApplyTilde enabled.
(DataToEncode, 1) returns the human readable text.
(DataToEncode, 2) returns only the check digit.

The Code128HR function is only available in VB.NET.
It returns the human readable text for Code 128. To obtain this function in VB6, use Code128(Data, 1, True)

Code128a(DataToEncode) Caution: Entering a lower case character with this method will create a function. Use the character values 64 through 95 from set A to create functions.
Formats output to the Code 128 barcode fonts.
Code128b(DataToEncode) Returns codes formatted to the Code 128 character set B. Formats output to the Code 128 bar code fonts.
Code128c(DataToEncode,
Optional ReturnType)
This code128 function "interleaves" numbers into pairs for high density.
(DataToEncode, 0) formats output to the Code 128 barcode fonts.
(DataToEncode, 1) returns the human readable text with the check digit included.
(DataToEncode, 2) returns only the check digit.
I2of5(DataToEncode) This function "interleaves" numbers into pairs for high density without check digits and formats the return string to the Interleaved 2 of 5 font.
I2of5Mod10(DataToEncode,
Optional ReturnType)
(DataToEncode, 0) performs the mod10 checksum calculation for increased accuracy and formats the return string to the Interleaved 2 of 5 font. MOD 10 checksums are required by USPS for special services and for SSC-14 when using Interleaved 2 of 5 for that purpose.
(DataToEncode, 1) returns the human readable data with the MOD10 check digit included.
(DataToEncode, 2) returns the MOD10 check digit.
Code39(DataToEncode) Formats the output to print using Code 39 fonts.
Code39Mod43(DataToEncode,
Optional ReturnType)
(DataToEncode, 0) performs the mod43 checksum calculation for increased accuracy and then formats the output to print using Code 39 fonts. The mod43 checksum is usually required for LOGMARS and HIBC applications.
(DataToEncode, 1) returns the human readable data with the check digit included.
(DataToEncode, 2) returns only the check digit.
Codabar(DataToEncode) Formats the output to print using Codabar fonts.
UCC128(DataToEncode) This symbology option encodes an even number of number digits and includes the FNC1 character in set C as required. Use Code 128 Auto to encode additional FNC1 codes or data containing text or odd numbers. Use the UCC128 function only for UCC-128 applications where the input data is an even number such as in SSCC-18 and SCC-14 barcodes. For example, to encode an SSCC-18 barcode, enter 00000123455555555558 as the data input. Formats output to the Code 128 barcode fonts.
Postnet(DataToEncode,
Optional ReturnType)
Enter a single string of Zip, Zip + 4 or Zip + 4 + Delivery Point. The DataToEncode must be a number and can include dashes and spaces.
(DataToEncode, 0) formats output to the POSTNET barcode fonts.
(DataToEncode, 1) returns the human readable data with the check digit included.
(DataToEncode, 2) returns only the check digit.
MOD10(DataToEncode) Returns the MOD 10 check digit for a given string of numbers according to the UCC/EAN method.
RM4SCC(DataToEncode) Formats the output to print using RM4SCC fonts. Checksum calculated according to the mailsort customer barcoding specification.
MSI(DataToEncode,
Optional ReturnType)
(DataToEncode, 0) formats output to the MSI barcode fonts.
(DataToEncode, 1) returns the human readable data with the check digit included.
(DataToEncode, 2) returns only the check digit.
Code11(DataToEncode) Only the "C" check digit is calculated which is the standard when encoding 10 digits or less. Formats output to the Code11 barcode fonts.
UPCa(DataToEncode) DataToEncode is a UPC-A number string of 11, 12, 13, 14, 16 or 17 digits with or without a check digit, add-ons are supported.  Formats output to the UPC/EAN barcode font. Entering incorrect data will create a barcode containing all zeros.
UPCe(DataToEncode) DataToEncode is a UPC-A number string of 11, 12, 13, 14, 16 or 17 digits with or without a check digit, add-ons are supported. The purpose of this function is to print the UPC-E barcode from a UPC-A barcode that can be compressed. Formats output to the UPC/EAN barcode font. Entering incorrect data will create a barcode containing "00005000000".
EAN13(DataToEncode) DataToEncode is a number string of 12, 13, 14, 15, 17 or 18 digits with or without a check digit, add-ons are supported. Formats output to the UPC/EAN barcode font. Entering incorrect data will create a barcode containing all zeros.
EAN8(DataToEncode) DataToEncode is a number string of 7 or 8 characters (EAN-8 without the check digit). Formats output to the UPC/EAN barcode font. Entering incorrect data will create a barcode containing all zeros.

 

Visual Basic 6 Module Example:

  1. Install the desired IDAutomation Barcode Font package. If unsure of which is needed, IDAutomation recommends the Code 128 Font Advantage Package because this package includes several fonts in different heights and it encodes all letters and numbers.
  2. Download the Visual Basic Barcode Font Module and extract the IDAutomation.bas file into the VB project directory.
  3. Open Visual Basic; Choose Project - Add Module.
  4. Select the IDAutomation.bas file.
  5. Now that the module is part of the project, the methods of the module used to format text to the barcode fonts may be called. For example:
    Printer.FontName = "IDAutomationC128L"
    Printer.CurrentY = 3000
    Printer.FontSize = 16
    Printer.Print  Code128(Data1.Recordset("field1"), 0)
    Printer.FontSize = 10
    Printer.FontName = "Helv"  'Switch back to a text font
    Printer.Print "The above barcode was printed with the IDAutomation Code 128 Font"
    Printer.EndDoc 'This tells the printer to eject the page

Visual Basic .NET Module Example:

  1. Install the desired IDAutomation Barcode Font package. If unsure of which is needed, IDAutomation recommends the Code 128 Font Advantage Package because this package includes several fonts in different heights and it encodes all letters and numbers.
  2. Download the Visual Basic Barcode Font Module and extract the IDAutomation.vb file into the VB project directory.
  3. In Visual Basic .NET, Choose Project - Add Existing Item.
  4. Select the IDAutomation.vb file. After adding the module, it should show up in the IDE.
  5. Now that the module is part of the project, the functions of the module may be used to format text to IDAutomation Barcode Fonts. For example:
    TextVariable = Code128("Code 128 Font Test", 0)  returns a character string, that when combined with the bar code font, creates a correct barcode.
  6. The following code example prints a bar code in VB.NET:
    Import the System.Drawing.Printing class and define a class level variable:
    Imports System.Drawing.Printing
    Private PrintFont As Font                'Class level variable.
    private VariableToPrint as string    'Class level variable representing the data to print

    Place code in an object event to generate the page(s) to be printing:
    '****Code within the object generating the print, such as a toolbar button...
    'Initialize the Print Document object that will be sent to the printer
    Dim PrintDoc As New PrintDocument()
    Dim VariableToPrint as string            'variable that we are printing

    'Set the font of the font object and the text to encode
    PrintFont = New Font("IDAutomationHC39M", 12)
    VariableToPrint = "*" & "12827384736388" & "*"

    'Add an event handler that will be called for each page that is generated
    AddHandler PrintDoc.PrintPage, AddressOf Me
    .PrintDoPrintPage

    'Initiate the printing of the document
    PrintDoc.Print()
    '****


    The following code should be placed in the event handler, which places the data on the page:
    Dim linesPerPage As Single = 0        'Number of lines (bar codes) that you will be able to fit on the page
    Dim yPos As Single = 0                    'yPos on the page of the bar code
    Dim leftMargin As Single = ev.MarginBounds.Left    'left margin of bar code
    Dim topMargin As Single = ev.MarginBounds.Top   'top margin of bar code
    Dim line As String = Nothing        'String that font will be applied to and printed

    'Calculate the number of lines to print per page that we can fit based on the
    'height of the font

    linesPerPage = ev.MarginBounds.Height / PrintFont.GetHeight(ev.Graphics)

    'Set the y position of the font (how far down on the page it prints
    yPos = 100

    'Set the text that will print
    line = VariableToPrint

    'Send the string to the printer. An empty string format variable is passed in. This
    'variable can do any formatting on the string that is necessary
    ev.Graphics.DrawString(line, PrintFont, Brushes.Black, leftMargin, yPos, New StringFormat())

Barcode ActiveX Control Integration with VB.NET

The IDAutomation.com ActiveX controls allow generation of Windows Enhanced Metafile images and printing from Visual Studio.NET. IDAutomation recommends using .NET Forms Controls instead of ActiveX if possible.

  1. Purchase or download and install the ActiveX Control.
  2. Open a VB .NET Project and choose Project | Add Reference.
  3. Select the appropriate IDAutomation Barcode Component from the COM components tab of the reference list. Click Select. Click OK.
  4. The following code examples use the IDAutomation.com Linear Bar Code ActiveX control in the object named axBarCode1 to print to the default printer:

Private Sub cmdPrintToPrinter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrintToPrinter.Click

    Dim pd As New PrintDocument()
   
    'Add an Event Handler for printing
    AddHandler pd.PrintPage, AddressOf Me
.pd_PrintPage
   

     'Fire off the print page event.
    pd.Print()
End Sub

'Event handler for the PrintDocument
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = ev.MarginBounds.Left     'Left Margin of Page
    Dim topMargin As Single = ev.MarginBounds.Top     'Top Margin of Page.
    Dim line As String = Nothing                                            'Data that we will write to the printer

    ' Calculate the number of lines per page based on the height of the font that was sent.
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

    ' Print each line of the file.
    line = "IDAutomation.com, Inc. VB Dot Net Example"

    'Set the y co-ordinate of where we are going to print the Line
    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)

    'Send the string to the printer
    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

    'Increase the line count
    count += 1

    'Set the y position of the next line
    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)

    'Set the second string to be printed. We have to account for the actual printing position of the
    'bar code when displaying yPos in this line. yPos set above is actually the location that this line
    'will be printed on.
    line = "Printing the Enhanced Metafile version of the barcode at X=" & leftMargin & ", Y=" & yPos + (topMargin + ((count + 1) * printFont.GetHeight(ev.Graphics)))
   
    'Print the string
    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

    'Increase the line count
    count += 1

    'Increase the ypos
    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
   
    'Print the enhanced metafile image of the bar code

    ev.Graphics.DrawImage(AxBarCode1.GetEnhWMF, leftMargin, yPos)

End Sub

An image may also be saved the a local hard drive with the following code:
Private Sub cmdSaveToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveToFile.Click
    AxBarCode1.SaveBarCode("C:\SavedBarCode.wmf")
End Sub

Copying the image to the clipboard is only supported with IDAutomation's .NET Forms Controls. When printing with Crystal Reports, refer to the Crystal Reports Integration Guide for the best implementation method.

 

 Product Links: [Barcode Fonts | Barcode Components | Barcode Label Software | Barcode Scanners]

© Copyright 2003-2007 IDAutomation.com, Inc., All Rights Reserved. Legal Notices.  

Over 70% of Fortune 100 companies use our products to automate their businesses.