' Code authored by Rob Williams - rob@taltech.com 2002-2003 ' Last Updated July 2003: Added UPC-A to UPC-E function. Function UPCE2A(UPCE As String) As String ' check the validity of the input data If not isNumeric(UPCE) then Msgbox ("UPC Codes must contain Numeric Data Only!") Exit Function End If Select Case Len(UPCE) Case 6 ' do nothing everything is OK UPCEString$ = UPCE Case 7 UPCEString$ = Left$(UPCE, 6) ' truncate last digit - assume that it is the UPCE check digit Case 8 UPCEString$ = Mid$(UPCE, 2, 6) ' truncate first and last digit ' assume that the first digit is the number system digit ' and the last digit is the UPCE check digit Case Else MsgBox "wrong size UPCE message" Exit Function End Select ' break up the string into its 6 individual digits Digit1$ = Mid$(UPCEString$, 1, 1) Digit2$ = Mid$(UPCEString$, 2, 1) Digit3$ = Mid$(UPCEString$, 3, 1) Digit4$ = Mid$(UPCEString$, 4, 1) Digit5$ = Mid$(UPCEString$, 5, 1) Digit6$ = Mid$(UPCEString$, 6, 1) Select Case Digit6$ ' expand the 6 digit UPCE number to a 12 digit UPCA number Case "0", "1", "2" ManufacturerNumber$ = Digit1$ + Digit2$ + Digit6$ + "00" ItemNumber$ = "00" + Digit3$ + Digit4$ + Digit5$ Case "3" ManufacturerNumber$ = Digit1$ + Digit2$ + Digit3$ + "00" ItemNumber$ = "000" + Digit4$ + Digit5$ ' original code was in error Case "4" ManufacturerNumber$ = Digit1$ + Digit2$ + Digit3$ + Digit4$ + "0" ItemNumber$ = "0000" + Digit5$ ' original code was in error Case Else ManufacturerNumber$ = Digit1$ + Digit2$ + Digit3$ + Digit4$ + Digit5$ ItemNumber$ = "0000" + Digit6$ End Select ' put the number system digit "0" together with the manufacturer code and Item number Msg$ = "0" + ManufacturerNumber$ + ItemNumber$ ' calculate the check digit - note UPCE and UPCA check digits are the same Check% = 0 ' initialize the check digit value For X% = 1 To 11 Test$ = Mid$(Msg$, X%, 1) Select Case X% Case 1, 3, 5, 7, 9, 11 Check% = Check% + Val(Test$) * 7 ' odd position digits multiplied by 7 Case 2, 4, 6, 8, 10 Check% = Check% + Val(Test$) * 9 ' even position digits multiplied by 9 End Select Next Check% = (Check% Mod 10) + 48 ' convert value to ASCII character value CheckChar$ = Chr$(Check%) ' check character UPCE2A = Msg$ + CheckChar$ ' put the pieces together and return End Function Function ConvertUPCAtoUPCE(UPCA) Dim csumTotal Dim holdString csumTotal = 0 ' The checksum working variable starts at zero ' If the source message string is less than 12 characters long, we make it 12 characters If Len(UPCA) < 12 Then holdString = "000000000000" & UPCA UPCA = Right(holdString, 12) End If If Left(UPCA, 1) <> "0" And Left(UPCA, 1) <> "1" Then MsgBox "Invalid UPC-E message" '(only 0 & 1 are valid) Else If Mid(UPCA, 4, 3) = "000" Or Mid(UPCA, 4, 3) = "100" Or Mid(UPCA, 4, 3) = "200" Then UPCE = Mid(UPCA, 2, 2) & Mid(UPCA, 9, 3) & Mid(UPCA, 4, 1) ElseIf Mid(UPCA, 5, 2) = "00" Then UPCE = Mid(UPCA, 2, 3) & Mid(UPCA, 10, 2) & "3" ElseIf Mid(UPCA, 6, 1) = "0" Then UPCE = Mid(UPCA, 2, 5) & Mid(UPCA, 11, 1) & "4" ElseIf Val(Mid(UPCA, 11, 1)) >= 5 Then UPCE = Mid(UPCA, 2, 5) & Mid(UPCA, 11, 1) Else MsgBox "Invalid UPC-E Message" End If End If ConvertUPCAtoUPCE = UPCE End Function