Solved

How to continue do/while?

Posted on 2009-05-07
16
1,892 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

822 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