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.

ADVERTISEMENTS

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:

  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.

 
 Subscribe for updates via RSS or Email

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. The 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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services