Printing UPC Bar Codes in Visual Basic

Azalea Software’s barcode fonts come with the functions you’ll need within Microsoft Visual Basic to create and print UPC A barcodes using our barcode fonts. Other Visual Basic functions are available for Code 128, Code 39, Interleaved 2 of 5, and POSTNET.

You may port these functions to other languages and development tools, so feel free to copy, paste, and edit the code as needed; our sole caveat is that our copyright notice must remain intact. When you port, or if you find a bug or something that needs improvement, please send us a copy so we can learn something new. (Our email address is technicalsupport at azalea dot com…and as always, AtDhVaAnNkCsE!)

Our Visual Basic sample code is provided as is. While every attempt has been made to ensure that it works correctly, please double-check it against the results obtained from our bundled wizard utility that comes with UPCTools.

Function Azalea_UPC_A(ByVal UPCnumber As String) As String

' UPCTools v6.0 Copyright 2008 Jerry Whiting. All rights reserved.

' Azalea Software, Inc. www.azalea.com

' The input, UPCnumber, is a string to be converted into a UPC version A barcode.

Dim checkDigitSubtotal As Integer ' check digit throwaway

Dim i As Integer ' loop counter

Dim checkDigit As String ' check digit itself

Dim temp As String ' placeholder variable

' Valid input is 11-digits (no check digit), 12-digits (with check digit), or 14-digits (GTIN).

Select Case Len(UPCnumber)

  Case 11

      ' do check digit calculation

   Case 12

  ' ignore their check digit & use ours (TBD compare and handle mismatches)

     UPCnumber = Left(UPCnumber, 11)

  Case 14

  ' GTIN input, strip leading 2 characters & last one too;

     UPCnumber = Mid(UPCnumber, 3, 11)

  Case Else

      8' error handling goes here

End Select

' string substitution for the 1st character in lower-right corner

Select Case Left(UPCnumber, 1)

  Case "0"

    temp = "U|xa"

  Case "1"

    temp = "[|xb"

  Case "2"

    temp = "V|xc"

  Case "3"

    temp = "W|xd"

  Case "4"

    temp = "X|xe"

  Case "5"

    temp = "Y|xf"

  Case "6"

    temp = "Z|xg"

  Case "7"

    temp = "u|xh"

  Case "8"

    temp = "\|xi"

  Case "9"

    temp = "]|xj"

End Select

' string substitution for left notch

For i = 2 To 6 Step 1

temp = temp + Chr(65 + (Val(Mid(UPCnumber, i, 1))))

Next i

' do the check digit

checkDigitSubtotal = (Val(Left(UPCnumber, 1))) + (Val(Mid(UPCnumber, 3, 1))) + (Val(Mid(UPCnumber, 5, 1))) + (Val(Mid(UPCnumber, 7, 1))) + (Val(Mid(UPCnumber, 9, 1))) + (Val(Right(UPCnumber, 1)))

checkDigitSubtotal = (3 * checkDigitSubtotal) + (Val(Mid(UPCnumber, 2, 1))) + (Val(Mid(UPCnumber, 4, 1))) + (Val(Mid(UPCnumber, 6, 1))) + (Val(Mid(UPCnumber, 8, 1))) + (Val(Mid(UPCnumber, 10, 1)))

checkDigit = Right(Str(300 - checkDigitSubtotal), 1)

' add the center guard bar, product number, check digit & right guard bar

temp = temp + "y" + Right(UPCnumber, 5) + Chr(107 + (Val(checkDigit))) + "z"

' the check digit human readable

Select Case checkDigit

  Case "0"

     Azalea_UPC_A = temp + "U"

  Case "1"

    Azalea_UPC_A = temp + "["

  Case "2"

    Azalea_UPC_A = temp + "V"

  Case "3"

    Azalea_UPC_A = temp + "W"

  Case "4"

    Azalea_UPC_A = temp + "X"

  Case "5"

    Azalea_UPC_A = temp + "Y"

  Case "6"

    Azalea_UPC_A = temp + "Z"

  Case "7"

    Azalea_UPC_A = temp + "u"

  Case "8"

    Azalea_UPC_A = temp + "\"

  Case "9"

    Azalea_UPC_A = temp + "]"

End Select

' Format the output, Azalea_UPC_A, in one of Azalea Software's UPC fonts.

' For example in Excel, B1=Azalea_UPC_A(A1)

' Or put another way, yourContainer.text=Azalea_UPC_A(yourInputString)

End Function