Autonumbering fields in a repeating table in InfoPath 2007 using VB.NET code
This article shows an example of using the Changed event of the row of a repeating table to programmatically autonumber a field in the repeating table using VB.NET code.
In Programmatically execute code when a repeating table row is inserted or deleted I showed you how you can add an event handler to a repeating table row so that you can execute code when a repeating table row is inserted or deleted.
In this article, I'll give you an example of using the Changed event of the row of a repeating table to programmatically autonumber a field in the repeating table 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 Whilewhere 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.
Related InfoPath Articles:
- Auto-numbering InfoPath forms when they are submitted to a SharePoint form library
- Counting the amount of documents submitted today to a SharePoint library
- Programmatically execute code when a repeating table row is inserted or deleted
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.



