Public Function Code128(DataToFormat As String, ReturnType As Integer) As String
'
' Copyright © IDautomation.com, Inc. 2001. All rights reserved.
' For more info visit http://www.IDAutomation.com
'
' You may use our source code in your applications only if you are using barcode fonts created by IDautomation.com, Inc.
' and you do not remove the copyright notices in the source code.
'
' The purpose of this code is to calculate the Code 128 barcode for ANY character set
'
' Encode UCC/EAN 128 by inserting ASCII 202 into the string to encode
'
' You MUST use the fully functional Code 128 (dated 12/2000 or later)
' font for this code to create and print a proper barcode
'
DataToPrint = ""
DataToFormat = RTrim(LTrim(DataToFormat))
C128_StartA = Chr(203)
C128_StartB = Chr(204)
C128_StartC = Chr(205)
C128_Stop = Chr(206)
'Here we select character set A, B or C for the START character
StringLength = Len(DataToFormat)
CurrentCharNum = Asc(Mid(DataToFormat, 1, 1))
If CurrentCharNum < 32 Then C128Start = C128_StartA
If CurrentCharNum > 31 And CurrentCharNum < 127 Then C128Start = C128_StartB
If ((StringLength > 4) And IsNumeric(Mid(DataToFormat, 1, 4))) Then C128Start = C128_StartC
'202 is the FNC1, with this Start C is mandatory
If CurrentCharNum = 202 Then C128Start = C128_StartC
If C128Start = Chr(203) Then CurrentEncoding = "A"
If C128Start = Chr(204) Then CurrentEncoding = "B"
If C128Start = Chr(205) Then CurrentEncoding = "C"
For I = 1 To StringLength
'check for FNC1 in any set
If ((Mid(DataToFormat, I, 1)) = Chr(202)) Then
DataToEncode = DataToEncode & Chr(202)
'check for switching to character set C
ElseIf ((I < StringLength - 2) And (IsNumeric(Mid(DataToFormat, I, 1))) And (IsNumeric(Mid(DataToFormat, I + 1, 1))) And (IsNumeric(Mid(DataToFormat, I, 4)))) Or ((I < StringLength) And (IsNumeric(Mid(DataToFormat, I, 1))) And (IsNumeric(Mid(DataToFormat, I + 1, 1))) And (CurrentEncoding = "C")) Then
'switch to set C if not already in it
If CurrentEncoding <> "C" Then DataToEncode = DataToEncode & Chr(199)
CurrentEncoding = "C"
CurrentChar = (Mid(DataToFormat, I, 2))
CurrentValue = CInt(CurrentChar)
'set the CurrentValue to the number of String CurrentChar
If (CurrentValue < 95 And CurrentValue > 0) Then DataToEncode = DataToEncode & Chr(CurrentValue + 32)
If CurrentValue > 94 Then DataToEncode = DataToEncode & Chr(CurrentValue + 100)
If CurrentValue = 0 Then DataToEncode = DataToEncode & Chr(194)
I = I + 1
'check for switching to character set A
ElseIf (I <= StringLength) And ((Asc(Mid(DataToFormat, I, 1)) < 31) Or ((CurrentEncoding = "A") And (Asc(Mid(DataToFormat, I, 1)) > 32 And (Asc(Mid(DataToFormat, I, 1))) < 96))) Then
'switch to set A if not already in it
If CurrentEncoding <> "A" Then DataToEncode = DataToEncode & Chr(201)
CurrentEncoding = "A"
'Get the ASCII value of the next character
CurrentCharNum = (Asc(Mid(DataToFormat, I, 1)))
If CurrentCharNum = 32 Then
DataToEncode = DataToEncode & Chr(194)
ElseIf CurrentCharNum < 32 Then
DataToEncode = DataToEncode & Chr(CurrentCharNum + 96)
ElseIf CurrentCharNum > 32 Then
DataToEncode = DataToEncode & Chr(CurrentCharNum)
End If
'check for switching to character set B
ElseIf (I <= StringLength) And ((Asc(Mid(DataToFormat, I, 1))) > 31 And (Asc(Mid(DataToFormat, I, 1)))) < 127 Then
'switch to set B if not already in it
If CurrentEncoding <> "B" Then DataToEncode = DataToEncode & Chr(200)
CurrentEncoding = "B"
'Get the ASCII value of the next character
CurrentCharNum = (Asc(Mid(DataToFormat, I, 1)))
If CurrentCharNum = 32 Then
DataToEncode = DataToEncode & Chr(194)
Else
DataToEncode = DataToEncode & Chr(CurrentCharNum)
End If
End If
Next I
HumanReadableText = ""
'FORMAT TEXT FOR AIs
StringLength = Len(DataToFormat)
For I = 1 To StringLength
'Get ASCII value of each character
CurrentCharNum = Asc(Mid(DataToFormat, I, 1))
'Check for FNC1
If ((I < StringLength - 2) And (CurrentCharNum = 202)) Then
'It appears that there is an AI
'Get the value of each number pair (ex: 5 and 6 = 5*10+6 =56)
CurrentChar = (Mid(DataToFormat, I + 1, 2))
CurrentCharNum = CInt(CurrentChar)
'Is 4 digit AI?
If ((I < StringLength - 4) And ((CurrentCharNum <= 81 And CurrentCharNum >= 80) Or (CurrentCharNum <= 34 And CurrentCharNum >= 31))) Then
HumanReadableText = HumanReadableText & " (" & (Mid(DataToFormat, I + 1, 4)) & ") "
I = I + 4
'Is 3 digit AI?
ElseIf ((I < StringLength - 3) And ((CurrentCharNum <= 49 And CurrentCharNum >= 40) Or (CurrentCharNum <= 25 And CurrentCharNum >= 23))) Then
HumanReadableText = HumanReadableText & " (" & (Mid(DataToFormat, I + 1, 3)) & ") "
I = I + 3
'Is 2 digit AI?
ElseIf ((CurrentCharNum <= 30 And CurrentCharNum >= 0) Or (CurrentCharNum <= 99 And CurrentCharNum >= 90)) Then
HumanReadableText = HumanReadableText & " (" & (Mid(DataToFormat, I + 1, 2)) & ") "
I = I + 2
End If
ElseIf (Asc(Mid(DataToFormat, I, 1)) < 32) Then
HumanReadableText = HumanReadableText & " "
ElseIf ((Asc(Mid(DataToFormat, I, 1)) > 31) And (Asc(Mid(DataToFormat, I, 1)) < 128)) Then
HumanReadableText = HumanReadableText & Mid(DataToFormat, I, 1)
End If
Next I
DataToFormat = ""
'<<<< Calculate Modulo 103 Check Digit >>>>
'Set WeightedTotal to the value of the start character
weightedTotal = (Asc(C128Start) - 100)
StringLength = Len(DataToEncode)
For I = 1 To StringLength
'Get the ASCII value of each character
CurrentCharNum = (Asc(Mid(DataToEncode, I, 1)))
'Get the Code 128 value of CurrentChar according to chart
If CurrentCharNum < 135 Then CurrentValue = CurrentCharNum - 32
If CurrentCharNum > 134 Then CurrentValue = CurrentCharNum - 100
If CurrentCharNum = 194 Then CurrentValue = 0
'multiply by the weighting character
CurrentValue = CurrentValue * I
'add the values together
weightedTotal = weightedTotal + CurrentValue
Next I
'divide the WeightedTotal by 103 and get the remainder, this is the CheckDigitValue
CheckDigitValue = (weightedTotal Mod 103)
'Now that we have the CheckDigitValue, find the corresponding ASCII character from the table
If CheckDigitValue < 95 And CheckDigitValue > 0 Then C128_CheckDigit = Chr(CheckDigitValue + 32)
If CheckDigitValue > 94 Then C128_CheckDigit = Chr(CheckDigitValue + 100)
If CheckDigitValue = 0 Then C128_CheckDigit = Chr(194)
'Check for spaces or "00" and print ASCII 194 instead
'place changes in DataToPrint
StringLength = Len(DataToEncode)
For I = 1 To StringLength
CurrentChar = Mid(DataToEncode, I, 1)
If CurrentChar = " " Then CurrentChar = Chr(194)
DataToPrint = DataToPrint & CurrentChar
Next I
'Get Printable String
Printable_string = C128Start & DataToPrint & C128_CheckDigit & C128_Stop & " "
DataToEncode = ""
DataToPrint = ""
'ReturnType 0 returns data formatted to the barcode font
If ReturnType = 0 Then Code128 = Printable_string
'ReturnType 1 returns data formatted for human readable text
If ReturnType = 1 Then Code128 = HumanReadableText
'ReturnType 2 returns the check digit for the data supplied
If ReturnType = 2 Then Code128 = C128_CheckDigit
End Function