In the previous section "To control scripts with user input," we use the Show Custom Dialog step to give the user information and provide a choice between going forward or canceling. This method is a clean way to communicate simple information or prompt the user to choose between two opposite actions, but it's otherwise limited. If you need more complex user interaction, you can use the Show Custom Dialog script step to create a dialog box that can gather text input from the user into fields. A dialog box created using the Show Custom Dialog step can display up to three fields into which the user can enter values.
To illustrate how useful this method can be, we'll create a script that displays a dialog box prompting the user to enter any combination of the contents of the Last Name, City, and State fields to perform a find. The script will use global storage fields to capture the user input and set the find criteria from the contents of these global storage fields.
1 .
Choose File > Define >Database (Control+Shift+D/Command+Shift+D). When the Define Database dialog box appears, click the Fields tab.
2 .
Type gLastName in the Field Name box, choose Text (Control+T/Command+T) as the field type, and click Create.
3 .Click the Options button. When the Options dialog box appears, click the Storage tab. Click the Use global storage box (Figure 8.52). Click OK.
4 .Highlight the gLastName field and click Duplicate twice. Rename one copy gCity. Click Change. Rename the second copy gState (Figure 8.53).
5 .
Choose Scripts > ScriptMaker (Control+Shift+S/Command+Shift+S). When the Define Scripts dialog box appears, click New.
6 .
In the Edit Scripts dialog box, type a name for the script (we use Dialog Find).
7 .Scroll down to the Miscellaneous section in the step list on the left and double-click Show Custom Dialog (Figure 8.54).
8 .In the Script Step Options section, click Specify.
9 .
The "Show Custom Dialog" Options dialog box opens with the General tab selected. Under Title, enter the text for the title of the dialog box (Figure 8.55).
10 .
Under Message, enter the text of the dialog box message. In our example, we give the user explicit instructions on how to use the dialog box (Figure 8.56).
11 .The default button names are already inserted for the three possible buttons. If you want to rename any of the buttons, highlight the text and type the new caption. If a button field is left blank, the button will not appear in the custom dialog box (Figure 8.57).
12 .Click the Input Fields tab. Check "Show input field #1."
13 .
When the Specify Field dialog box appears, double-click the gLastName field.
14 .
In the Label input box, enter Last Name for the field label (Figure 8.58).
15 .Click the Show input field #2 check box. Repeat steps 1214 for the gCity field, and then again for the gState field.
16 .Click OK to return to the Edit Script dialog box. In the step window on the left scroll up to the Control section. Double- click If.
17 .
In the Script Step Options section, click Specify.
18 .When the Specify Calculation dialog box appears, click the View drop-down menu and scroll down to "Get functions." Click to select it and display the detailed Get functions list.
19 .In the list of Get functions, double-click the Get(LastMessageChoice) function (Figure 8.59).
20 .In the formula box, click to the right of the status function and type =2 (Figure 8.60). Click OK.
21 .
In the step window on the left, double-click Exit Script.
This If step looks at which button you clicked. If you chose the first button (Never Mind), it cancels the script without any action.
22 .To make sure that the next script steps are inserted after the If statement, click to select End If in the script-assembly list.
23 .
Under the Navigation category of the script step list on the left, double-click Enter Find Mode. Uncheck the Pause box (Figure 8.61).
24 .We need to set the find criteria to use values only for the fields the user selects. In the step window on the left, double-click If.
25 .In the Script Step Options section, click Specify.
26 .When the Specify Calculation dialog box appears, double-click not in the Operators list (Figure 8.62).
27 .
In the function list, double-click IsEmpty.
28 .
Replace the text parameter by double-clicking gLastName in the field list (Figure 8.63). Click OK.
29 .In the step window on the left, scroll down to the Fields category and double-click Set Field. In the Script Step Options section, click the "Specify target field" check box.
30 .When the Specify Field dialog box appears, double-click to choose Last Name and return to the Edit Script dialog box.
31 .In Script Step Options, click the Calculated result: Specify button.
32 .When the Specify Calculation dialog box appears, double-click gLastName in the field list. Click OK (Figure 8.64).
If the user has typed anything into the Last Name section of the custom dialog box, this portion of the If step transfers it to the LastName field.
33 .Click the If step you created in step 24. Hold down the Shift key and then select the Set Field and End If steps. Click the Duplicate button twice (Figure 8.65).
34 .
Double-click the second of the three identical If steps. When the Specify Calculation dialog box appears, highlight "gLastName" after IsEmpty and double-click to replace it with gCity.
35 .
In the script-assembly list, double-click the second of the three Set Field steps. When the Specify Field dialog box appears, double-click the field City.
36 .In the Script Step Options section, click the Calculated result: Specify button. When the Specify Calculation dialog box appears, change the field from gLastName to gCity. Click OK.
37 .
Repeat steps 3436, changing the fields in the last If and Set Field steps to gState and State (Figure 8.66).
38 .
To make sure that the next script steps are inserted after the If statement, click to select the last End If in the script-assembly list.
39 .In the step window on the left, scroll to the Found Sets category and double-click Perform Find (Figure 8.67).
Now we'll add another If section to alert the user if there are no records found. Otherwise, if no records are found, the user gets dumped into the generic No Records Found dialog box, which would be confusing.
40 .In the step window on the left, double-click Set Error Capture.
41 .In the step window on the left, scroll up to the Control category and double-click If. In the Script Step Options section, click the Specify button.
42 .When the Specify Calculation dialog box appears, click the View drop-down menu and scroll down to choose "Get functions."
43 .
In the list of Get functions, double-click Get(LastError).
44 .
Click after the close parentheses and type =401 (Figure 8.68). Click OK. If no records match the Find criteria, Error 401 is returned.
45 .In the step window on the left, scroll down to the Miscellaneous category. Double-click Show Custom Dialog. In the Script Step Options section, click Specify.
46 .When the "Show Custom Dialog" Options dialog box appears, type a title for the dialog box in the Title input box. Add a message to be displayed when no records are found in the Message input box.
47 .
Delete "Cancel" from the Button 2 input box (Figure 8.69). Click OK to return to the Edit Script dialog box.
48 .
In the step window on the left, scroll up to the Found Sets category and double-click Show All Records.
If no records are found, this step will reset the database to display all records.
49 .
After the find is performed, we need to clear all three global fields. Click the last EndIf step in the script-assembly list. In the step window on the left, scroll up to the Fields category and double-click Set Field.
50 .In the Script Step Options section, click the "Specify target field" check box. When the Specify Field dialog box appears, double-click to choose gLastName.
51 .Click Calculated result: Specify button. When the Specify Calculation dialog box appears, click the quotes button in the Operators keypad (Figure 8.70). Click OK.
By putting nothing between the quotes, you erase whatever was in the global storage field.
52 .
Click the Set Field step you just created. Click Duplicate twice.
53 .Double-click the first of the new Set Field steps. When the Specify Field dialog box appears, change the field to gCity.
54 .Repeat step 52 to replace the last global field with gState (Figure 8.71). Click OK twice to finish.
When the script is run, the user will see a dialog box in which to enter the find requests (Figure 8.72). If the user clicks Do It, the script will perform the find. If no records are found, the second dialog box will tell the user so. At the end the script will clear the global fields so the data entered won't appear the next time the script is run.
Although they're terribly addictive, try to avoid the temptation of creating too many nested If statements. They become next to impossible to follow in the script editor, guaranteeing lots of wasted time if you have to troubleshoot them.
As shown earlier in this section, clearing a global field after you use it in a script is a good housekeeping step. If you create a database with many global fields, you might consider creating a separate table for them. Then you can create a single script to clear all your globals when necessary.
The first button in a Show Custom Dialog dialog box is always the default choice. If you press Enter instead of clicking a button when the message box is displayed, the Get(LastMessageChoice) function will react as if you chose the default button. Protect your users from unpleasant accidents by choosing the least destructive option as the default choice.