Solved

How to continue do/while?

Posted on 2009-05-07
16
1,813 Views
Last Modified: 2012-05-06
OK, I give up guessing. How do I continue a loop? I've tried:

While | do while
    continue
    continue while
    next
   continue do
wend | end while | loop

none of these work. What's the right syntax? I'm ready to resort to goTo!
0
Comment
Question by:jmarkfoley
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 24323205
Sub doTest()
    Dim intCounter As Integer
    Dim intTest As Integer
    intTest = 1
    intCounter = 1
    Do While intTest = 1
        Debug.Print "This is loop number " & intCounter
        If intCounter >= 5 Then
            intTest = 0
        End If
        intCounter = intCounter + 1
    Loop
End Sub

Programming Fundamentals Using VBA
http://www.aspfree.com/c/a/VB.NET/Programming-Fundamentals-Using-VBA/6/
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24323362
Hi jmarkfoley,

This explains it nicely
http://visualbasic.freetutes.com/learn-vb6/lesson4.html


Good Luck!

Gary
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24323846
Thanks for the responses. Maybe I'm missing something in your examples and references, but I don't see the answer to my question. Perhaps I wasn't clear: I want to know how to CONTINUE, e.g.

Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then CONTINUE      ' skip to next record
    ' do stuff here
    :
Loop

The above is not valid in VBA ("sub or function not defined"), nor is:
While
    ... continue while
Wend

etc., all the various combinations I've tried as shown in my initial question don't work. So, how do I do a loop continuation in vba?
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24323971
You don't need continue ,that's the default unless I don't understand you
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24323997
OK, show me how to code the following (notice two CONTINUE statements):

Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then continue      ' skip to next record

     If moreErrors = True Then
        resultsOut inRec
        extraCnt = extraCnt + 1
        continue
    End If
       
    EmployeeId = strtok(inRec, ",", True)
    refNo = strtok(inRec, ",", False)
    AddrStreet = strtok(inRec, ",", False)
    AddrExtra = strtok(inRec, ",", False)
    City = strtok(inRec, ",", False)
    State = strtok(inRec, ",", False)
    zip = strtok(inRec, ",", False)
    zipPlus = strtok(inRec, ",", False)
    taxYear = strtok(inRec, ",", False)
    errorCode = strtok(inRec, ",", False)
    ' etc.
Loop
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24324015
Where do you have move to the next record/line?
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24324025
If you were using a recxordset you would have rs.movenext inside the loop
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24325354
This is just an example! It doesn't matter what the logic is inside the loop. I'm asking how to "continue" within a loop, not how to read records. But to answer your question, my "next record" is the line right under the 'while': Line Input #fnum, inRec

I don't think you understand my fundamental question. There are various looing constructs: While/Wend, Do While/Loop, etc. In most languages there are ways to alter the loop behavior: "break" - terminates the loop before the condition is met, and "continue" resumes the loop from the beginning bypassing the rest of the logic in the loop. The following are logical equivalents:

While <some condition>
   ' do something
  if <condition> continue     ' this should resume with the <some condition> test
  ' do more things
Wend

While <some condition>
  'do something
  if <condition> goTo there
  ' do more things
there:
Wend

So, I am trying to figure out VBA's "continue" syntax. Do you know if VBA even has a "continue"?

0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 46

Accepted Solution

by:
tbsgadi earned 250 total points
ID: 24325411
That's what I've been saying..there is no continue! If you want to exit there's exit
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24326579
> That's what I've been saying..there is no continue!

Really? That seems like a fundamental language component. Hmm. So, I either have to put everything inside one big IF:

While <some condition>
   ' do something
  if !<condition> then
      ' do more things
  end if
Wend

or use a goto as in my example above?
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24326651
Depending on what you want to do, you might want to use "Do until" or For...
0
 
LVL 29

Assisted Solution

by:QPR
QPR earned 250 total points
ID: 24330190
there is no continue because you have already specified that you want to "do" while a certain condition is true.
If you want to walk to the shop whuile it is not raining you don't need to continually tell yourself to walk... you just do it until you get there (or it starts raining at which point you exit).

Not trying to be smart, just trying to use an analogy to explain the do loop
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24347433
Anything else?
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24383406
QPR - I understand the analogy, I've been programming for 30 years and have taught various languages. Most languages, include VB.net [http://msdn.microsoft.com/en-us/library/801hyx6f(VS.80).aspx] permit one to alter loop interation via break (one gets an emergency call and has to interrupt the shopping prematurely and go home), or some condition arises that makes you want to bypass the rest of the logic in the loop (skip the current shop and move on to the next one). For example, a do or while or for loop that reads records from a file and processes them. If, immediately after reading a record one determines that it is a comment or blank, a 'continue' lets you skip to the next record. Possible programming contstructs are:

1. continue
Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then continue      'blank record, skip to next record
    ' do logic on good records
Loop

2. if
Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec) > 0 Then
        ' do logic on good records
    End If
Loop

3. goto
Do While Not EOF(fnum)
     Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then goTo nextRec   'blank record, skip to next record
    ' do logic on good records
nextRec:
Loop

#1 is preferable over #2 if the 'do logic on good records' bit is fairly extensive; plus there may be other continuation conditions along the way (errors in the input rec, relational data not found, etc.) which can lead to a rat's nest of nested If's. Apparently VBA does not have a 'continue' loop flow alteration command, so we are left with options #2 or #3.
0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 31578884
basically the answer is "can't do it", so I'll split the points between you for your efforts. Thanks.
0
 
LVL 1

Expert Comment

by:chillguy
ID: 27662080
Jmarkfoley's 3 methods have been tested and do work. Sadly, only VB.NET supports the continue loop statement. Goodluck!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now