Attribute VB_Name = "PrecisionID_C128_Module"

'*********************************************************************
'*  Visual Basic / VBA Functions for PrecisionID V2.1
'*  Copyright, PrecisionID.com, Inc. 2005. All rights reserved.
'*
'*  You may incorporate our Source Code in your application
'*  only if you own a valid license from PrecisionID.com,
'*  for the associated font and no text is removed from the
'*  source code.
'*
'*  Distributing our source code or fonts outside your
'*  organization requires a Developer License.
'*********************************************************************

Private Text_Input As String
Private Text_Raw As String
Private End_Sequence As String
Private Start_Code As String
Private Stop_Code As String
Private Function_1 As String
Private C128_Begin As String
Private C128_CheckDigit As String
Private F As Integer
Private Text_Output As String
Private Correct_Text As String
Private Printable_Text As String
Private Enc_Method As String
Private PreCheckTotal As Long
Private PreCheckVal As Integer
Private Current_Val As Long
Private Check_Val As Integer
Private Switch As Integer
Private I As Integer
Private Check_Digit As Integer
Private Current_Method As String
Private New_Line As String
Private This_Char As String
Private This_CharNum As Integer
Private Lead_Char As Integer
Private AddOn_2 As String
Private AddOn_5 As String
Private AddOn_TextFinal As String
Private HR_Text As String
Private Text_Len As Integer
Private FNC_Int As Integer
'END OF DECLARACTIONS

Public Function PrecisionID_C128AUTO(Text_Raw As String) As String
     End_Sequence = "  "
    FNC_Int = 0
    Printable_Text = ""
    Text_Len = Len(Text_Raw)
    This_CharNum = AscW(Mid(Text_Raw, 1, 1))
    If This_CharNum < 32 Then C128_Begin = ChrW(203)
    If This_CharNum > 31 And This_CharNum < 127 Then C128_Begin = ChrW(204)
    If ((Text_Len > 4) And IsNumeric(Mid(Text_Raw, 1, 4))) Then C128_Begin = ChrW(205)
    '202 is for the Function_1, with this Start C is mandatory
    If This_CharNum = 202 Then C128_Begin = ChrW(205)
    If C128_Begin = ChrW(203) Then Current_Method = "A"
    If C128_Begin = ChrW(204) Then Current_Method = "B"
    If C128_Begin = ChrW(205) Then Current_Method = "C"
    For I = 1 To Text_Len
        This_CharNum = AscW(Mid(Text_Raw, I, 1))
        If (This_CharNum = 202) Then
            Text_Input = Text_Input & ChrW(202)
        ElseIf ((I < Text_Len - 2) And (IsNumeric(Mid(Text_Raw, I, 1))) And (IsNumeric(Mid(Text_Raw, I + 1, 1))) And (IsNumeric(Mid(Text_Raw, I, 4)))) Or ((I < Text_Len) And (IsNumeric(Mid(Text_Raw, I, 1))) And (IsNumeric(Mid(Text_Raw, I + 1, 1))) And (Current_Method = "C")) Then
            If Current_Method <> "C" Then Text_Input = Text_Input & ChrW(199)
            Current_Method = "C"
            This_Char = (Mid(Text_Raw, I, 2))
            Current_Val = CInt(This_Char)
            If (Current_Val < 95 And Current_Val > 0) Then Text_Input = Text_Input & ChrW(Current_Val + 32)
            If Current_Val > 94 Then Text_Input = Text_Input & ChrW(Current_Val + 100)
            If Current_Val = 0 Then Text_Input = Text_Input & ChrW(194)
            I = I + 1
        ElseIf (I <= Text_Len) And ((AscW(Mid(Text_Raw, I, 1)) < 31) Or ((Current_Method = "A") And (AscW(Mid(Text_Raw, I, 1)) > 32 And (AscW(Mid(Text_Raw, I, 1))) < 96))) Then
            If Current_Method <> "A" Then Text_Input = Text_Input & ChrW(201)
            Current_Method = "A"
            This_CharNum = AscW(Mid(Text_Raw, I, 1))
            If This_CharNum = 32 Then
                Text_Input = Text_Input & ChrW(194)
            ElseIf This_CharNum < 32 Then
                Text_Input = Text_Input & ChrW(This_CharNum + 96)
            ElseIf This_CharNum > 32 Then
                Text_Input = Text_Input & ChrW(This_CharNum)
            End If
        ElseIf (I <= Text_Len) And ((AscW(Mid(Text_Raw, I, 1))) > 31 And (AscW(Mid(Text_Raw, I, 1)))) < 127 Then
            If Current_Method <> "B" Then Text_Input = Text_Input & ChrW(200)
            Current_Method = "B"
            This_CharNum = (AscW(Mid(Text_Raw, I, 1)))
            If This_CharNum = 32 Then
                Text_Input = Text_Input & ChrW(194)
            Else
                Text_Input = Text_Input & ChrW(This_CharNum)
            End If
        End If
    Next I
    Text_Raw = ""
    PreCheckTotal = AscW(C128_Begin) - 100
    Text_Len = Len(Text_Input)
    For I = 1 To Text_Len
        This_CharNum = AscW(Mid(Text_Input, I, 1))
        If This_CharNum < 135 Then Current_Val = This_CharNum - 32
        If This_CharNum > 134 Then Current_Val = This_CharNum - 100
        If This_CharNum = 194 Then Current_Val = 0
        Current_Val = Current_Val * I
        PreCheckTotal = PreCheckTotal + Current_Val
        If This_CharNum = 32 Then This_CharNum = 194
        Printable_Text = Printable_Text & ChrW(This_CharNum)
    Next I
    Check_Val = (PreCheckTotal Mod 103)
    If Check_Val < 95 And Check_Val > 0 Then C128_CheckDigit = ChrW(Check_Val + 32)
    If Check_Val > 94 Then C128_CheckDigit = ChrW(Check_Val + 100)
    If Check_Val = 0 Then C128_CheckDigit = ChrW(194)
    Text_Input = ""
    PrecisionID_C128AUTO = C128_Begin & Printable_Text & C128_CheckDigit & ChrW(206) & End_Sequence
