Sunday, April 7, 2013

Retrieve a row of a repeating table

Problem: You have a repeating table on an InfoPath form and want to be able to retrieve the values of fields that are located in one specific row of the repeating table.

Solution:

You can retrieve rows of a repeating table either by their position within the repeating table or by the value of a specific field within the repeating table.

1 - Create a new InfoPath form

2  - Add a Repeating Table with 2 columns ("field1" & "field2")

3 - Add a Text Box control to the view and name it contents

4 - Add a second Text Box control to the view and name it toSelect

5 - Add a Button control and add the following code to the Clicked event handler:

A) If you want by position:


Public Sub CTRL14_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            If String.IsNullOrEmpty(toSelect) Then
                toSelect = "1"
            End If

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[" & toSelect & "]", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

            mainDS.SelectSingleNode("/my:myFields/my:Contents", NamespaceManager).SetValue(sb.ToString())


        End Sub

B) If you want by field value:

        Public Sub CTRL15_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[my:field1 = '" & toSelect & "']", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

            mainDS.SelectSingleNode("/my:myFields/my:Contents", NamespaceManager).SetValue(sb.ToString())

        End Sub


6 - Save and preview the form

If the form retrieves rows by position, enter a number in the toSelect text box and click on the button.

If the form retrieves rows by field value then enter a piece of the text that is the same as the value of one of the field1 fields in any row of the repeating table.

No comments:

Post a Comment

About Me

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