【问题标题】:Access VBA giving incorrect results when comparing two numbers比较两个数字时访问 VBA 给出不正确的结果
【发布时间】:2014-11-15 03:41:09
【问题描述】:

我有一个表链接到用户输入 ISBN 号的表单。我正在检查以确保 ISBN 有效。但是,当我需要比较两个数字时,我被错误地告知它们不匹配,而它们确实匹配。

Private Sub isbn_BeforeUpdate(Cancel As Integer)

Dim isbn As String
Dim cleanIsbn As Double
Dim onlyIsbn As Double
Dim checkDigit As Integer
Dim legnth As Integer

length = 0
isbn = Forms!frmPubInfo!isbn.Value

' Strip out all hyphens
For i = 1 To Len(isbn)
    Dim ch As String
    ch = Mid(isbn, i, 1)
    If IsNumeric(ch) Then
        length = length + 1
        Dim num As Integer
        num = CInt(ch)
        cleanIsbn = (cleanIsbn * 10) + num
    End If
Next

' Check if 13 numbers
If length = 13 Then
    Dim xBy3 As Boolean
    Dim total As Integer
    Dim calcCheckDigit As Integer

    total = 0
    xBy3 = False

    ' Calculate total amount
    For j = 1 To 12
        ch = Mid(cleanIsbn, j, 1)
        If xBy3 = True Then
            total = total + (ch * 3)
            xBy3 = False
        Else
            total = total + ch
            xBy3 = True
        End If
    Next

    ' Get calculated check digit
    calcCheckDigit = 10 - (total Mod 10)

    ' Extract check digit
    checkDigit = Mid(cleanIsbn, 13, 1)

    ' Debug output
    MsgBox ("Actual CheckDigit: " & checkDigit & vbNewLine & _
    "Calculated CheckDigit: " & calcCheckDigit)

    ' Check if check digit and calculated check digit match
    If checkDigit <> calculatedCheckDigit Then
        MsgBox ("checkDigit and calcCheckDigit are not the same")
    Else
        MsgBox ("They match! ISBN is good!")
    End If

Else
    ' Display error
    MsgBox ("Not enough numbers!")
End If

End Sub

当我进入“检查校验位和计算的校验位是否匹配”时,If 语句总是说它们不匹配,即使上面的调试输出给了我相同的两个数字。

我试过了:

  • 将 checkDigit 变量声明为字符串。
  • 将 checkDigit 变量转换为带有 CStr 的 If 语句中的字符串。
  • 在带有 CInt 的 If 语句中将 checkDigit 变量转换为整数。

我最初认为这是数据类型的问题,但如果我在比较它们时将它们转换为相同的类型,那不会是问题,对吧?

这都是使用 Access 2013 的。

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    伙计……我不敢相信我一开始没有看到这个。我测试了您的代码并得到了与您相似的结果。仔细观察后,我注意到 if 语句是错误的。简而言之,您需要使用Option Explicit,并且会发现此错误。 Option Explicit 确保声明所有变量。不会抛出错误。

    您的语句包含一个空值

    If checkDigit <> calculatedCheckDigit Then
    

    您没有名为calculatedCheckDigit 的变量,它应该是calcCheckDigit

    If checkDigit <> calcCheckDigit Then
    

    只是一个旁注:你的去除连字符的代码显然有效,但我提供了这个调整。

    ' Dim as string since it is treated as one with Mid anyway. 
    ' In practice Len didnt give accurate results while it was Double. 
    Dim cleanIsbn As String
    
    ' Strip out all hyphens
    cleanIsbn = Replace(isbn, "-", "")
    
    ' Check if 13 numbers
    If (Len(cleanIsbn) = 13) And IsNumeric(cleanIsbn) Then
        ' Process Stuff
    Else
        ' Display error
         MsgBox "Does not appear to be a valid ISBN value!"
    End If
    

    获取 isbn 并使用替换删除连字符。在此更改之后,如果结果 cleanIsbn 是 13 个字符长且为数字,那么您可以假设它是一个很好的处理值。

    ISBN 参考

    我必须查一下,但 ISBN 号码背后的数学公式可供参考here

    【讨论】:

    • 我跑了很多次我的代码,我不敢相信我没有听懂。忘记Option Explicit 也是一个愚蠢的错误。我也刚刚发现我拼写错误length,并且没有声明变量ij。您去除连字符的方法要好得多。我最初想这样做,但我也想去掉任何随机字符。我现在意识到这并不重要,因为我正在检查所有字符是否都是数字。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多