6.5. Working with Rules at the Control Level

While you can specify values as a default value for a control, there are times when you want a control to be updated to a value based on a different control. You can't do that with a default value, because at the time the default value is assigned there are no values, other than default values, in the other fields of the form. If you try to use another field in a default value assignment, you will receive an error. Instead of using the default value, you would specify a rule on the control you want to "trigger" the rule on.

6.5.1. What Are Rules and When Are They Used?

Rules are a combination of conditions and actions that you can use to manipulate values in other controls that are based on the control to which the rules are assigned.

You can have more than one rule per control, and you can specify that those rules either run all the way through or stop when a condition is met. You can also specify the order of the rules, and rearrange them as necessary.

Here are a few examples of utilizing rules:

  • Assign a default tax on an order based on the city the order was sold in.

  • Update a contact e-mail field on an order after a customer has been chosen.

  • Update a ship date field to the order date field plus a couple of days when the order date is specified.

  • In the current form a good example of a set of rules is assigning various discounts based on the quantity ordered.

We are going to use the last example mentioned to demonstrate creating rules in the next section.

6.5.2. Creating Rules

The way to assign rules is to open the property sheet for the control, in this case the Quantity text box, and click the Rules... command button. After doing so, you will see the Rules dialog box, as shown here in Figure 6-9, minus the two rules.

Figure 6.9. Figure 6-9

Two rules displayed will be used in this section: The first will be described in detail. The second rule will be created in the Try It Out section.

When you click the Modify command, the Rule dialog box opens, displaying the rule labeled Rule 1, as shown in Figure 6-10.

Figure 6.10. Figure 6-10

The first order of business to create a rule is to set the condition or conditions that you want the rule to perform the action, or actions, on. To accomplish this, you will click on the Set Condition button. The Condition dialog box then opens, and you can set a connection, as shown in Figure 6-11.

Figure 6.11. Figure 6-11

Note that what is displayed in the second and third drop-down lists is based on what you set the first drop-downlist to. They may also disappear altogether.

The first column contains the field or expression for the comparison, containing the following:

  • Fields in the Section: Each of the fields is listed. When you specify one of the fields, the list of operators is displayed for you to choose from, including the is greater than operator displayed in Figure 6-11. You can also choose to fill in the Type a Number text box (which changes with the data type), select a field or group of fields, or specify a formula.

  • The Expression: Enables you to type in an expression directly. Using this option, you can type in your own criteria as the condition. When referring to the current field you are creating the rule in, you can use the period (.). For example, for the condition displayed in Figure 6-10, you would type: . > 5.

  • User's Current Role: With this option, you can customize a form based on the current user, and his or her role. Roles are created in InfoPath and have nothing to do with Windows or SQL Server roles.

  • Select Set of Signable Data...: With digital signatures, you can secure fields, groups of fields, sections, or a whole form. When using this option, you can specify the name of the part of the form to enable digital signitures for, thus adding more security to your form. You can read more on digital signatures in Chapter 16, "Implementing Security."

  • Select a Field or Group...: Picking this option displays all the fields and tables in the data source of the form. Depending on what you pick here, the second drop-down list will display a list of operators. One thing that is different from when you choose a field using the first option, fields in the section, is that in the third drop-down list you can type various data types into the field, instead of the just the data type of the actual field.

Once an option is chosen, the rest of the condition is filled out, varying with the option chosen. When you select a field to use, the operator and value boxes are displayed for you to supply these elements. At this point, additional criteria can be added, creating what is called complex criteria using ANDs and OR. You can see the And button back in Figure 6-11.

Once the condition is completed, click the OK button, and the condition is added to the rule. After the condition is specified, you can assign the actions you want to have performed when the condition you created is met. To create an Action, you will click the Add Action command button. The action for Figure 6-10 can be seen in Figure 6-12.

Figure 6.12. Figure 6-12

The first task to perform when entering the Action dialog box is to assign the Action to be performed. There are a limited number of actions that can be performed, although you can add as many actions to the rule as needed. A list of possible actions appears when you click the Action drop-down list. That list is displayed in Figure 6-13.

Figure 6.13. Figure 6-13

The following list explains the actions that can be performed:

  • Show a Dialog Box Message: Displays a literal text string that you specify.

  • Show a Dialog Box Expression: Displays a message that you can create using a combination of literals and formulas, including functions.

  • Set a Field's Value: Assigns the value you specify to field that you also provide. This was the Action used in Figure 6-12.

  • Query Using a Data Connection: Queries the data connection you assign it to, refreshing the data in a form or section.

  • Submit Using a Data Connection: Submits data back to the database using a data connection. This is necessary when you need to update the database when the information has been partially entered.

  • Open a New Form to Fill Out: Opens a form with a blank record.

As with the Condition dialog box, the fields after the Action field will vary depending on the action chosen. After filling in the other field or fields in the Action dialog box, click OK to save the action to the list of actions for the rule. At this point, you can add another action to the rule or accept the rule by clicking OK.

6.5.2.1. Try It Out: Adding a Second Rule

The time has finally come to add your own rule to the list of rules for Quantity. While the first rule establishes that any customer purchasing a quantity greater than 5 receives a 10 percent discount, you want those customers who purchase between 2 and 5 to receive a 5 percent (or .05) discount. To accomplish this, another rule has to be created.

  1. Right-click the Quantity field, and pick TextBox Properties.... The property sheet for the quantity text box opens.

  2. Click the Rules button. The Rules dialog box is displayed.

  3. Click Add... in the Rules dialog box. A new rule is opened in the Rule dialog box.

  4. Click the Set Condition button. The Condition dialog box opens.

  5. For the first part of the condition, type Quantity, is greater than, and 1 in the three drop-downs displayed.

  6. Click on the And button. A new row will appear in the Condition dialog box.

  7. For the next criteria type Quantity, is less than or equal to, and 5 in the three drop-down lists. The Condition now looks as it does in Figure 6-14.

    Figure 6.14. Figure 6-14
  8. Click Ok to accept the condition.

  9. Click on Add Action. The Action dialog box opens with a blank action.

  10. Choose Set a field's value for the action. The Field and Value fields are displayed.

  11. Type @Discount for the field, or use the Field locator button, displayed next the Field field.

  12. Type .05 for the Value:. The Action dialog box now looks like Figure 6-15.

    Figure 6.15. Figure 6-15
  13. Click the OK button to accept the action. The Action dialog box closes, and you are back in the Rules dialog box.

  14. Click OK twice to close the Rules dialog box and then the property sheet.

  15. Click Preview Form to open the form in preview mode.

  16. Query the form using BC, then modify the Quantity field to test the rules. Enter in various numbers such as 0, 1, numbers up to 5, and numbers over 5, then view the results. You should see results simular to those shown in Figure 6-16.

    Figure 6.16. Figure 6-16

As you have just seen, by using the rules as they are, you can accomplish quite a bit in creating a full-featured form that will react as you need it to when users enter data into the various fields. You have control over what happens. In other words you make the rules!

Another way of controlling how your form looks is by setting the formatting of the fields on the form.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.171.137