0

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.

Erik A
  • 31,639
  • 12
  • 42
  • 67
toofaced
  • 141
  • 4
  • 18
  • You shouldn't multiply the first subtotal with 3. Instead you should multiply the second subtotal with 3, i.e. the digits at the odd positions. – Codo Dec 08 '16 at 15:47
  • @Codo: Is it only for EAN-14? Because it works for EAN-8 and EAN-13 when I coded like as I have shown above? – toofaced Dec 08 '16 at 16:31
  • GS1 explains how to [calculate a check digit](http://www.gs1.org/how-calculate-check-digit-manually). If you work from the first to the second last digit, it changes whether the even or odds digits are multiplied with 3. If you work backwards, it's more consistent across the different EAN code lengths. – Codo Dec 09 '16 at 09:02

3 Answers3

1

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
toofaced
  • 141
  • 4
  • 18
0

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
Gustav
  • 53,498
  • 7
  • 29
  • 55
0

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
R.Alonso
  • 989
  • 1
  • 8
  • 9