error checking a check digit

David b

Registered User.
Local time
Today, 09:54
Joined
Mar 2, 2003
Messages
102
In my livestock app I need to check the ear tag number of cattle using a check digit.
The tag numbers are pre issued by the agriculture dept in this format. - UK107248500321.
In this example the 5 is the check digit.
The number with the 5 taken out - 10724800321 is divided by 7 and 1 is added to the remainder to get the check digit.

What do I need in the after update event of the tagno control on a form to confirm the check digit ?

TIA
David B
Hexham UK
 
Is the tag number always the same length? Isthe check digit always in the same position?
 
David:

A couple of questions...

1. Is the Check Digit always in the same place (i.e 7th position after the UK is removed)

2. How is (10724800321 / 7) + 1 eqaul to 5...
by my calcualtions it's 1532114332.5714285714285714285714
Are you saying the 5 after the decimal is the check??
 
Mile-O...

2 seconds ahead of me....

he he
 
Here's a routine that may give you some ideas.

Code:
Private Sub YourControl_AfterUpdate()

   ' stage 1 - working on the premise that UK is always the code at the start, we need to get the number only

   Dim strTagNumber As String

   strTagNumber = Mid(YourControl, 3)

   ' stage 2 - the check digit would appear to be the 7th number in the overall number

   Dim intCheckDigit As Integer

   intCheck = Int(Mid(strTagNumber, 7, 1))

   ' stage 3 - remove the check digit from the overall number

   strTagNumber = Left(strTagNumber, 6) & Mid(strTagNumber, 8)

   ' stage 4 - divide the number by 7 and add 1 to see if it resembles the check digit

   If (Val(strTagNumber) Mod 7)+1 = intCheckDigit Then
      MsgBox "This is correct"
   Else
      MsgBox "This is incorrect."
   End If

End Sub
 
jfgambit,

(10724800321 MOD 7 ) + 1 = 5
 
Thanks for the replies.
For births after 01/01/2000 the tag is always the same format so i will start with - If DOB = >01/01/2000 then --

The check digit is always in the same place

The sum is "old fashioned" long division. remember that ?
The answer I get is 1532114331 remainder 4
Add 1 to the remainder gives 5
David
 
You can edit that routine I posted to take the date into account; that should do it.
 
Thanks for the reply
I am getting "overflow" on this line If (Val(strTagNumber) Mod 7) + 1 = intCheckDigit Then -

What is causing that ?
David
 
Ah! The number is too large. Try this in its place.

If (CLng(strTagNumber) Mod 7) + 1 = intCheckDigit Then
 
Damn, the number is too long.

I've tried a few other conversions and can't get it to work.

Anyone else got any ideas?
 
Been fiddling with some thing like this.
Do you see any problems with it

David

> Const strcTest As String = "UK107248500321"
>
> Dim strTemp As String
> Dim varNum1 As Variant
> Dim varNum2 As Variant
> Dim varNum3 As Variant
> Dim varNum4 As Variant
>
> strTemp = Mid$(strcTest, 3, 6) '107248
> strTemp = strTemp & Right$(strcTest, 5) '00321
> varNum1 = CDec(strTemp)
> varNum2 = CDec(Int(CDec(varNum1) / CDec(7)))
> varNum3 = varNum2 * CDec(7)
> varNum4 = (varNum1 - varNum3) + 1
> Debug.Print varNum4
 
That works. Congrats! :p

Just in your own code, don't use the variable as a Constant if the string is going to change.
 

Users who are viewing this thread

Back
Top Bottom