You can use expression boxes to display the row number in a repeating table, as well as perform other calculations. When you create formulas in an expression box, you refer to the fields and groups to which controls on the form are bound, rather than the controls themselves. The formulas are created using XPath, but you can follow these steps without knowing XPath.
- In design mode, place the insertion point where you want to insert the expression box.
NOTE The expression box must be part of the repeating table.
- If the Controls task pane is not visible, click More Controls on the Insertmenu.
- In the Controls task pane, click Expression Box.
- In the Insert Expression Box dialog box, type position() in the XPath box.
- To test the formula, click Preview Form on the Standard toolbar.
Autonumbering fields in a repeating table in InfoPath 2007 using VB.NET code
Note that you are not required to write code if you want to create sequential numbers in a repeating table, and that you could also use a formula to autonumber a repeating table in InfoPath.
Here are the steps:
- Follow the instructions in Programmatically execute code when a repeating table row is inserted or deleted to add a Changed event handler for the row of a repeating table.
- Add the following code to the Changed event:
Dim nav As XPathNavigator = MainDataSource.CreateNavigator()
Dim iter As XPathNodeIterator = nav.Select("//my:field1", NamespaceManager) Dim i As Integer = 1 While iter.MoveNext() iter.Current.SetValue(i.ToString()) i = i + 1 End While
where my:field1 is a field in the repeating table.
The final code would look something like:
Public Sub group2_Changed(ByVal sender As Object, ByVal e As XmlEventArgs)
Dim nav As XPathNavigator = MainDataSource.CreateNavigator()
Dim iter As XPathNodeIterator = nav.Select("//my:field1", NamespaceManager)
Dim i As Integer = 1
While iter.MoveNext()
iter.Current.SetValue(i.ToString())
i = i + 1
End While
End Sub
Now whenever you insert a row into the repeating table or delete a row from the repeating table, the my:field1 fields in all of the rows of the repeating table are automatically renumbered
Example: In InfoPath, you have a repeating table with 3 columns. You stored the row numbers in the first field (my:field1) of the repeating table. When you change the value of the second field (my:field2) of the repeating table and then move away from that field, you want to determine the number of the row in which you just made a change.
You can write the following code for the Changed event handler ofmy:field2 to determine the row number as follows:
Public Sub field2_Changed(ByVal sender As Object, ByVal e As XmlEventArgs)
Dim site As XPathNavigator = e.Site
site.MoveToParent()
Dim rowNumber As String = site.SelectSingleNode("my:field1", NamespaceManager).Value
End Sub