Kim said:
First record:
input = 3295
output= 32955 (ok)
second record:
input= 4296
output= 42966 (false, should be 42961)
third record:
input= 6297
output= 62976 (false, should be 62970)
OK, I could not use a recordset of course, so I put your inputs into an array and ran your code to get your results. I then did this to help you see what was going on:
<%
Dim kontrollnummer
Dim raknare
Dim Summa
Dim referens
dim ar, i
Dim faktor
ar=array("3295","4296","6297")
for i = 0 to 2
referens = ar(i)
faktor = 7
Response.Write "<hr><b>" & referens & "</b><br>"
Response.Write "<TABLE><tr><th>raknare</th><th>factor" & _
"</th><th>Summa (pre)</th><th>Summa (post)</th></tr>"
For raknare = Len(referens) To 1 Step -1
Response.Write "<tr><td>" & raknare & "</td>"
Response.Write "<td>" & faktor & "</td>"
Response.Write "<td>" & Summa & "</td>"
Summa = Summa + Mid(referens, raknare, 1) * faktor
Response.Write "<td>" & Summa & "</td></tr>"
If faktor = 7 Then
faktor = 3
ElseIf faktor = 3 Then
faktor = 1
ElseIf faktor = 1 Then
faktor = 7
End If
Next
Response.Write "</table>"
kontrollnummer = Right((10 - (Summa Mod 10)), 1)
Response.Write kontrollnummer & "<BR>"
next
%>
Running this code yields this result:
--------------------------------------------------------------------------------
3295
raknare factor Summa (pre) Summa (post)
4 7 35
3 3 35 62
2 1 62 64
1 7 64 85
5
--------------------------------------------------------------------------------
4296
raknare factor Summa (pre) Summa (post)
4 7 85 127
3 3 127 154
2 1 154 156
1 7 156 184
6
--------------------------------------------------------------------------------
6297
raknare factor Summa (pre) Summa (post)
4 7 184 233
3 3 233 260
2 1 260 262
1 7 262 304
6
As you should see from this, the problem is the failure to initialize Summa before each loop. Adding "Summa = 0" to the code, right below the "faktor=7" line yields the correct results:
--------------------------------------------------------------------------------
3295
raknare factor Summa (pre) Summa (post)
4 7 0 35
3 3 35 62
2 1 62 64
1 7 64 85
5
--------------------------------------------------------------------------------
4296
raknare factor Summa (pre) Summa (post)
4 7 0 42
3 3 42 69
2 1 69 71
1 7 71 99
1
--------------------------------------------------------------------------------
6297
raknare factor Summa (pre) Summa (post)
4 7 0 49
3 3 49 76
2 1 76 78
1 7 78 120
0