My next discovery with Infopath was to make a sommation of other fields. Although this is very simple in other programming languages, in Infopath it isn't. Why ? Well, as you may know, Info uses XML to hold houw your form is build. So when wanting a sommation you first have to get the value out of the XML for evey filed and then put the total value in your total field XML tag.
Now, first you have to decide how to trigger the function. I have triggered it, by the "after change" event of every field I want to include in the total.
You can program the event. This can be done by selecting the properties of the field, and then choosing "datavlidation". When this screen popups up, you can select the event and press edit. Now a script editor will be opened. You can choose between Javascript and VB script.

When the VB script editor is opened Infopath has created following code for you :
///=======
// The following function handler is created by Microsoft Office InfoPath.
// Do not modify the name of the function, or the name and number of arguments.
// This function is associated with the following field or group (XPath): /my:myVerlofSheet/my:myTotalVerlof/my:TotaalVerlof
// Note: Information in this comment is not updated after the function handler is created.
//=======
function msoxd_my_TotaalVerlof::OnAfterChange(eventObj)
{
// Write code here to restore the global state.
if (eventObj.IsUndoRedo)
{
// An undo or redo operation has occurred and the DOM is read-only.
return;
}
// A field change has occurred and the DOM is writable. Write code here to respond to the changes.
}
Most of this code you don't have to change. The part in red can be changed by your code. In my case I call a function "CalcVerlofTotal". This function will do my sommation. No when going into the details of this function. First I had to find out how the get the value of the fields. So I needed an XLMDom document, so I can use the selectSingleNode. But how was I going to define which node I want to select; Well now I came to my previous part : Infopath - datasources [Put link]. As you can remember I had several groups. Well to go to the correct node, you have to do two things :
- First find this line : "XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-02-10T19:53:49"');" Here you can see the red word (could be different). this is important.
- Next you have to now you exact datasource structure.
So when we want to go to the Verlof2003 field this is our node :
/my:myVerlofSheet/my:myTotalVerlof/my:Verlof2003
As you can see, the keyword my is important. And we take every level of our datasource to go to the exact field.
Now it's easy. So I created for every field an object. The next step is to create the summation. So I created a variable nTotaal, which will store the result for me. Don't make my mistake, because first I just took the nodevalue of every object : "myVerlof2003.nodeTypedValue" and add here the value of my next object. it will work, but's wrong. You get following result :

As you can see, there's is no summation done, but the systems concatenates the values as strings. So after some searching I found out, that you have to do some typecasting. So, to cast the value to integers, just use the function number. this gives us following : "Number(myVerlof2003.nodeTypedValue)". And when adding all the values now to each other, we got our correct result.

to complete this part here's the full code of my function :
/*------------------------------------------------------------------------------
CalcverlofTotal
Calculates the total of all holidays an employee can have
------------------------------------------------------------------------------*/
function CalcVerlofTotal()
{
var xmlDom = XDocument.DOM
var myVerlof2003 = xmlDom.selectSingleNode("/my:myVerlofSheet/my:myTotalVerlof/my:Verlof2003");
var myVerlof = xmlDom.selectSingleNode("/my:myVerlofSheet/my:myTotalVerlof/my:Verlof");
var myADV = xmlDom.selectSingleNode("/my:myVerlofSheet/my:myTotalVerlof/my:ADV");
var myVervangendeFeestdagen = xmlDom.selectSingleNode("/my:myVerlofSheet/my:myTotalVerlof/my:VervangendeFeestdagen");
var myTotaalVerlof = xmlDom.selectSingleNode("/my:myVerlofSheet/my:myTotalVerlof/my:TotaalVerlof");
var nTotaalVerlof = 0
nTotaalVerlof = Number(myVerlof2003.nodeTypedValue) + Number(myVerlof.nodeTypedValue) + Number(myADV.nodeTypedValue) + Number(myVervangendeFeestdagen.nodeTypedValue);
myTotaalVerlof.nodeTypedValue = nTotaalVerlof;
}
Now, I had my function, the next step will not be the repeated, but I will install the Toolkit for Visual Studio.Net and try this. So keep in touch for my experience with this.