Display a message box if an item does not exist in the list of entries for a combo box

Use a rule to display a message box when an item entered into a combo box cannot be found in a list of valid entries.

ADVERTISEMENTS

Problem

You want to display a message box to alert a user whenever an invalid item (an item that does not exist in a list of valid entries) is entered into a combo box on an InfoPath form.

Solution

Use a Rule to display a message box when an item entered into a combo box cannot be found in a list of valid entries.

Discussion

To be able to check whether an item is present in a list of known entries you must bind the combo box to either a repeating node in the form's main data source or to an external data source. We will bind the combo box to a repeating node in the form's main data source.

You can accomplish this functionality as follows:

  1. Design a new InfoPath form template.
  2. Add a Combo Box control to the form and name it comboboxvalue.
  3. Add groups and fields to the Main data source so that the main data source of the form resembles the following figure. Note that the entry node is a repeating group node.


    Figure 1. The Main data source of the form template.
  4. On the Tools menu, click Default Values.
  5. In the Edit Default Values dialog box, expand the entries node, right-click on the first entry node, and choose Add another entry below from the context menu that appears. While you add each entry, enter a Default value for the value and displayname node under each entry node.



    Click OK when you're done adding the entries.
  6. Double-click on the Combo Box control to open its Properties dialog box.
  7. Choose Look up values in the form's data source in the List box entries section on the Data tab of the Combo Box Properties dialog box.
  8. Bind the Combo Box to the entry node as shown in the following figure:

  9. On the Combo Box Properties dialog box, click the Rules button.
  10. On the Rules dialog box, click Add.
  11. On the Rule dialog box, click Add Action and add an action that says:
    Show dialog box message: not present
    and a second action that says:
    Set a field's value: . = ""
    The first action displays a message box with the text "not present" and the second action sets the value of the Combo Box to an empty string.

  12. On the Rule dialog box, click Set Condition and add two conditions that say:
    The expression: count(../my:entries/my:entry/my:value[. = ../../../my:comboboxvalue]) = 0
    and
    comboboxvalue: is not blank
    The first condition counts the amount of entries that have the same value as the value of the combo box. If the amount of entries found is zero, the item is an invalid entry. The second condition ensures that the rule only runs when an item has been entered into the combo box. Without the second condition, the message box would be shown twice, since we empty the combo box after displaying the message box (see step 11).

  13. Close all open dialog boxes by clicking OK.

You should now have a fully functional form so that when an invalid entry is typed into the combo box a message box will appear.

Note: This solution also works for a Drop-Down List Box (you can set the value of a drop-down list box by using rules) and in InfoPath 2003. This solution does not work for browser-compatible InfoPath forms.

Tip:

If writing XPath expressions is still a challenge to you, here's a tip on how to easily find the correct expression for the The expression condition on the Condition dialog box:

  1. Double-click on the Combo Box control to open its Properties dialog box.
  2. On the Combo Box Properties dialog box, click the Rules button.
  3. On the Rules dialog box, click Add.
  4. On the Rule dialog box, click Add Action.
  5. On the Action dialog box, select Set a field's value from the first drop-down list box.
  6. On the Action dialog box, click on the formula button behind the Value field.
  7. On the Insert Formula dialog box, click Insert Field or Group.
  8. On the Select a Field or Group dialog box, select the value node, and then click on Filter Data.

  9. On the Filter Data dialog box, click Add.
  10. On the Specify Filter Conditions dialog box, select the settings as in the following figure:

  11. Click OK to close the Specify Filter Conditions dialog box.
  12. Click OK to close the Filter Data dialog box.
  13. Click OK to close the Select a Field or Group dialog box.
  14. On the Insert Formula dialog box, check the Edit XPath (advanced) check box.

  15. Copy the formula that InfoPath created for you from the Formula text box:
    ../my:entries/my:entry/my:value[. = ../../../my:comboboxvalue]
  16. Change the expression into
    count(../my:entries/my:entry/my:value[. = ../../../my:comboboxvalue]) = 0
    and you have the expression to be used as The expression on the Condition dialog box.
 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services