check digit

S

sam

Hi,
I'm just after an idea of how I could go about doing the following:

0 0 0 0 0 8 4 4
2 1 2 1 2 1 2 1

The top line of numbers is just a formatted number that will change but
always be 8 digits. The bottom row of numbers are weights. These will not
change but the corresponding digit above is multiplied by the digit immediate
below. i.e. 4*1=4 4*2=8, 8*1=8
the 4, 8, 8 are then added and used in a caulculation to create a check digit.
What I am having trouble with is how to loop through the top number to
multiply out each corresponding digit in the second row. Any ideas?
 
G

Graham Mandeno

Hi Sam

Here are two functions I use for (a) generating a check digit to append to a
number and (b) validating a number where the last digit is assumed to be the
check digit. For some reason (I can't remember why!) I use a weight of 3
for alternate digits instead of 2.

Public Function Mod10Generate(InVal As Long) As Byte
Dim sVal As String, iSum As Integer, iPos As Integer, iDigit As Integer
sVal = InVal
For iPos = 1 To Len(sVal)
iDigit = CInt(Mid(sVal, Len(sVal) - iPos + 1, 1))
If iPos Mod 2 Then
iSum = iSum + iDigit * 3
Else
iSum = iSum + iDigit
End If
Next
Mod10Generate = (10 - (iSum Mod 10)) Mod 10
End Function

Public Function Mod10Validate(InVal As Long) As Boolean
Mod10Validate = Mod10Generate(InVal \ 10) = InVal Mod 10
End Function

You should be able to tweak this to work for your requirements.
 
S

sam

Thanks Graham, thats way more than I expected.
--
Thanks


Graham Mandeno said:
Hi Sam

Here are two functions I use for (a) generating a check digit to append to a
number and (b) validating a number where the last digit is assumed to be the
check digit. For some reason (I can't remember why!) I use a weight of 3
for alternate digits instead of 2.

Public Function Mod10Generate(InVal As Long) As Byte
Dim sVal As String, iSum As Integer, iPos As Integer, iDigit As Integer
sVal = InVal
For iPos = 1 To Len(sVal)
iDigit = CInt(Mid(sVal, Len(sVal) - iPos + 1, 1))
If iPos Mod 2 Then
iSum = iSum + iDigit * 3
Else
iSum = iSum + iDigit
End If
Next
Mod10Generate = (10 - (iSum Mod 10)) Mod 10
End Function

Public Function Mod10Validate(InVal As Long) As Boolean
Mod10Validate = Mod10Generate(InVal \ 10) = InVal Mod 10
End Function

You should be able to tweak this to work for your requirements.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

sam said:
Hi,
I'm just after an idea of how I could go about doing the following:

0 0 0 0 0 8 4 4
2 1 2 1 2 1 2 1

The top line of numbers is just a formatted number that will change but
always be 8 digits. The bottom row of numbers are weights. These will not
change but the corresponding digit above is multiplied by the digit
immediate
below. i.e. 4*1=4 4*2=8, 8*1=8
the 4, 8, 8 are then added and used in a caulculation to create a check
digit.
What I am having trouble with is how to loop through the top number to
multiply out each corresponding digit in the second row. Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top