Printing bar codes in Visual Basic

To print bar codes you must use the bar code functions in the supplied Visual Basic module (.BAS). Before you can use these new functions in your project, see Add our Visual Basic module in a project.

Each bar code function takes a String argument and returns a String result. The string argument is the data you want to encode in the bar code and the returned string result can be used to print a bar code. The bar code is printed by printing the string result using a specific bar code font. 

See Bar code functions for a list of the new functions and Bar code font name and font size to use for a list of the fonts to use with each bar code function.

Function CIA_CODE39(String) As String

The example below use the CIA_CODE39( ) bar code function.

Example: Printing bar codes in a form

- Create a new form Form1 

- Add three TextBox objects; Text1, Text2 and Text3, and two buttons Command1 and Command2.

- Remove any text in the Text properties of the text boxes.

- Set the Font property of Text 3 to 'CIA Code 39 Medium' and a set font Size property to 14.

- Set the Caption property of Command1 to '&Calculate', and Command1 to '&Print'.

- Double-click the Calculate button and edit the code to look like this;

Private Sub Command1_Click()

  Text2 = CIA_CODE39((Text1))
  Text3 = Text2

End Sub

- Double-click the Print button and edit the code to look like this;

Private Sub Command2_Click()

  Form1.PrintForm

End Sub

- Run the form and enter text in to Text1 

- Click Calculate to calculate the text and bar code, click Print to print the form.

Example: Print bar codes using the Printer object

This example uses the same form as the previous example;

- Double-click button Command2 and edit the code to look like this;

Private Sub Command2_Click()
Dim Barcode As String, Human As String
Dim WidthBarcode As Integer, WidthHuman As Integer
    
    Human = Text1
    Barcode = CIA_CODE39(Human)
    
    Printer.Print " "
    Printer.CurrentX = 50
    Printer.CurrentY = 50
    
    Printer.FontBold = False
    Printer.FontItalic = False
    
    ' Set font to bar code font
    Printer.FontName = "CIA Code 39 Medium"
    Printer.FontSize = 14
    ' Save the width of the bar code
    WidthBarcode = Printer.TextWidth(Barcode)
    ' Print bar code
    Printer.Print Barcode
    
    ' Set font to a human-readable font
    Printer.FontName = "Arial"
    Printer.FontSize = 10
    ' Save the width of the human-readable text
    WidthHuman = Printer.TextWidth((Text1))

    ' Position in the centre of the bar code
    Printer.CurrentX = (WidthBarcode / 2) - (WidthHuman / 2)
    ' Print human-readable text
    Printer.Print Human
    
    Printer.Print Chr(13)

    ' Print done.
    Printer.EndDoc
End Sub

- Run the form and enter text in to Text1 

- Click Calculate to calculate the text and bar code, click Print to print the bar code.

 


Copyright 2000 CIA (BAR CODES) UK. All rights reserved. Reproduction prohibited.