toofaced
toofaced

Reputation: 151

How to calculate checksum digit for EAN-14?

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.

Upvotes: 0

Views: 2071

Answers (3)

R.Alonso
R.Alonso

Reputation: 1065

EAN8 AND EAN13 IN VB.NET

  Public Function generateEAN(ByVal barcode As String, EsEan8 As Boolean) As String

    Dim first As Integer = 0
    Dim second As Integer = 0
    If EsEan8 Then
        barcode = Right(("00000000" & barcode), 7)
    Else
        barcode = Right(("000000000000" & barcode), 12)
    End If

    If barcode.Length() = 7 OrElse barcode.Length() = 12 Then

        For counter As Integer = 0 To barcode.Length() - 1 Step 2
            first = (first + barcode.Substring(counter, 1))

            If counter + 1 < barcode.Length Then
                second = (second + barcode.Substring(counter + 1, 1))
            End If

        Next
        If EsEan8 Then
            first = first * 3
        Else
            second = second * 3
        End If

        Dim total As Integer = second + first
        Dim roundedNum As Integer = (10 - (total Mod 10)) Mod 10
        barcode = barcode & roundedNum

    End If
    Return barcode



End Function

Upvotes: 0

toofaced
toofaced

Reputation: 151

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

Upvotes: 1

Gustav
Gustav

Reputation: 55806

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

Upvotes: 0

Related Questions