PMH4514
asked on
UPC-A Check Digit Algorithm - VB
Could somebody please provide me a complete visual basic function to take an 11 digit number and calculate the
UPC-A check-digit?
I'm sending the 11 digit number to a barcode printer and it's working fine - the printer internally is calculating the check-digit and printing the code with the extra digit, but I'd like to calculate the check-digit on my own so that I can store the completed UPC value for the original 11-digit number in a database field.
Thanks
-Paul
UPC-A check-digit?
I'm sending the 11 digit number to a barcode printer and it's working fine - the printer internally is calculating the check-digit and printing the code with the extra digit, but I'd like to calculate the check-digit on my own so that I can store the completed UPC value for the original 11-digit number in a database field.
Thanks
-Paul
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nice. Thanks, However, your line:
If Len(strUPC) <> 11 Or Len(strUPC) <> 12 Then Exit Function 'Wrong length
isn't quite right. that will cause the function to fail everytime. (true or false = true)
and I don't believe you can calculate a UPC-A check digit on a string that's 12 in length, as the total length of the code should be 12 including the check digit.
However, minus that one length check, and with 11 digits, the calculation works correctly so I'm accepting the answer.
thanks!
-Paul
If Len(strUPC) <> 11 Or Len(strUPC) <> 12 Then Exit Function 'Wrong length
isn't quite right. that will cause the function to fail everytime. (true or false = true)
and I don't believe you can calculate a UPC-A check digit on a string that's 12 in length, as the total length of the code should be 12 including the check digit.
However, minus that one length check, and with 11 digits, the calculation works correctly so I'm accepting the answer.
thanks!
-Paul
Happy to help. I was thinking you might want to verify the check digit on a UPC that already has one. I see I should have used And instead of Or.
ASKER
yup - I verified your algorithm against a bunch of UPC codes my barcode printer generated and they were all right.
Nice work!
if you're feeling helpful, I just posted the same question, only to do it with a SQL Server stored procedure (see the Microsoft SQL section) and I referenced your VB algorithm.
https://www.experts-exchange.com/questions/20452751/UPC-A-Check-Digit-Algorithm-SQL.html
Nice work!
if you're feeling helpful, I just posted the same question, only to do it with a SQL Server stored procedure (see the Microsoft SQL section) and I referenced your VB algorithm.
https://www.experts-exchange.com/questions/20452751/UPC-A-Check-Digit-Algorithm-SQL.html
Make the second to last line read:
intTmp1 = (10 - (intTmp1 Mod 10)) Mod 10
Otherwise it will return a check digit of 10 when it's supposed to be zero.
intTmp1 = (10 - (intTmp1 Mod 10)) Mod 10
Otherwise it will return a check digit of 10 when it's supposed to be zero.
Private Function genCheckDigit(strUPC As String) As Integer
genCheckDigit = -1
Dim intTmp1 As Integer
Dim intTmp2 As Integer
If Len(strUPC) <> 11 Or Len(strUPC) <> 12 Then Exit Function 'Wrong length
If Not IsNumeric(strUPC) Then Exit Function 'check if it's a number
intTmp1 = 0 + _
Mid$(strUPC, 1, 1) + _
Mid$(strUPC, 3, 1) + _
Mid$(strUPC, 5, 1) + _
Mid$(strUPC, 7, 1) + _
Mid$(strUPC, 8, 1) + _
Mid$(strUPC, 11, 1)
intTmp1 = intTmp1 * 3
intTmp2 = 0 + _
Mid$(strUPC, 2, 1) + _
Mid$(strUPC, 4, 1) + _
Mid$(strUPC, 6, 1) + _
Mid$(strUPC, 8, 1) + _
Mid$(strUPC, 10, 1)
intTmp1 = intTmp1 + intTmp2
intTmp1 = 10 - (intTmp1 Mod 10)
genCheckDigit = intTmp1
End Function