Stop Loop and return to program code error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rookie in PA

    Stop Loop and return to program code error

    I want loop to stop if cancel is clicked and eturn to program. Here is my code. Stumped - sorry if it's basic.
    Code:
    Do while not EOF
    .
    .
    .
    .
    Printer.EndDoc
    If Label9 = 5 Then ' use 20 for production
    Label9 = 0
    intpress = MsgBox("Print NEXT batch? (NO will cancel printing)?", vbQuestion + vbOKCancel, "Continue?")
    If intpress = vbCancel Then
    End ' Think problem here!
    Else
    End If
    End If
    Loop
    Close ifile
    End Sub
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. Use Exit Sub if you want to jump out of your subroutine at line 11 above. However, this will simply stop the sub altogether and will not close any open file cleanly. I'd suggest redesigning your sub so that you use a boolean variable of some kind as part of your loop test:

    Code:
    Dim blPrintMore as Boolean
    blPrintMore = True
    Do while Not EOF And blPrintMore
    ..
     blPrintMore = Not intPress = vbCancel
     if blPrintmore then
      'your print section here
     End If
    Loop
    -Stewart
    Last edited by Stewart Ross; Oct 25 '10, 06:01 AM.

    Comment

    • Rookie in PA

      #3
      Thank you!

      Comment

      Working...