【发布时间】: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 的。
【问题讨论】: