Printing Code 128 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 Code 128 barcodes using our barcode fonts. Other Visual Basic functions are available for UPC, 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 Code128Tools.

Module C128

Function AzaleaCode128A(ByVal C128A As String) As String

' C128Tools v6.0 Copyright 2008 Azalea Software, Inc. All rights reserved. www.azalea.com

' C128A is a string to be encoded as a Code 128 code set A symbol.

' Format the output, AzaleaCode128A, using Azalea Software's barcode fonts.

Dim temp As String

Dim chunk As String ' loop variable

Dim i As Integer ' just a counter

Dim checkDigitSubtotal As Integer ' check digit calculation variable

Dim e As Integer ' placeholder variable

checkDigitSubtotal = 103

' map the input string into the Azalea Code 128 character set

For i = 1 To Len(C128A) Step 1

  chunk = Mid(C128A, i, 1)

  Select Case Asc(chunk)

   ' map from above ASCII 182 placeholders to the font's character assignments

     Case Is > 95

       temp = temp & Chr(Asc(chunk) - 66)

     Case Is = 32 ' move the space character

       temp = temp & Chr(206)

    Case Else

       temp = temp & chunk

  End Select

Next i

For i = 1 To Len(C128A)

  e = Asc(Mid(C128A, i, 1)) - 32

  If e <> 142 Then

    checkDigitSubtotal = checkDigitSubtotal + (e * i)

  End If

Next i

' start bar, encoded string, check digit, stop bar

Select Case checkDigitSubtotal Mod 103

  Case 0

    AzaleaCode128A = Chr(181) & temp & Chr(194) & Chr(196)

  Case 1 To 93

    AzaleaCode128A = Chr(181) & temp & Chr(checkDigitSubtotal Mod 103 + 32) & Chr(196)

  Case Else

    AzaleaCode128A = Chr(181) & temp & Chr(checkDigitSubtotal Mod 103 + 71) & Chr(196)

End Select

End Function

Function AzaleaCode128B(ByVal C128B As String) As String

' C128Tools v6.0 Copyright 2008 Azalea Software, Inc. All rights reserved. www.azalea.com

' C128B is a string to be encoded as a Code 128 code set B symbol.

' Format the output, AzaleaCode128B, using Azalea Software's bar code fonts.

Dim temp As String

Dim chunk As String ' loop variable

Dim i As Integer ' just a counter

Dim e As Integer ' just a variable

Dim checkDigitSubtotal As Integer ' check digit calculation variable

checkDigitSubtotal = 104

For i = 1 To Len(C128B) Step 1

  chunk = Mid(C128B, i, 1)

  Select Case Asc(chunk)

   ' map from above ASCII 200 placeholders to actual character assignments

    Case Is > 200

      temp = temp & Chr(Asc(chunk) - 35)

    ' The space character is at ASCII 194 because TrueType

    ' doesn't allow a glyph in the ASCII 32 slot

    Case Is = 32

      temp = temp & Chr(206)

    Case Else

    temp = temp & chunk

  End Select

Next i

For i = 1 To Len(C128B) Step 1

  e = Asc(Mid(C128B, i, 1)) - 32

  If e <> 142 Then

    checkDigitSubtotal = checkDigitSubtotal + (e * i)

  End If

Next i

Select Case checkDigitSubtotal Mod 103

  Case 0

    AzaleaCode128B = Chr(182) & temp & Chr(194) & Chr(196)

  Case 1 To 93

    AzaleaCode128B = Chr(182) & temp & Chr(checkDigitSubtotal Mod 103 + 32) & Chr(196)

  Case Else

    AzaleaCode128B = Chr(182) & temp & Chr(checkDigitSubtotal Mod 103 + 71) &Chr(196)

End Select

End Function

Function AzaleaCode128UCCEAN(ByVal appIdentifier As String, ByVal dataString As String) As String

' C128Tools v6.0 Copyright 2008 Azalea Software, Inc. All rights reserved. www.azalea.com

' C128UCCEAN is a string to be encoded as a Code 128 UCC/EAN-13 symbol.

' Format AzaleaCode128UCCEAN, using Azalea Software's bar code fonts.

Dim checkDigitSubtotal As Long

Dim checkDigit As Byte

Dim symbol As Byte

Dim weight As Long

Dim fontString As String

Dim i As Integer

checkDigitSubtotal = 105 ' start character

checkDigitSubtotal += 102 ' add the FNC1, required by the spec and not weighted

' Add our start character (codeset C), FNC1 and our app identifier

fontString = Chr(183) & Chr(205)

' right justify the data

If Not ((Len(dataString) + Len(appIdentifier)) Mod 2) = 0 Then

  dataString = "0" & dataString

End If

dataString = appIdentifier & dataString

' Since the FNC1 has a weight of 1, we start with a weight of

' two for the "real" data

' Ref: GS1 General Specifications, 5.3.A.1.1, January 2006, Version 7.0

weight = 2

For i = 1 To Len(dataString) / 2

  symbol = Val(Mid(dataString, i, 2))

  checkDigitSubtotal = checkDigitSubtotal + (symbol * weight)

  fontString = fontString & mapCharC128C(symbol)

  weight += 1

Next i

checkDigit = checkDigitSubtotal Mod 103

fontString = fontString & mapCharC128C(checkDigit)

fontString = fontString & Chr(196) ' stop character

' for old versions of VB (6.0 and older)

AzaleaCode128UCCEAN = fontString

' and new versions (2005 and newer)

' if VB refuses to compile, remove this return statement

Return fontString

End Function

Function eanMod10(ByVal data As String) As Byte

' C128Tools v5.0 27nov06 jwhiting

' Copyright 2006 Azalea Software, Inc. All rights reserved. www.azalea.com

Dim evenAcc As Integer

Dim oddAcc As Integer

Dim i As Integer

For i = 1 To Len(data) Step 2

  evenAcc += Val(Mid(data, i, 1))

Next

For i = 2 To Len(data) Step 2

  oddAcc += Val(Mid(data, i, 1))

Next

eanMod10 = ((evenAcc * 3) + oddAcc) Mod 10

End Function

Function mapCharC128C(ByVal symbol As Byte) As String

' C128Tools v5.0 27nov06 jwhiting

' Copyright 2006 Azalea Software, Inc. All rights reserved. www.azalea.com

Dim fontSymbol As String

Select Case symbol

  Case 0

    fontSymbol = Chr(206)

  Case 1 To 93

    fontSymbol = Chr(symbol + 32)

  Case Is > 93

    fontSymbol = Chr(symbol + 103)

  Case Else

    fontSymbol = ""

End Select

Return fontSymbol

End Function

End Module