What else might cause a "Compile Error: Loop Without Do"?

In Access 2010, I am finishing up a very long function (prints to 11 pages) which translates a delineated text file into table data  It's has 15 "Do While" loops and countless "If...Then...ElseIF...ElseIF...Else" statements nested in various combinations.  It has been running great as I've developed it step by step.  Unfortunately, this morning I've been struggling with a "Compile Error: Loop Without Do" message which highlights the last "Loop" statement in the code.  That "Loop" should return to the "Do While" statement at the very beginning of the function If I add the hyphen in front to REM it out, then I get the opposite message of having a "Do While" statement without a "Loop".

I have poured over it time and time again.  The count of "Do While" statements and "Loop" statements are equal, and I can't find any place where a loop is misplaced inside of an If...Then statement making it conditional.

I thought I had the loops running on Friday when I left for the weekend, and I've just been tweaking other lines in between, but I can't find anything I've done recently that would cause this to happen.

Does anyone have any advice or tips on how to troubleshoot something like this in a timely manner?

I don't want to go through REM'ing out 11 pages of code line by line trying to figure out which group is preventing it from compiling.

Thanks,
Cevin
Answer
Answer

Is this all one big function?  I usually try to keep my functions smaller.

Depending on the function, you might be able to extract some (or all) the loops into separate routines that you call from your current function.  If you can do so you might find it easier to trouble shoot.

Do all the IF statements have a corresponding End If?  That may be causing the error (erroneous error message pointing at the wrong line).

For instance, the following generates an error - Loop without do and highlights the Loop line

Public Function badFunction()

Do Until 1 = 2

    If 1 = 2 Then

       MsgBox "Where is the error?"

Loop

End Function

John P Spencer
Access MVP 2002-2005, 2007-2011

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 2,444 Applies to: