Loop without Do

S

Sash

I tried to format this so it would be easier to look at. I'm trying to do a
nested loop. This program is 7 pages, so I took the middle section out as
you will see below. I can't see why I'm getting the aforementioned error
message.


Do While Not rs.EOF 'First Loop for initial RS
With rs4
If Not rs4.BOF And Not rs4.EOF Then
rs4.MoveFirst
Do While Not rs4.EOF ' Second Loop for RS4

If rs4.Fields("DEBT_NO") <> DebtNo Then
HD = "HD" & Chr(9) & rs.Fields("DEBTOR_ID") & Chr(9) &
rs4.Fields("CLT_ID")
HD = HD & Chr(9) & rs4.Fields("CLT_REF_NO") & Chr(9)
HD = HD & rs4.Fields("CLT_REF_NO") & Chr(9) & Chr(9) & Chr(9)
Print #1, HD
DebtNo = rs4.Fields("DEBT_NO")

------------------lots of other code-----------------------------

rs.MoveNext '1st loop
Loop

rs4.MoveNext
End If
Loop '2nd loop
rs4.Close
End If
End With
 
K

Klatuu

I see 1 Do and 2 Loops

Do While Not rs.EOF 'First Loop for initial RS
rs.MoveNext '1st loop
Loop
rs4.MoveNext
End If
Loop '2nd loop

You also have End If outside the loop that should be in the loop.

But, without all the code, I don't know what else might be there.
 
R

Robert Morley

This is very minor, but you do realize that "Do While Not" is the same as
"Do Until", don't you? Not huge, but it may help make it a tiny bit easier
to read.

Unlike some other languages, the placement of the condition determines
whether the loop is guaranteed to run at least once, not the actual keyword.
So if your condition is placed with the Do, the condition is evaluated
before looping; if you put your condition with the Loop keyword, the loop
will run at least once before the condition is evaluated.


Rob
 
G

George Nicholson

I'm trying to do a nested loop.
Nested loops require the inner loop be completed (or exited) before
continuing the outer loop. Your posted code does not do that, it seems to
get the loops mixed up. Nested means one loop within (and entirely within)
the other. What you have seems to be overlapping loops which is an
interesting, but unsupported, concept.

Also: any Ifs or Withs "started" within a loop need to be closed (i.e., have
an End If, or End With) within the loop as well. "Loop without Do" errors
will occur if you have an unclosed "If..." or similar construct. I translate
that message to really mean:

"Hey, I've encountered an unexpected "Loop" statement that I can't handle.
I'm missing a "Do" or "End If" or "End With" or something. Sorry I can't be
more specific, but if this is a loop, it is not self contained and I am
stuck."

*************
Purpose: For each record in rs, loop through all records in rs4.

Do While Not rs.EOF
With rs4
If Not rs4.BOF And Not rs4.EOF Then
rs4.MoveFirst
Do While Not rs4.EOF
If rs4.Fields("DEBT_NO") <> DebtNo Then
' 7 pages (!!) of code here
End If
rs4.MoveNext
Loop ' refers to rs4
'move to next rs record *after* completing or exiting rs4
loop
rs.MoveNext
End If
End With
Loop ' refers to rs
rs4.Close
rs.Close
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top