Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exiting a For/Next loop while in a subroutine 2

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
I'm trying to read some invoices (XML files) and handle them based on certain criteria. In this case, I'm checking the grand total of each one.
If the total is a positive number: continue with the rest of the script.
If the total is zero or a negative number: report it, stop processing this particular file and move on to the next one.

I have two problems with the bit of code below:
1. When I encounter a zero or negative amount in the subroutine, how do I stop processing this file and move on to the the next one? (I'm calling the subroutine from within a For/Next loop)
2. Isn't there a "cleaner" way of determining if the total is positive/negative/zero? The nested If..Then stuff just seems a bit crude.

Code:
Sub subCreditMemo
  If strTotal > 1 Then
    'continue
  Else
    If strTotal = 0 Then
      rptFile.WriteLine strInvoiceNumber & " (" & Left(strCompany, 3) & ") " & strStore & " - This invoice has a grand total of zero."
    Else
      If strTotal < 0 Then
        rptFile.WriteLine strInvoiceNumber & " (" & Left(strCompany, 3) & ") " & strStore & " - This invoice should be converted to a credit invoice."
      Else
      End If
    End if
  End if
End Sub

 
Since I have no idea what your for loop looks like, here's an example of how to exit a for loop. I'd probably make the CreditMemo a function rather than a sub, so it can return a value letting you know whether to continue or not.

As for your If Then Else, you can do a Case TRUE. Or you can do:
If > 1
ElseIf = 0
Else
End If

Code:
For Each strTotal In strTotals
  If doCreditMemo = 0 Then
    Exit For 
  End If
Next

Function doCreditMemo
  doCreditMemo = 0
  Select Case TRUE
    Case strTotal = 0
      rptFile.WriteLine strInvoiceNumber & " (" & Left(strCompany, 3) & ") " & strStore & " - This invoice has a grand total of zero."
    Case strTotal < 0
      rptFile.WriteLine strInvoiceNumber & " (" & Left(strCompany, 3) & ") " & strStore & " - This invoice should be converted to a credit invoice."
    Case Else
      doCreditMemo = 1
  End Case
End Function
 
Thanks, Skie. That appears to work just fine. I had a feeling that a function was more suitable for what I was trying to do.

Also, I didn't know you could do a "case TRUE". I don't think I've ever seen that before.

Thanks again for your help.

PS: There's a minor problem with the code you posted. It should have been "End Select" instead of "End Case". I just wanted to point that out in case another beginner tried to use the code the way it was posted.
 
Have a look at the Exit Do, Exit For and Exit Function instructions.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I spoke a little too soon. The script stops processing any more invoices if it encounters a zero or negative amount.

I have three invoices:
320950 = 9.99
323075 = 0.00
323758 = -9.99

320950 is processed ok.
323075 triggers the exit.
323758 is never processed.

Looking back at my original post, my title is a bit misleading. I don't need to exit the For loop. As I mentioned in question 1, I need a way to stop processing the current invoice and move on to the next one.

If it helps to know, I'm cycling through a collection of XML files in a particular directory.
Code:
For Each xmlFile in colFiles
...
call the function
...
Next

I'm beginning to think that I just need to forget using a function or sub and put the code in the main part of the script.
 
I figured it out thanks to you, PHV. I needed to add "Exit Function". It seems to be working properly now.

This is what it looks like now:
Code:
Function doCreditMemo
  doCreditMemo = 0
  Select Case TRUE
    Case strTotal = 0
      rptFile.WriteLine strInvoiceNumber & " (" & Left(strCompany, 3) & ") " & strStore & " - This invoice has a grand total of zero."
      Exit Function
    Case strTotal < 0
      rptFile.WriteLine strInvoiceNumber & " (" & Left(strCompany, 3) & ") " & strStore & " - This invoice should be converted to a credit invoice."
      Exit Function
    Case Else
      doCreditMemo = 1
  End Select
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top