# Two loops and some calculation.

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
Next i

For l = 0 To UBound(intNumbers)
If k Mod 2 > 0 Then
intDivBy = 1
Else
intDivBy = 2
End If
intResult = intNumbers(k) * intDivBy
If intResult > 9 Then
intParts(0) = Left(intResult, 1)
intParts(1) = Right(intResult, 1)
intResult = intResult + intParts(0) + intParts(1)
Else
intResult = intResult + intResult
End If
k = k + 1
Next l

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.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
give me a number and what the correct answer should be and i'll see if i can find the problem...
0
Commented:
intResult = intNumbers(k) * intDivBy

you're resetting intResult in that line of code, i'm not sure if that's what u want, judging by the code below that:

intResult = intResult + intParts(0) + intParts(1)
or
intResult = intResult + intResult

could that be the problem?
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Pl give what input given, exactly what output expected?!?!?!

And your else part is wrong...
intresult = intresult+intresult
will give you intresult = 32 if input intresult value is 16.

so modify it to
remainder = intresult mod 10
intresult = remainder + ((intresult - remainder)/10)

0
Commented:
Pl ignore my previous post
I just now understood a little more of your program
1. REMOVE THE BIG LETTERS IN THE FOLLOWING CODE
ACTUALLY THERE IS NO NEED FOR ELSE PART

intResult = INTRESULT+ intParts(0) + intParts(1)
Else
intResult = INTRESULT+ intResult
End If

2. yu have not initialized k

give
k = 1 before the following line

For l = 0 To UBound(intNumbers)

0
Commented:
k needs to be 0 at first, so he's fine there...
0
Author Commented:
I solved it myself before I read here, but as you say I messed it up with not storing whatever came out of the loop each time. I merely added a new variable storing the results continueously. Thanks.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.