How to programmatically change the default view of an InfoPath 2003 form (826997)



The information in this article applies to:

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

SUMMARY

Microsoft Office InfoPath forms support multiple views. One view is the default view. When you open a form in InfoPath, you open the form in the default view. This article describes how to programmatically change the default view of an InfoPath form.

back to the top

Create a New Form

  1. Start InfoPath 2003.
  2. On the File menu, click Design a Form.
  3. On the Design a Form task pane, click New Blank Form.
back to the top

Create View 1

  1. On the blank form, type This is View 1 on the first line, and then type Check to display View 2 when the form is opened on the second line.
  2. On the Task Pane drop-down menu, click Controls.
  3. In the Controls task pane, click Check Box.
  4. Change the Check Box text to View 2.
back to the top

Create View 2

  1. On the Task Pane drop-down menu, click Views.

    View 1 is the default view for the form.
  2. In Actions section of the task pane, click Add a New View.
  3. Type the name View 2, and then click OK.
  4. On the blank form, type This is View 2.
back to the top

Add the Script

  1. On the Tools menu, point to Script, and then click Microsoft Script Editor.
  2. Add the following code to the Code window:
    function XDocument::OnLoad(eventObj)
    {
    	//Pick the default view based on the value of element DefaultView
    	var objDefaultView = XDocument.DOM.selectSingleNode("//my:field1");
    
    	if ((objDefaultView != null) && (objDefaultView.text != ""))
       	{
    		switch (objDefaultView.text)
          		{
             		case "false":
    				    XDocument.ViewInfos("View 1").IsDefault = true;
                			break;
             		case "true":
                			XDocument.ViewInfos("View 2").IsDefault = true;
                			break;
          		}
       	}
    }
  3. Save the script, and then close the Microsoft Script Editor.
  4. Save your form template as SwitchDefaultView.xsn, and then close the form template.
back to the top

Try It Out

  1. On the File menu, click Fill Out a Form.

    Note Make sure that you fill out the form in this step instead of previewing the form. Preview does not reflect any programmatic change of the default view in the OnLoad event. Preview mode always shows the view that is currently selected in the InfoPath Designer.
  2. In the task pane, click SwitchDefaultView.

    The form opens in View 1 (the initial default view).
  3. Click the View 2 check box.
  4. Save the form as SwitchDefaultViewForm.xml, and then close the form.
  5. On the File menu, click Open.
  6. Locate SwitchDefaultViewForm.xml, and then click Open.

    The form opens in View 2.
back to the top

Modification Type:MinorLast Reviewed:8/30/2004
Keywords:kbhowto KB826997 kbAudDeveloper kbAudITPRO