Solved

How to continue do/while?

Posted on 2009-05-07
16
2,047 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print previewing the record showing on the form 4 51
Form performance table vs. query 27 70
Microsoft Access report help 4 38
ISeries Remote Location Route entry 2 28
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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