Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to continue do/while?

Posted on 2009-05-07
16
Medium Priority
?
2,656 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
LVL 46

Accepted Solution

by:
tbsgadi earned 750 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 750 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

824 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