Just as the If step is made more flexible and dynamic by the addition of Else, so is looping improved by the Exit Loop If step. If you don't want a loop to continue through every record in your table (and lots of times you really don't!), Exit Loop If provides a way to target your loop to a specific number or group of records.There are many ways to define your exit point, but one of the most elegant methods is with a counter. Counters do precisely that: count. You can set them to start at zero, or any other number you need. When it comes to loops, counters are particularly useful in keeping track of how many times a loop has run, allowing you to stop it after as few or as many iterations as you need. For example, in this script we use a counter with the Exit Loop If to print only 10 invoices at a time.
To use a loop to print multiple invoices
1 .Choose Scripts > ScriptMaker (Control+Shift+S/Command+Shift+S). When the Define Scripts dialog box appears, click New.2 .When the Edit Script dialog box appears, type a descriptive name for the script in the Script Name text box (in this case Print Ten Invoices).3 .Scroll down to the Fields category in the script step list on the left and double-click Set Field.4 .In the Script Step Options section, click the "Specify target field" check box.5 .When the Specify Field dialog box appears, click Define Database (Figure 8.37) in the drop-down list.
Figure 8.37. You can define new fields when creating scripts.
6 .When the Define Database dialog box appears, click the Fields tab. Type gCounter for the field name and choose Number (Control+N/Command+N) as the Type. Click Create. Click the Options button. In the Options dialog box, click the Storage tab. Click the "Use global storage" check box (Figure 8.38). Click OK twice to return to the Specify Field dialog box.
Figure 8.38. gCounter is a Number field with a global storage option.
7 .Choose gCounter from the list of fields, then click OK.8 .In the Script Step Options section, click the Calculated result: Specify button (Figure 8.39).
Figure 8.39. Choose Specify to access the Specify Calculation dialog box.
9 .When the Specify Calculation window appears, type 0 and click OK.You now have a counter field that will start counting from 0 at the beginning of the script (Figure 8.40).
Figure 8.40. gCounter must be set to 0 before running the script.
10 .In the script step list on the left, double-click Loop and then double-click Perform Script.11 .In the Script Step Options section, click the Specify button, and double-click to choose Print Invoices from the Specify Script dialog box (Figure 8.41).
Figure 8.41. The sub-script Print Invoices will be executed in each loop.
12 .Scroll down to the Fields category in the script step list on the left and double-click Set Field.13 .In the Script Step Options section, click the "Specify target field" check box. When the Specify Field dialog box appears, choose gCounter, and click OK.14 .In the Script Step Options section, click the Calculated result: Specify button.15 .When the Specify Calculation window appears, scroll to gCounter in the field list on the left and double-click it. Type +1 to the right of the field name. Click OK (Figure 8.42).
Figure 8.42. gCounter is increased by 1 after each invoice is printed.
The script step will add one to whatever number is in the gCounter field.16 .In the step list on the left, double-click Exit Loop If (Figure 8.43).
Figure 8.43. Choose Exit Loop If while the Set Field step is selected.
17 .In the Script Step Options section, click Specify.18 .In the Specify Calculation dialog box, scroll to gCounter in the field list on the left and double-click to select it. Click greater than (>) in the Operators list and type 10 (Figure 8.44). Click OK.
Figure 8.44. The Exit Loop tests to see if Counter has reached 10. If it has, the loop is exited.
19 .Click OK twice to finish.The script will start by setting gCounter to 0, then it will print an invoice, add 1 to gCounter, and exit the loop (and script) after the tenth invoice.