访问VBA - 创建文本框公式检查(检查最后一位数)

时间:2016-04-11 17:53:37

标签: vba excel-vba ms-access access-vba excel

我必须在文本框中使用VBA中的公式创建一些数字字段检查输入值,但不知道如何开始。这是一个例子:

文本框字段值 - 132549

  1. 你排除6,所以你使用13254
  2. 然后你做产品并与其他数字相加如下: 4 * 2 + 5 * 3 + 2 * 3 + 3 * 4 + 1 * 5 = 46

  3. 然后你做46 mod 11 = 2

  4. 然后11减去2 = 9,此处的结果必须与最后一位数相匹配(在此示例中,最后一位数字是正确的)

  5. 我是一名中级VBA程序员,但这是我的头脑。任何建议都非常感谢!

4 个答案:

答案 0 :(得分:2)

你必须编写自定义函数,它将一个数字作为输入参数并返回布尔值(true / false)。 A"懒惰的程序员"实现可能如下所示:

Public Function IsValidNumber(ByVal iNum As Long) As Boolean
Dim consums As String, digits As String
Dim i As Integer, j As Integer

'control sums
consums = "23345"
'convert number to its string representation and reject last digit
digits = Left(CStr(iNum), 5)
'reverse string
digits = StrReverse(digits)
'loop through the chars in string
For i = 1 To Len(digits)
    'convert single char to digit and calculate sum
    j = j + CInt(Mid(digits, i, 1)) * CInt(Mid(consums, i, 1))
Next
'get modulo
i = j Mod 11
'deduct the result of modulo from constant
j = 11 - i

'return
IsValidNumber = (j = CInt(Right(CStr(iNum), 1)))

End Function

怎么称呼它?创建如下过程:

Sub Test()
Dim i As Long

i = 132549

MsgBox IsValidNumber(i)

End Sub

运行它(F5)并检查会发生什么。

注意:您必须添加新模块才能将上面的代码粘贴到其中。

见下文"视觉表现" for...next循环步骤。

|==================================================|
| Iteration | Control number | Current digit | Sum |
| (step)    |  (multiplier)  |(reverse order)|     |
|==================================================|
|     1     |        2       |       4       |   8 |
|     2     |        3       |       5       |  15 |
|     3     |        3       |       2       |   6 |
|     4     |        4       |       3       |  12 |
|     5     |        5       |       1       |   5 |
|==================================================|

答案 1 :(得分:1)

  

我正在询问上面公式的VBA代码,我知道如何使用Access   和VBA编码。我需要的是告诉我如何做的人   从文本框中提取每个数字,将它们相乘并对这些结果进行SUM。   然后使用Mod函数,依此类推,如下所述。

据我所知,主要问题是从文本中获取数字。 我认为你可以使用下一个逻辑(它只显示想法)。

Dim nLen as Integer    
Dim stInput as string

stInput = nz(Me.Field)
nLen = Len(stInput)

Dim arrNumbers(nLen) As Integer
Dim nIndex asInteger
dim nValue as Intger

For nIndex = 0 to nLen
    nValue = Mid(stInput, nIndex, 1)
    arrNumbers(nIndex) = nValue
Next nIndex

'-- you can use arrNumbers to get access to your numbers

答案 2 :(得分:1)

你要求的可能是Modulus-11检查。您还可以进行Modulus-10检查,两者都可以合并为一个函数:

Public Function ModulusCheck(ByVal strNum As String, ByVal intModulus As Integer) As Integer

  ' Checks that strNum is a Modulus-10 or -11 number with
  ' a valid check digit.
  ' Non-numeric characters are ignored.

  ' Maximum length of number.
  Const cintNumLenMax = 32

  Dim strChk    As String
  Dim strTmp    As String
  Dim strVal    As String
  Dim intChr    As Integer
  Dim intLen    As Integer
  Dim intSum    As Integer
  Dim intVal    As Integer
  Dim intWeight As Integer
  Dim intCount  As Integer
  Dim intChk    As Integer

  Select Case intModulus
    Case 10, 11
      intLen = Len(strNum)
      If intLen > 0 Then
        ' Remove non-numeric characters.
        For intCount = 1 To intLen
          intChr = Asc(Mid(strNum, intCount))
          If intChr >= 48 And intChr <= 57 Then
            strTmp = strTmp & Chr(intChr)
          End If
        Next intCount
        strNum = strTmp
        intLen = Len(strNum)

        If intLen > 1 Then
          If intLen <= cintNumLenMax Then
            ' Separate number and check digit.
            strVal = Mid(strNum, 1, intLen - 1)
            strChk = Mid(strNum, intLen, 1)
            For intCount = 1 To intLen - 1
              intVal = Val(Mid(strVal, intLen - intCount, 1))
              Select Case intModulus
                Case 10
                  intWeight = 1 + (intCount Mod 2)
                  intVal = intWeight * intVal
                  intVal = Int(intVal / 10) + (intVal Mod 10)
                Case 11
                  intWeight = 2 + ((intCount - 1) Mod 6)
                  intVal = intWeight * intVal
              End Select
              intSum = intSum + intVal
            Next intCount
            intSum = intSum + Val(strChk)
            intChk = (intSum Mod intModulus = 0)
          End If
        End If
      End If
  End Select

  ModulusCheck = intChk

End Function

要检查一个数字,该函数将返回True表示确定。但是:

ValidNumber = ModulusCheck("132549", 11)

将为False,而:

ValidNumber = ModulusCheck("132543", 11)

返回True。

所以要么你有一些非标准的验证,要么你已经错了9或6,因为通过Modulus-11检查的“13254”的校验位是3。

答案 3 :(得分:0)

我已经自己解决了 - 我知道这看起来有些幼稚,但它的确有效。

  Sub test()

        Dim Seven As Integer
        Dim Six As Integer
        Dim Five As Integer
        Dim Four As Integer
        Dim Three As Integer
        Dim Two As Integer
        Dim One As Integer

        Dim Seven_Sum As Integer
        Dim Six_Sum As Integer
        Dim Five_Sum As Integer
        Dim Four_Sum As Integer
        Dim Three_Sum As Integer
        Dim Two_Sum As Integer
        Dim One_Sum As Integer

        Dim All_Sum As Integer
        Dim Do_Modulus As Integer
        Dim Check_Digit As Integer

        Seven = Mid(Text0, 7, 1)
        Seven_Sum = Seven * 2

        Six = Mid(Text0, 6, 1)
        Six_Sum = Six * 3

        Five = Mid(Text0, 5, 1)
        Five_Sum = Five * 4

        Four = Mid(Text0, 4, 1)
        Four_Sum = Four * 5

        Three = Mid(Text0, 3, 1)
        Three_Sum = Three * 6

        Two = Mid(Text0, 2, 1)
        Two_Sum = Two * 7

        One = Mid(Text0, 1, 1)
        One_Sum = One * 8

        All_Sum = Seven_Sum + Six_Sum + Five_Sum + Four_Sum + Three_Sum + Two_Sum + One_Sum

        Do_Modulus = All_Sum Mod 11
        Check_Digit = 11 - Do_Modulus

'Finally check for correct serial number validation based on last digit in Textbox
        If Mid(Text0, 8, 1) <> Check_Digit Then

        MsgBox "This serial number is faulty. Last digit of this entered serial number should be :" & Check_Digit
        End If

        End Sub