Modulus 10

  • Thread starter Thread starter DrEvil
  • Start date Start date
D

DrEvil

Hi Guys,
We are trying to do Modulus 10 on string that is 57 numbers long
(015116946868000060740000012463000112100031150000000016699).
We need to be able to multiply every odd number with 2 and then sum up even
numbers.
Not sure if I need to go in more detail about Modulus 10 but here is the
link that explains everything about it and gives few different samples for
bunch of programs (but not Access).
http://en.wikipedia.org/wiki/Luhn_algorithm
 
Try out this function:

Public Function TestMod10(strNumbers As String) As Boolean
Dim aryResult As Variant
Dim strModNumber As String
Dim lngX As Long
Dim lngFinalNumber As Long
Dim blnTranslate As Boolean

aryResult = Array("0", "2", "4", "6", "8", "1", "3", "5", "7", "8")
blnTranslate = False

For lngX = Len(strNumbers) To 1 Step -1
If blnTranslate Then
strModNumber = aryResult(CLng(Mid(strNumbers, lngX, 1))) &
strModNumber
blnTranslate = False
Else
strModNumber = Mid(strNumbers, lngX, 1) & strModNumber
blnTranslate = True
End If
Next lngX

For lngX = 1 To Len(strModNumber)
lngFinalNumber = lngFinalNumber + CLng(Mid(strModNumber, lngX, 1))
Next lngX

TestMod10 = lngFinalNumber Mod 10 = 0
End Function
 
Try this. Its based on something similar I used elsewhere. Not very elegabt
but it works.

It takes the number as a string and breaks it into its component 'integers'
in an array. Then creates a second integer array based on the first. Finally
spinning backwards through the array to create the Mod10 number:

Dim strng, Arr(57) As String, Brr(57) As Integer, Result, Result9, i

strng = "015116946868000060740000012463000112100031150000000016699"

For i = 1 To 57
Arr(i) = Mid(strng, i, 1)
Select Case Arr(i)
Case "0"
Brr(i) = 0
Case "1"
Brr(i) = 1
Case "2"
Brr(i) = 2
Case "3"
Brr(i) = 3
Case "4"
Brr(i) = 4
Case "5"
Brr(i) = 5
Case "6"
Brr(i) = 6
Case "7"
Brr(i) = 7
Case "8"
Brr(i) = 8
Case "9"
Brr(i) = 9
End Select
Next

For i = 0 To 56
Result = Result + Brr(57 - i)

i = i + 1
If 2 * Brr(57 - i) > 9 Then
Result9 = 1 + (Brr(57 - i) - 10)
Else
Result9 = (2 * Brr(57 - i)
End If
Result = Result + Result9
Next

MsgBox Result
 
Sorry, bottom bit should read:

For i = 0 To slen - 1
Result = Result + Brr(slen - i)

i = i + 1
If 2 * Brr(slen - i) > 9 Then
Result9 = 1 + (2 * Brr(slen - i) - 10)
Else
Result9 = (2 * Brr(slen - i))

End If
Result = Result + Result9
Next

Cheers.

BW
 
I'll test both of the suggestions when I come from holidays,
I have did one myself in meantime but I don't think it's as efficient as it
should be.
Thank you all for quick replys, i will let you guys know how it comes out.
 
Back
Top