give me a number and what the correct answer should be and i'll see if i can find the problem...

Solved

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

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.

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.

6 Comments

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?

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)

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)

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

VBA function that will take a string parameter and return a string wiht first letter of the string capitalised. | 2 | 45 | |

VBA: Do Loop with Input Box | 14 | 38 | |

Run code from text file in vb | 1 | 17 | |

Microsoft Access combo box help | 2 | 14 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**22** Experts available now in Live!