repeating table calculations - InfoPath Dev
in

InfoPath Dev

repeating table calculations

Last post 10-12-2005 04:39 PM by Greg Collins. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 10-10-2005 02:58 PM

    repeating table calculations

    I am trying to calculate subtotals for each row in the repeating table but I can get it to work only for the first row! I need to do calculations using managed code because there is some looping etc. involved. Here is a simplified example of my code (in VB.Net):

     
    Public Sub field1_OnAfterChange(ByVal e As DataDOMEvent)

    Dim field1 As Double = _
    CDbl(e.Source.selectSingleNode("/my:myFields/my:group1/my:group2/my:field1").text)

    Dim field2 As Double = _
    CDbl(e.Source.selectSingleNode("/my:myFields/my:group1/my:group2/my:field2").text)

    Dim nodeField3 As IXMLDOMNode = _
    e.Source.selectSingleNode("/my:myFields/my:group1/my:group2/my:field3")

    Dim field3 As Double

    field3 = SubtotalCalc (field1, field2)

    nodeField3.text = CStr(field3)

    End Sub


    Following Greg's advise a while ago (http://www.infopathdev.com/forums/topic.asp?TOPIC_ID=171&SearchTerms=relative,paths) I tried using relative paths instead of absolute paths. I changed my paths to ("../fieldName") but then function SubtotalCalc is never executed - not even for the first row:( What am I doing wrong?

    Also, I always get following warning when I build my solution in VS:

    "A first chance exception of type 'System.NullReferenceException' occurred in InfoPathProject1.dll".

    It doesn't seem to affect the form when it is opened in InfoPath though - no error messages are displayed and form works just fine.
    I tried to inlcude code in If ... Then statement to make sure the nodes can be found in XDocument and used Try ... Catch block to handle exeptions - no change.

    I would appreciate if somebody could help me, please!!! I am new to InfoPath...
  • 10-10-2005 06:40 PM In reply to

    Re: repeating table calculations

    Hi,

    Have you just tried: "my:fieldX" inside your SelectSingleNode?

    Regards,

    Franck Dauché
  • 10-11-2005 07:46 AM In reply to

    Re: repeating table calculations

    Yes, I have tried that and many other possible ways of referencing table fields. Only "//my:fieldX" works for the first row at least nothing else does.

    Thanks for any suggestions!



    quote:
    Originally posted by franck

    Hi,

    Have you just tried: "my:fieldX" inside your SelectSingleNode?

    Regards,

    Franck Dauché

  • 10-11-2005 10:02 AM In reply to

    Re: repeating table calculations

    You are using absolute paths in your code. That is why you are always getting the first instance of the nodes. Your OnAfterChange event is already pointing to the correct field1... you just need to get the sibling field2 and field3... not go all the way back to the top of the tree...

    There may be other issues too, but try the following changes:



    Public Sub field1_OnAfterChange(ByVal e As DataDOMEvent)

    Dim field1 As Double = _
    CDbl(e.Source.text)

    Dim field2 As Double = _
    CDbl(e.Source.selectSingleNode("../my:field2").text)

    Dim nodeField3 As IXMLDOMNode = _
    e.Source.selectSingleNode("../my:field3")

    Dim field3 As Double

    field3 = SubtotalCalc (field1, field2)

    nodeField3.text = CStr(field3)

    End Sub


    Greg Collins [InfoPath MVP]
    InfoPath Dev
  • 10-11-2005 10:34 AM In reply to

    Re: repeating table calculations

    Thank you Greg but nope, that does not work at all, not even for the first row. When I made your suggested changes I got an error "Object reference not set to an instance of an object" and a warning in VS I had mentioned before.
    Anyway, what do you think about this: http://www.infopathdev.com/forums/topic.asp?TOPIC_ID=962


    quote:
    Originally posted by Greg Collins

    You are using absolute paths in your code. That is why you are always getting the first instance of the nodes. Your OnAfterChange event is already pointing to the correct field1... you just need to get the sibling field2 and field3... not go all the way back to the top of the tree...

    There may be other issues too, but try the following changes:



    Public Sub field1_OnAfterChange(ByVal e As DataDOMEvent)

    Dim field1 As Double = _
    CDbl(e.Source.text)

    Dim field2 As Double = _
    CDbl(e.Source.selectSingleNode("../my:field2").text)

    Dim nodeField3 As IXMLDOMNode = _
    e.Source.selectSingleNode("../my:field3")

    Dim field3 As Double

    field3 = SubtotalCalc (field1, field2)

    nodeField3.text = CStr(field3)

    End Sub


    Greg Collins [InfoPath MVP]
    InfoPath Dev

  • 10-12-2005 04:39 PM In reply to

    Re: repeating table calculations

    I'm not a VB person so I might have some issues with my code changes... but the concept I was trying to put across is that when you are working with repeating tables/sections you need to get fields relative to the one in the current row... not go all the way back to the top and work down -- that just brings you back to the first item.

    - - - - -

    Creating a row ID might be interesting if you have reasons for one... but many schemas are fixed and you can't add one. The best thing is to work relative to the item that cuased the OnAfterChange event.

    The other thing that you will probably want to consider is that OnAfterChange receives 2 events -- one for Delete and one for Insert. You want to filter out the Delete events by returning from the handler if e.Operation is not equal to "Insert".

    Greg Collins [InfoPath MVP]
    InfoPath Dev
Page 1 of 1 (6 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.