Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with the for loop in a recordset 1

Status
Not open for further replies.

lanelouna

Programmer
Dec 19, 2002
71
GB
hello all
plz i am desperate with my code
it is not working
it keeps giving me this error: Next without For
i have a table, which looks as follows
courrier classe A B C D E F G H I J....Z
1 A
1 B 2 1 2 2 3 1
1 C
1 D 1 1 2 3
. .
. .
1 Z
2 A
2 B

i have an array called mes classes for example it has the following elements (B, D)
i read line by line this table, if the classe = myclasses(i), i create an array of numerators: classnum, where i put the classes which has a value of 1 or 2, and i create an array of denominators, where i put the classes whose values are 3 or 2

can anyone tell me why my code is not working
i would really appreciate it

toutesClasses = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
Set RS1 = db.OpenRecordset("A0TDEFAUT_SOUSPROT", dbOpenDynaset)
With RS1
.FindFirst "COURRIER = " & H & ""
.FindNext "COURRIER = " & H & ""
Do Until RS1.EOF
For i = 0 To nbr

If (RS1!Classe = mesClasses(i)) Then
nj = 0
dj = 0
cnj = 0
cdj = 0
For j = 0 To 25
clsFieldname = toutesClasses(j)
If RS1.Fields(clsFieldname) = 1 Or RS1.Fields(clsFieldname) = 2 Then
classeNum(nj) = toutesClasses(j)
nj = nj + 1
End If
If RS1.Fields(clsFieldname) = 3 Or RS1.Fields(clsFieldname) = 2 Then
classeDenom(dj) = toutesClasses(j)
dj = dj + 1
End If
Next j
If nj > 0 Then
counterNum(cnj) = nj - 1
cnj = cnj + 1
End If
If dj > 0 Then
counterDenom(cdj) = dj - 1
cdj = cdj + 1
End If
RS1.MoveLast
RS1.MoveNext
Else: Next i <--------------------- the error
End If
RS1.MoveNext
Loop
End With
RS1.Close

thanks in advance
i hope u can help; i am stuck here
Lanelouna

 
Hi, the problem you've got is that you can't put your next inside the else of an if statement.

Effectively what you have is something similar to..

Dim i As Integer

For i = 1 To 10

If i = 3 Then
'do something
Else
Next i
End If

The structure needs to be...


Dim i As Integer

For i = 1 To 10

If i = 3 Then
'do something
Else
'do something else
End If

Next i

You will need to change the logic of your routine to fit this structure.

There are two ways to write error-free programs; only the third one works.
 
lanelouna:

I believe the If statement which begins after the For i = 0 statement must be ended before the Next i statement.

Your code diagram basically looks like this:


For i = 0 To nbr

If (RS1!Classe = mesClasses(i)) Then
.
.
.
Else

Next i

End If


As you can see, you are intersecting two 'loops' which is not allowed. You will have to rework the logic.

Hope this helps

Vic
 
yeah thank you both, i can now see the pb clearly
well dats whatam gonna do: rework the structure
and refresh my mind a bit! wish it was as simple as a me!refresh command
cheers
Lanelouna
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top