[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Two loops and some calculation.

Posted on 2004-11-04
Medium Priority
182 Views
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.
0
Question by:bobamatics
• 3
• 2

LVL 4

Expert Comment

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

LVL 4

Accepted Solution

learning_t0_pr0gram earned 700 total points
ID: 12501860
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

LVL 3

Expert Comment

ID: 12501992
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

LVL 3

Expert Comment

ID: 12502023
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

LVL 4

Expert Comment

ID: 12504169
k needs to be 0 at first, so he's fine there...
0

Author Comment

ID: 12511162
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month18 days, 8 hours left to enroll