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



The information in this article applies to:

  • Microsoft Office InfoPath 2003
  • Microsoft Office InfoPath 2003, Service Pack 1 (SP1)

SUMMARY

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

back to the top

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.

back to the top

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

  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.
back to the top

Designate a Required String Field in Your External Schema

  1. In a text editor such as Notepad or an XML editor, create the following schema, and 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.
  2. Start InfoPath.
  3. Design a new form that uses Customschema.xsd for the primary data source:
    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.
  4. 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.
  5. Drag the Employee group from the Data Source task pane to your form, and then click Section with Controls.
  6. Test your form:
    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.
back to the top

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

  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. On the File menu, point to Preview Form, and then click Default. The form opens in preview mode.
    2. Notice that the Last Name text box is now 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 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

Modification Type:MinorLast Reviewed:8/19/2004
Keywords:kbXML kbSchema KB827004 kbAudDeveloper