Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2008
    Posts
    78

    Unanswered: Simple Control to Bottom of Loop

    Sorry for the total newbie question. I'm having trouble jumping to the bottom of a loop construct. I don't want to 'Exit Do', I just want to jump to the bottom of my loop and keep going.

    Code:
    Do
        Inspect field1 for condition
            If condition is true 
                Perform some action
                Inspect for a subcondition
                    Perform some action
                    RSWIP.MoveNext
                    *** GO TO "Loop Until RSWIP.EOF"
                End if SubCondtion
            End if Condition
            
            
        Inspect field2 for condition
            If condition is true 
                Perform some action
                Inspect for a subcondition
                    Perform some action
                    RSWIP.MoveNext
                    *** GO TO "Loop Until RSWIP.EOF"
                End if SubCondtion
            End if Condition
       
    
        Inspect field3 for condition
            If condition is true 
                Perform some action
                Inspect for a subcondition
                    Perform some action
                    RSWIP.MoveNext
                    *** GO TO "Loop Until RSWIP.EOF"
                End if SubCondtion
            End if Condition
       
       
        ** Arrive here if no conditions were true
           
        RSWIP.MoveNext        
    
    Loop Until RSWIP.EOF
    I've seen where I can use a GOTO but please advice me for something else!
    The GO TO would work because i could stick it right before I move the record pointer.. but i dont want to use a go to due to the stigma

  2. #2
    Join Date
    Nov 2008
    Posts
    8
    Quote Originally Posted by PonPending
    I've seen where I can use a GOTO but please advice me for something else!
    The GO TO would work because i could stick it right before I move the record pointer.. but i dont want to use a go to due to the stigma
    Use the GOTO. It's clear and simple.

    If you really, really, really don't want to use the GOTO, you'll need some kind of flag that you test in each outer if test.

    Something like:

    Code:
    Dim skipToBottom as Boolean 
    
    skipToBottom = False
    If test1 then
        ' ... whatever
        If I_should_now_skip_to_bottom then
            skipToBottom = True
        End If
    end If
    
    If not skipToBottom and test_2 then
        ' whatever
        If I_should_now_skip_to_bottom then
            skipToBottom = True
        End If
    end if
    
    ' repeat until bored
    I would say that the GOTO is probably the cleaner option.

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I'm gonna disagree Code using GOTO can quickly become spaghetti code. You just want mutually exclusive control of flow?
    Code:
    Do
        Inspect field1 for condition
            If condition is true 
                Perform some action
                Inspect for a subcondition
                    Perform some action
                End if SubCondtion
        Inspect field2 for condition
            ELSEIf condition is true 
                Perform some action
                Inspect for a subcondition
                    Perform some action
                End if SubCondtion
            ELSEIf condition is true 
                Perform some action
                Inspect for a subcondition
                    Perform some action
                End if SubCondtion
            End if Condition
       
        ** Arrive here if no conditions were true
           
        RSWIP.MoveNext        
    
    Loop Until RSWIP.EOF
    Out of curiosity - what is "Perform some action"?
    Last edited by pootle flump; 11-03-08 at 16:50.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    Aug 2008
    Posts
    78
    Yea i didn't want to test for an additional condition each step of the way going down. I have 10 main groups of conditions and embedded subconditions that any one of them could trigger the 'go to'.

    ok, I'll use the goto.. it's pretty clear since I'm only goign to the bottom of the loop and it would clutter up the conditions I'm testing for.

    thanks

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    ElseIf doesn't result in every condition being evaluated. Also, it results in less lines of code -> less clutter.

    I googled "VBA goto statement", knowing what would come up. Selected highlights:
    http://www.techotopia.com/index.php/...c_Flow_Control
    http://www.pbdr.com/vbstd/VB%20Coding%20Standards(2).htm
    http://my.safaribooksonline.com/9780...8/ch13lev1sec3

    Use under extreme caution.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  6. #6
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Also - spotted an error in your code logic - if a condition is met you jump forward TWO rows. You know why I noticed? Looking at the ELSEIF code block - the flow of control is more obvious. Anyway, I corrected my code.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  7. #7
    Join Date
    Aug 2008
    Posts
    78
    Curious about "Perform some task"

    I'm building an error log. Each item on the main list could have multiple errors. Some errors are so severe that it can be written to the log w/out needing to look for other errors. Also, the log has the ability to report multiple problems on one line, so if I've already started a log entry, simply update a different field in the existing entry, not create a new entry.

    I need to stop looking if certain errors are uncovered, and I need not create a new entry if i've already started a log entry for this particular item in the RSWIP. Just a couple of reasons to break out and just get the next record and continue.

    If I use a GOTO and I know I'm not branching to any other conditional execution, I think I can live with it. Cleaner than using flags and testing for those flags along with all my other condition testing.

    I was actually looking for a 'system' go to that always takes me to the bottom of a loop condition, kind of like a 'Continue' statement.
    Last edited by PonPending; 11-03-08 at 17:58.

  8. #8
    Join Date
    Aug 2008
    Posts
    78
    Quote Originally Posted by pootle flump
    ElseIf doesn't result in every condition being evaluated. Also, it results in less lines of code -> less clutter.

    I googled "VBA goto statement", knowing what would come up. Selected highlights:
    http://www.techotopia.com/index.php/...c_Flow_Control
    http://www.pbdr.com/vbstd/VB%20Coding%20Standards(2).htm
    http://my.safaribooksonline.com/9780...8/ch13lev1sec3

    Use under extreme caution.

    I clicked on that first link and feel a little relieved:

    "Before going into detail of how to use the GoTo statement it is important to be aware that the use of the GoTo statement is generally considered to be bad programming practice. A programmer under pressure will often reach for the GoTo statement when they have programmed themselves into a corner and need some way of getting to another section of code....One valid use, which we will use in this example, is to exit a subroutine or function when an error is found"

    I'm breaking out based on an error condition....

  9. #9
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Lol!
    Testimonial:
    pootle flump
    ur codings are working excelent.

  10. #10
    Join Date
    Nov 2008
    Posts
    8

    GOTO leading to spaghetti code

    I'm gonna disagree Code using GOTO can quickly become spaghetti code.
    (emphasis added)

    That's absolutely true. AND when used very sparingly, in cases like this, it can enhance readability and maintainability. If the point is to write maintainable code, I would submit that a single, carefully used GOTO is far better than a huge nested if/elseif block that has to keep checking for the same condition.

    I would never, ever, suggest that GOTO should be used freely. Given the limited toolset that VBA offers, sometimes GOTO is the best of a bad lot of options.

    "Don't use GOTO" is a great rule. The reason we have rules is to make us think before breaking them.

  11. #11
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    There's no huge nested code here -
    Code:
    IF
    ...GOTO
    END IF
    IF
    ....GOTO
    END IF
    is being used instead of
    Code:
    IF.....
    ELSEIF.....
    ELSEIF.....
    END IF
    The IF blocks are identical in both solutions (there are no duplicated checks), except that a reader only has to view the left hand most lines of code using ELSEIF to know EXACTLY what the flow of control is. The IF blocks are mutually exclusive: so use ELSEIF. That is what it is for. For a programmer to see that these GOTO blocks are mutually exclusive (s)he must read the content of EVERY one and identify the GOTO, assuming they even know that they must do this. Remember these blocks currently contain a placeholder for further code - the GOTO will become less clear once this is filled in.
    ELSEIF is documented, standard and reliable. Sticking GOTO into a bunch of IF blocks is not standard, goes against basic programming good practice and prone to error, as I pointed out in the OP's original code.

    This is exactly the sort of case NOT to use GOTO.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  12. #12
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    BTW - I noticed one other thing. I prefer:
    Code:
     DO WHILE NOT RSWIP.EOF AND NOT RSWIP.BOF
    .....
    LOOP
    If the recordset is empty, the loop won't execute and the code won't crash.

    You might prefer using GOTO instead
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •