This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.

bar code software

Azalea Software, Inc.


Printing Code 128 Bar Codes in Visual Basic

The following functions can be used within Microsoft Visual Basic to create and print Code 128 barcodes using Azalea Software's barcode fonts. Other functions are available for UPC, EAN, Code 39, and Interleaved 2 of 5.

These functions can be ported to other languages and development tools. Copy, paste, and edit the code as needed. Our copyright notice must remain intact. When you port, please send us a copy so we can learn something new. AtDhVaAnNkCsE

This sample code is provided as is. While every attempt has been made to insure that it works correctly, please double-check it against the results obtained from our bundled wizard utility. Please report any bugs or problems.

Module C128
  Function AzaleaCode128A(ByVal C128A As String) As String
    ' C128Tools v5.0 27nov06 jwhiting
    ' Copyright 2006 Azalea Software, Inc. All rights reserved. www.azalea.com
    ' C128A is a string to be encoded as a Code 128 code set A symbol.
    ' $LastChangedDate: 2006-10-16 14:51:14 -0700 (Mon, 16 Oct 2006) $
    ' $LastChangedRevision: 41 $
    ' 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 v5.0 27nov06 jwhiting
    ' Copyright 2006 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 AzaleaCode128C(ByVal C128C As String) As String
    ' C128Tools v5.0 27nov06 jwhiting
    ' Copyright 2006 Azalea Software, Inc. All rights reserved. www.azalea.com
    ' C128C is a string to be encoded as a Code 128 code set C symbol.
    ' Format the output, AzaleaCode128C, using Azalea Software's bar code fonts.
    Dim temp As String
    Dim temp2 As String ' throwaway variable
    Dim checkDigitSubtotal As Integer ' (sic)
    Dim chunk As String ' loop variable
    Dim i As Integer ' just a counter
    temp2 = C128C
    checkDigitSubtotal = 105 ' start with 105, which is for the start character
    ' first do the mod 103 check digit
    ' then map into the Azalea Code 128 character set
    For i = 1 To Len(C128C) / 2
      chunk = Left$(temp2, 2)
      checkDigitSubtotal = checkDigitSubtotal + Val(chunk) * i
      Select Case Val(chunk)
        Case 0
          temp = temp + Chr(206)
        Case 1 To 93
          temp = temp & Chr(Val(chunk) + 32)
        Case Is > 93
          temp = temp & Chr(Val(chunk) + 103)
      End Select
      temp2 = Right$(temp2, Len(temp2) - 2)
    Next i
    ' start bar, encoded string, check digit, stop bar
    Select Case checkDigitSubtotal Mod 103
      Case 0
        AzaleaCode128C = Chr(183) & temp & Chr(194) & Chr(196)
      Case 1 To 93
        AzaleaCode128C = Chr(183) & temp & Chr(checkDigitSubtotal Mod 103 + 32) & Chr(196)
      Case Is > 93
        AzaleaCode128C = Chr(183) & temp & Chr(checkDigitSubtotal Mod 103 + 103) & Chr(196)
    End Select
  End Function

Function AzaleaCode128SSCC(ByVal ssccId As String) As String
  Dim mod10Check As Byte
  Dim data As String
  ' first get our check digit
  mod10Check = eanMod10(ssccId)
  data = ssccId & Val(mod10Check)
  AzaleaCode128SSCC = AzaleaCode128UCCEAN("00", data)
End Function

Function AzaleaCode128UCCEAN(ByVal appIdentifier As String, ByVal dataString As String) As String
  ' C128Tools v5.0 27nov06 jwhiting
  ' Copyright 2006 Azalea Software, Inc. All rights reserved. www.azalea.com
  ' Azalea Software, Inc.  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

contact ustechnical supportabout usour blogsite mapmisc.links