Two loops and some calculation.
Posted on 2004-11-04
I need to calculate the control number in a Swedish identification number. As in 19640823-3234. I need to calculate the last integer in this string as it is now.
For i = 1 To Len(strPartial)
intNumbers(j) = Mid(strPartial, i, 1): j = j + 1
For l = 0 To UBound(intNumbers)
If k Mod 2 > 0 Then
intDivBy = 1
intDivBy = 2
intResult = intNumbers(k) * intDivBy
If intResult > 9 Then
intParts(0) = Left(intResult, 1)
intParts(1) = Right(intResult, 1)
intResult = intResult + intParts(0) + intParts(1)
intResult = intResult + intResult
k = k + 1
strPartial contains 640823323 since the last integer, which is the control number, can be calculated through the stuff that is in strPartial. Each character (integer) in strPartial needs to be multiplied with either 1 or 2, depending on its position. The first int is multiplied with 2, the next with 1, the next with 2 and so on.
If the product is 16 (8*2) the added result is 1+6, not 16. That's why I add the array to keep hold of my results.
The code up above does give me a number, but it's rarely the correct one. Does anyone understand my problem or have a clue on what is the problem with my code. I'm soon going insane over this...answers appreciated. Please ask additional questions if you don't understand what I'm trying to calculate.