Monday, October 8, 2012

Setting fields to their original values

Problem: You want to restore an InfoPath form to the state it had when it was opened for the very first time.

You can save the state of a form when it initially opens and then restore to this state by replacing all the values on the fields with the data you initially stored.

The method to do this is a little different between an InfoPath Filler or browser form.

1 - Design a form and add as many fields as you like.
2 - Add a button to the form and label it RESET
3 - Add the following code to the FormCode class

For InfoPath Filler:

Private initialData As String

For Browser Forms

Private Property initialData() As Object

            Get
                Return FormState("initialData")
            End Get
            Set(ByVal value As Object)
                FormState("initialData") = value
            End Set
 End Property

4 - Add the following code to the FormEvents_Loading() event handler

initialData = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields", NamespaceManager).InnerXml

5 - Add the following code to the Clicked() event handler of the button

MainDataSource.CreateNavigator.SelectSingleNode("/my:myFields", NamespaceManager).InnerXml = initialData.ToString

Preview the form and change the default values of the fields. Notice that once you click the Reset button the value goes back to their original values.

The innerXML property of an XPathNavigator object gets or sets the markup that represents the child nodes of the current node. By using this property you can store the entire XML contents of the myFields group node in the variable or property.

1 comment:

  1. Hi Alberto.

    Good to see you writing a great post. I wish this blog can help many people around the world.

    Good Luck.

    From your friend in Brazil

    Renato

    ReplyDelete

About Me

My photo
Toronto, Ontario, Canada
MBA, M.Sc. & MCP