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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error msg NEXT WITHOUT FOR 2

Status
Not open for further replies.

jambet

Technical User
Jan 8, 2007
5
US
Ok, I am probably blind (comes with age, I guess) but I can't figure out this error. When the following code is run I get the error msg "Next without For"
Code:
    Set db = CurrentDb()
    Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
    If rs.RecordCount > 0 Then
    
    For i = 1 To 37
    If IsDate(f("date" & i)) Then
        With rs
        .MoveFirst
            Do While Not .EOF
            If rs![Date Scheduled] = f("date" & i) Then
                If Len(Trim(Nz(f("text" & i), ""))) > 0 Then
                    f("text" & i) = f("text" & i) & vbCrLf & _
                     rs![job#]
                Else
                    f("text" & i) = rs![job#]
                End If
            End If
            .MoveNext
            Loop
    Next i
    End If
Is there a possibility the interveining code is bypassing the NEXT?
Any sharpeyed help will be appreciated.
Thanks
 
You need an 'End with' for your 'with' clause
Code:
    For i = 1 To 37
    If IsDate(f("date" & i)) Then
        With rs
        .MoveFirst
            Do While Not .EOF
            If rs![Date Scheduled] = f("date" & i) Then
                If Len(Trim(Nz(f("text" & i), ""))) > 0 Then
                    f("text" & i) = f("text" & i) & vbCrLf & _
                     rs![job#]
                Else
                    f("text" & i) = rs![job#]
                End If
            End If
            .MoveNext
            Loop
       [COLOR=red]End With[/color]
    Next i

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
jambet,

Not sure if it's a problem, but seems like it could be - you have your For statement outside the first If..Then, but its corresponding Next is inside the If..Then:
Code:
    [highlight]For i = 1 To 37[/highlight]
    [highlight cyan]If IsDate(f("date" & i)) Then[/highlight]
        With rs
        .MoveFirst
            Do While Not .EOF
            If rs![Date Scheduled] = f("date" & i) Then
                If Len(Trim(Nz(f("text" & i), ""))) > 0 Then
                    f("text" & i) = f("text" & i) & vbCrLf & _
                     rs![job#]
                Else
                    f("text" & i) = rs![job#]
                End If
            End If
            .MoveNext
            Loop
    [highlight]Next i[/highlight]
    [highlight cyan]End If[/highlight]
I would swap the Next i and End If statements.

HTH,

Ken S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top