How to calculate checksum digit for EAN-14?

﹥>﹥吖頭↗ 提交于 2019-12-12 06:37:31

问题


I am trying to validate EAN 14 UPC code in vba access. I am trying to find it online but no luck. I just found for EAN 8 and EAN 13. So, I just tried to code it similar to EAN 13 as following:

If Len(Barcode) = 14 Then
    'do the check digit for EAN 14 for anything 14 long
    checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
                            + (Val(Mid(Barcode, 4, 1))) _
                            + (Val(Mid(Barcode, 6, 1))) _
                            + (Val(Mid(Barcode, 8, 1))) _
                            + (Val(Mid(Barcode, 10, 1))) _
                            + (Val(Mid(Barcode, 12, 1)))
        checkDigitSubtotal = (3 * checkDigitSubtotal) _
                            + (Val(Mid(Barcode, 1, 1))) _
                            + (Val(Mid(Barcode, 3, 1))) _
                            + (Val(Mid(Barcode, 5, 1))) _
                            + (Val(Mid(Barcode, 7, 1))) _
                            + (Val(Mid(Barcode, 9, 1))) _
                            + (Val(Mid(Barcode, 11, 1))) _
                            + (Val(Mid(Barcode, 13, 1)))

    If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
        Validate_UPC = "EAN14-BAD"
        Exit Function
    End If
        Validate_UPC = "EAN14-GOOD"
        Exit Function
    End If

It is not working. Issue I am having is although i enter valid EAN, it gives me EAN14-BAD. I think my validating code is not working. I just added last line + (Val(Mid(Barcode, 13, 1))) on EAN13 validation code. Please help.


回答1:


Did you try with 8 characters?

If Len(Barcode) = 8 Then
    'do the check digit for EAN 8 for anything 8 long
    checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
                            + (Val(Mid(Barcode, 4, 1))) _
                            + (Val(Mid(Barcode, 6, 1))) 
        checkDigitSubtotal = (3 * checkDigitSubtotal) _
                            + (Val(Mid(Barcode, 1, 1))) _
                            + (Val(Mid(Barcode, 3, 1))) _
                            + (Val(Mid(Barcode, 5, 1))) _
                            + (Val(Mid(Barcode, 7, 1))) 

    If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
        Validate_UPC = "EAN8-BAD"
        Exit Function
    End If
    Validate_UPC = "EAN8-GOOD"
    Exit Function
End If



回答2:


It worked when I switched multiplying odd ones by 3 as following:

 If Len(Barcode) = 14 Then       
    checkDigitSubtotal = (Val(Mid(Barcode, 1, 1))) _
                            + (Val(Mid(Barcode, 3, 1))) _
                            + (Val(Mid(Barcode, 5, 1))) _
                            + (Val(Mid(Barcode, 7, 1))) _
                            + (Val(Mid(Barcode, 9, 1))) _
                            + (Val(Mid(Barcode, 11, 1))) _
                            + (Val(Mid(Barcode, 13, 1)))
        checkDigitSubtotal = (3 * checkDigitSubtotal) _
                            + (Val(Mid(Barcode, 2, 1))) _
                            + (Val(Mid(Barcode, 4, 1))) _
                            + (Val(Mid(Barcode, 6, 1))) _
                            + (Val(Mid(Barcode, 8, 1))) _
                            + (Val(Mid(Barcode, 10, 1))) _
                            + (Val(Mid(Barcode, 12, 1)))

    If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
        Validate_UPC = "EAN14-BAD"
        Exit Function
    End If
        Validate_UPC = "EAN14-GOOD"
        Exit Function
    End If


来源:https://stackoverflow.com/questions/41043300/how-to-calculate-checksum-digit-for-ean-14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!