0

我正在尝试在 vba 访问中验证 EAN 14 UPC 代码。我试图在网上找到它,但没有运气。我刚刚找到了 EAN 8 和 EAN 13。所以,我只是尝试将其编码为类似于 EAN 13,如下所示:

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

它不工作。我遇到的问题是虽然我输入了有效的 EAN,但它给了我 EAN14-BAD。我认为我的验证代码不起作用。+ (Val(Mid(Barcode, 13, 1))) 我刚刚在 EAN13 验证代码上添加了最后一行 。请帮忙。

4

3 回答 3

0

当我将奇数乘以 3 时,它起作用了,如下所示:

 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
于 2016-12-09T14:52:57.677 回答
0

你试过8个字符吗?

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
于 2016-12-08T15:47:40.053 回答
0

VB.NET 中的 EAN8 和 EAN13

  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
于 2020-12-01T16:08:55.467 回答