End Function

Public Function PrecisionID_C128_A(Text_Input As String) As String
     End_Sequence = "  "
     Printable_Text = ""
     PreCheckTotal = 103
     Printable_Text = ChrW(203)
     Text_Len = Len(Text_Input)
     For I = 1 To Text_Len
          This_CharNum = AscW(Mid(Text_Input, I, 1))
          If This_CharNum < 135 Then Current_Val = This_CharNum - 32
          If This_CharNum > 134 Then Current_Val = This_CharNum - 100
          Current_Val = Current_Val * I
          PreCheckTotal = PreCheckTotal + Current_Val
          If This_CharNum = 32 Then This_CharNum = 194
          Printable_Text = Printable_Text & ChrW(This_CharNum)
     Next I
     Check_Val = (PreCheckTotal Mod 103)
     If Check_Val < 95 And Check_Val > 0 Then C128_CheckDigit = ChrW(Check_Val + 32)
     If Check_Val > 94 Then C128_CheckDigit = ChrW(Check_Val + 100)
     If Check_Val = 0 Then C128_CheckDigit = ChrW(194)
     Printable_Text = Printable_Text & C128_CheckDigit & ChrW(206) & End_Sequence
     PrecisionID_C128_A = Printable_Text
End Function

Public Function PrecisionID_C128_B(Text_Input As String) As String
     Printable_Text = ""
     PreCheckTotal = 104
     Printable_Text = ChrW(204)
     Text_Len = Len(Text_Input)
     For I = 1 To Text_Len
          This_CharNum = AscW(Mid(Text_Input, I, 1))
          If This_CharNum < 135 Then Current_Val = This_CharNum - 32
          If This_CharNum > 134 Then Current_Val = This_CharNum - 100
          Current_Val = Current_Val * I
          PreCheckTotal = PreCheckTotal + Current_Val
          If This_CharNum = 32 Then This_CharNum = 194
          Printable_Text = Printable_Text & ChrW(This_CharNum)
     Next I
     Check_Val = (PreCheckTotal Mod 103)
     If Check_Val < 95 And Check_Val > 0 Then C128_CheckDigit = ChrW(Check_Val + 32)
     If Check_Val > 94 Then C128_CheckDigit = ChrW(Check_Val + 100)
     If Check_Val = 0 Then C128_CheckDigit = ChrW(194)
     Printable_Text = Printable_Text & C128_CheckDigit & ChrW(206) & End_Sequence
     PrecisionID_C128_B = Printable_Text
End Function

Public Function PrecisionID_C128_C(Text_Input As String) As String
     End_Sequence = "  "
     Printable_Text = ""
     Correct_Text = ""
     Text_Len = Len(Text_Input)
     For I = 1 To Text_Len
          If IsNumeric(Mid(Text_Input, I, 1)) Then Correct_Text = Correct_Text & Mid(Text_Input, I, 1)
     Next I
     Text_Input = Correct_Text
     If (Len(Text_Input) Mod 2) = 1 Then Text_Input = "0" & Text_Input
     Printable_Text = ChrW(205)
     PreCheckTotal = 105
     PreCheckVal = 1
     Text_Len = Len(Text_Input)
     For I = 1 To Text_Len Step 2
          Current_Val = Mid(Text_Input, I, 2)
          If Current_Val < 95 And Current_Val > 0 Then Printable_Text = Printable_Text & ChrW(Current_Val + 32)
          If Current_Val > 94 Then Printable_Text = Printable_Text & ChrW(Current_Val + 100)
          If Current_Val = 0 Then Printable_Text = Printable_Text & ChrW(194)
          Current_Val = Current_Val * PreCheckVal
          PreCheckTotal = PreCheckTotal + Current_Val
          PreCheckVal = PreCheckVal + 1
     Next I
     Check_Val = (PreCheckTotal Mod 103)
     If Check_Val < 95 And Check_Val > 0 Then C128_CheckDigit = ChrW(Check_Val + 32)
     If Check_Val > 94 Then C128_CheckDigit = ChrW(Check_Val + 100)
     If Check_Val = 0 Then C128_CheckDigit = ChrW(194)
     PrecisionID_C128_C = Printable_Text & C128_CheckDigit & ChrW(206) & End_Sequence
End Function


Public Function PrecisionID_MOD10(Text_Input As String) As String
' This is a general MOD10 function like the one required for UCC/EAN-128
     Correct_Text = ""
     Text_Len = Len(Text_Input)
     For I = 1 To Text_Len
          If IsNumeric(Mid(Text_Input, I, 1)) Then Correct_Text = Correct_Text & Mid(Text_Input, I, 1)
     Next I
     Switch = 3
     PreCheckTotal = 0
     Text_Len = Len(Text_Input)
     For I = Text_Len To 1 Step -1
          This_CharNum = Mid(Text_Input, I, 1)
          PreCheckTotal = PreCheckTotal + This_CharNum * Switch
          Switch = 4 - Switch
     Next I
     I = (PreCheckTotal Mod 10)
     If I <> 0 Then
          Check_Digit = (10 - I)
     Else
          Check_Digit = 0
     End If
     MOD10 = Str(Check_Digit)
End Function





