Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to create a required string field for an InfoPath data source


View products that this article applies to.

Summary

This step-by-step article describes how to create required (that is, "cannot be blank") string fields in your InfoPath form.

Introduction

If you create your own data source in InfoPath, you can designate a string field as a required field (that is, "cannot be blank") by modifying the field's properties in the Data Source task pane. However, if you instead use an external schema for your form's data source, you can designate a string field as a required field in one of two ways:
  • You can create a simpleType element for the required field's type in the schema. This simpleType element contains a minLength restriction that is equal to or greater than 1.

    -or-
  • If you cannot modify the schema, you can create a data validation rule in your InfoPath form.
The steps in this article describe each of these techniques that you can use to create a required string field on your form.

Designate a Required String Field in a Data Source That Is Built with InfoPath

InfoPath 2007

  1. In InfoPath, design a new blank form.
  2. In the Data source task pane, right-click the myFields group, and then click Add. The Add Field or Group dialog box appears.
  3. In the Name box, type ID. Click to select the Cannot be blank check box, and then click OK. Notice that the ID field appears in the Data source task pane with a red asterisk to indicate that it is a required field.
  4. Drag the ID field from the Data source task pane to your form.
  5. On the File menu, point to Preview, and then click Form.
  6. With the mouse pointer, pause on ID. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.

InfoPath 2003

  1. In InfoPath, design a new blank form.
  2. In the Data Source task pane, right-click the myFields group, and then click Add. The Add Field or Group dialog box appears.
  3. In the Field Name box, type ID. Under Validation, click to select the Cannot be blank check box, and then click OK. Notice that the ID field appears in the Data Source task pane with a red asterisk to indicate that it is a required field.
  4. Drag the ID field from the Data Source task pane to your form.
  5. On the File menu, point to Preview Form, and then click Default.
  6. With the mouse pointer, pause on ID. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.

Designate a Required String Field in Your External Schema

Create a custom schema file

In a text editor such as Notepad or in an XML editor, create the following schema. Then, save the schema as Customschema.xsd.
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified"
	xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
	xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Employee">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="FirstName" type="myReqString" />
				<xs:element name="LastName" type="xs:string" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:simpleType name="myReqString">
		<xs:restriction base="xs:string">
			<xs:minLength value="1" />
		</xs:restriction>
	</xs:simpleType>
</xs:schema>
Notice that the FirstName field is typed with a simpleType element that is named "myReqString," and "myReqString" has a restriction that the length must be at least one character.

Use the custom schema file

InfoPath 2007
  1. Start InfoPath.
  2. Design a new form that uses Customschema.xsd for the primary data source. To do this, follow these steps:
    1. On the File menu, click Design a Form Template.
    2. In the Design a Form Template task pane, click XML or Schema, and then click OK. The Data Source Wizard appears.
    3. Click Browse, select the Customschema.xsd file in the Open dialog box, click Open, and then click Next.
    4. Click No, and then click Finish to complete the wizard.
  3. Notice the fields in the Data source task pane. Notice that the FirstName field is marked with a red asterisk to indicate that the field cannot be blank. LastName is not marked with an asterisk. Therefore, it is not a required field.
  4. Drag the Employee group from the Data source task pane to your form, and then click Section with Controls.
  5. Test your form. To do this, follow these steps:
    1. On the File menu, point to Preview , and then click Form. The form opens in preview mode.
    2. Notice that only the First Name text box is displayed with a red asterisk. This red asterisk indicates that the field is required and cannot be blank.
    3. With the mouse pointer, pause on First Name. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.
InfoPath 2003
  1. Start InfoPath.
  2. Design a new form that uses Customschema.xsd for the primary data source. To do this, follow these steps:
    1. On the File menu, click Design a Form.
    2. In the Design a Form task pane, click New from Data Source. The Data Source Setup Wizard appears.
    3. Select XML Schema or XML data file for the data source type, and then click Next.
    4. Click Browse, select Customschema.xsd in the Open dialog box, and then click OK. Click Finish to complete the wizard.
  3. Notice the fields in the Data Source task pane. Notice that the FirstName field is marked with a red asterisk to indicate that the field cannot be blank. Because LastName is not marked with an asterisk, it is not a required field.
  4. Drag the Employee group from the Data Source task pane to your form, and then click Section with Controls.
  5. Test your form. To do this, follow these steps:
    1. On the File menu, point to Preview Form, and then click Default. The form opens in preview mode.
    2. Notice that only the First Name text box is underscored with a red line. This red line indicates that the field is required and cannot be blank.
    3. With the mouse pointer, pause on First Name. Notice the screen tip "Cannot be blank." This is the default screen tip for fields that are marked as required in the schema.

Use an InfoPath Data Validation Rule to Create a Required String Field

Outlook 2007 or Outlook 2003

  1. Right-click the LastName field in the Data Source task pane, and then click Properties. The Field or Group Properties dialog box appears. Notice that the Cannot be blank option is disabled; this property is always disabled in the InfoPath user interface when your data source is an external data source. Click Cancel to dismiss the dialog box.
  2. Right-click the text box for the LastName field, and then click Text Box Properties.
  3. In the Text Box Properties dialog box, click Data Validation. The Data Validation dialog box appears.
  4. Click Add.
  5. Select is blank in the second condition list, type Last Name is a required field in the ScreenTip text box, and then click OK.
  6. Click OK to close the Data Validation dialog box, and then click OK to close the Text Box Properties dialog box.
  7. Test your form:
    1. Preview the form.
    2. Notice that the Last Name text boxis now displayed with a red asterisk or underscored with a red line. This red asterisk or red line indicates that the field is required and cannot be blank.
    3. With the mouse pointer, pause on Last Name. Notice the custom screen tip "Last Name is a required field." This is the custom screen tip that you typed for the data validation rule.

↑ Back to the top


Keywords: KB827004, kbschema, kbxml

↑ Back to the top

Article Info
Article ID : 827004
Revision : 1
Created on : 10/4/2011
Published on : 10/4/2011
Exists online : False
Views : 437