Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function: Mod10 check digit with weights of 7,5,3,2 1

Status
Not open for further replies.

linousa

IS-IT--Management
Mar 8, 2013
79
US
Need help writing code snippet for Modulus 10 check digit with weights of 7,5,3,2. As shown below:
mod10_o1ieos.png

Thank you!
 
Code:
Public Function GetCheckdigit(VarScanlineData As Variant) As Integer
  Dim Weights As New Collection
  Dim WeightedDigits As New Collection
  Dim I As Integer
  Dim J As Integer
  Dim NumberOfDigits As Integer
  Dim Digit As Integer
  Dim WeightedDigit As Integer
  Dim WeightedSum As Integer
  Dim GrandSum As Integer
  Dim GrandSumMod10
  Dim strDigit As String
  Weights.Add (7)
  Weights.Add (5)
  Weights.Add (3)
  Weights.Add (2)
  'Get Number of
  NumberOfDigits = Len(CStr(VarScanlineData))
  ' Not sure here if it is always 7 digits. It is a lot simpler if this is the case
  ' But I will assume the weight pattern just repeats itself so the scanline data can be any length
  
  For I = 1 To NumberOfDigits
    Digit = CInt(Mid(CStr(VarScanlineData), I, 1))
    J = J + 1
    WeightedDigit = Digit * Weights(J)
    Debug.Print "Digit:" & Digit & " Weighted Digit: " & WeightedDigit
    If J = Weights.Count Then J = 0
    'Add the weighted digits to a collection
    WeightedDigits.Add (WeightedDigit)
  Next I
    
  For I = 1 To WeightedDigits.Count
    WeightedDigit = WeightedDigits(I)
    strDigit = CStr(WeightedDigit)
    For J = 1 To Len(strDigit)
      WeightedSum = WeightedSum + CInt(Mid(strDigit, J, 1))
    Next J
    Debug.Print "Weighted Sum " & WeightedSum
    GrandSum = GrandSum + WeightedSum
    WeightedSum = 0
  Next I
    Debug.Print "GrandSum " & GrandSum
    'Mod 10 of Grand Sum
    GrandSumMod10 = GrandSum Mod 10
    Debug.Print GrandSum & " Mod 10: " & GrandSumMod10
    'Get Check digit
    If GrandSumMod10 = 0 Then
      GetCheckdigit = 0
    Else
      GetCheckdigit = 10 - GrandSumMod10
    End If
    Debug.Print "Check digit: " & GetCheckdigit
End Function
test
Code:
GetCheckDigit(8529831)

Results:
Code:
Digit:8 Weighted Digit: 56
Digit:5 Weighted Digit: 25
Digit:2 Weighted Digit: 6
Digit:9 Weighted Digit: 18
Digit:8 Weighted Digit: 56
Digit:3 Weighted Digit: 15
Digit:1 Weighted Digit: 3

Weighted Sum 11
Weighted Sum 7
Weighted Sum 6
Weighted Sum 9
Weighted Sum 11
Weighted Sum 6
Weighted Sum 3

GrandSum 53

53 Mod 10: 3

Check digit: 7
 
Nice when you get thanks and feedback from your hard work, ain't it Maj?

Darrylles [evil]

Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top