Autonumbering fields in a repeating table in InfoPath 2007 using VB.NET code

by S.Y.M. Wong-A-Ton

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.

Here are the steps:

  1. 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.
  2. 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.

Related InfoPath Articles:

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.

Working with InfoPath