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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Routine to validate VIN number 1

Status
Not open for further replies.

BMDE

Programmer
Apr 4, 2006
11
0
0
Does anyone who work in the auto industry have code that uses VIN check digit algorithm to validate a VIN number?

Any help would be greatly appreciated.
 
You'll notice that the code will respond with "VIN number seems to be valid". The main word is SEEMS. Outside of the auto industry, I can't find any that are 100 percent sure.
 
Here is my stab at translated to VB. I think it works
Code:
Public Function isVIN(strVIN As String) As Boolean
 Dim I As Integer
 Dim intCount As Integer
 Dim intCount2 As Integer
 Dim aModelYears() As Variant
 Dim aWeights() As Variant
 Dim aCharacters() As Variant
 Dim aCharacterValues() As Variant
 Dim aCheckDigits() As Variant
 Dim aVIN_Array(0 To 15) As Variant
 Dim intTotal As Integer
 Dim intRemainder As Integer
' Check VIN lenth
If Not Len(strVIN) = 17 Then
  MsgBox "ERROR - VIN lenth must be 17 characters long." & Chr(13) & "You only entered " & Len(strVIN) & " characters."
  Exit Function
End If
'make VIN all caps
strVIN = UCase(strVIN)
' model years 1980 - 2000
aModelYears = Array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "V", "W", "X", "Y")
' weights for multiplyer
aWeights = Array("8", "7", "6", "5", "4", "3", "2", "10", "9", "8", "7", "6", "5", "4", "3", "2")
'characters
aCharacters = Array("A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
'// character values
aCharacterValues = Array("1", "2", "3", "4", "5", "6", "7", "8", "1", "2", "3", "4", "5", "7", "9", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
' check digit 0 - 9 and 10 = X
aCheckDigits = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X")
'// push each character of the vin into an array removing the 9th character (check digit)
MsgBox "Your VIN's check digit is " & Mid(strVIN, 9, 1)
For intCount = 0 To 16
  If intCount < 8 Then
    aVIN_Array(intCount) = Mid(strVIN, intCount + 1, 1)
  ElseIf intCount > 8 Then
    aVIN_Array(intCount - 1) = Mid(strVIN, intCount + 1, 1)
  End If
Next intCount
'replace with char values
For intCount = 0 To 15
  For intCount2 = 0 To UBound(aCharacters)
    If aVIN_Array(intCount) = aCharacters(intCount2) Then
      aVIN_Array(intCount) = aCharacterValues(intCount2)
    End If
  Next intCount2
Next intCount
'For I = 0 To UBound(aVIN_Array)
'  Debug.Print aVIN_Array(I)
'Next I
'// preform the math
For intCount = 0 To 15
  intTotal = intTotal + aWeights(intCount) * aVIN_Array(intCount)
Next intCount
'debug.print intTotal
intRemainder = intTotal Mod 11
'Debug.Print intRemainder
  If Not Mid(strVIN, 9, 1) = aCheckDigits(intRemainder) Then
    MsgBox "ERROR - Check digit does not compute. Recheck your VIN number." _
          & " Computed check digit:" & aCheckDigits(intRemainder) _
          & " Your check digit: " & Mid(strVIN, 9, 1)
  Else
    MsgBox "Computed check digit: " & aCheckDigits(intRemainder) _
           & " VIN number seems to be valid."
    isVIN = True
  End If

End Function
I get the results from the wikepidia example

Needs some additional error checking for invalid characters, but it is a start.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top