Showing posts with label InfoPath. Show all posts
Showing posts with label InfoPath. Show all posts

Friday, May 1, 2009

Picking a single node from an eventObj

While we have already done code for a walkthru of all of the notes, after a column of buttons to our Daily Activity Worksheet Form it is important for us to be able to select values associated with that row, and only that row.

The object orientation of the JSCript code makes that a snap.

The button that was pressed, in particular, is the eventObj passed to the event handler. The "Source" Property is a reference to the other items in the row.

If we reference the form as seen, then the particular participant bound to the button in the row pressed is referenced as eventObj.Source.selectSingleNode("my:Participant").text



One important item of note, while doing the walkthru we used the path starting at the root... i.e. "//my:Participant". Since the button is already below the parent you need to reference it as "my:Participant" or at best, you'll always get the first child node.

Friday, January 30, 2009

Forms within Forms

An interesting InfoPath request; Quality has a project where I would like to use an InfoPath form for intakes. The idea is that the Intake form would be a form of forms where the user would launch each of the forms required for enrollment from a single master form. This form should be able to launch a blank copy of each of the enrollment forms, and this is the kicker, that follow “Paul’s Golden Rule” and avoid requiring any duplicate typing.

Launching a form is a point and click exercise, doing so while exposing the other forms fields requires a bit of code, but as it turns out, not much.

var newForm = Application.XDocuments.NewFromSolution(http://sharepoint/sites/it/area54/Sample Face Sheets/Forms/template.xsn);

var participant = newForm.DOM.selectSingleNode("my:FaceSheet/my:Client_Name");

myField = XDocument.DOM.selectSingleNode("my:myFields/my:Test");

participant.text = myField.text;

The first line launches the form, you’ll need the path to the form template, but that’s it. Your newForm variable then contains XDocument for the form just launched, in this case the face sheet. Now you can grab a field value from the parent form and copy it into the new form.

Monday, January 26, 2009

InfoPath Date Math Shuffle

Late Friday I got asked one of those seemingly simple questions whose simple answer is, as it turned out, missing. All my user wanted to do was have the difference between two dates calculate in her Infopath 2003 form. Naturally it’s a piece of cake in 2007… sigh.

But it can be done in 2003 with a fairly short VB script. Problem was I started out trying to write it as a Jscript and kept running into a lack of functionality… So anyway here is how to do the InfoPath 2003 Date Math Shuffle in all it’s glory.

Step One: Set the form to use VB Script in place of Jscript. This can only be done at the start, before you write any code for the form. Select Tools, then form options. This will open the form options dialog box, allowing you to select the Advanced Tab, where you can set the programming language to Visual Basic Script (VBScript as shown above).

To test it I added two date fields and a text field to a blank form.



Once there we can open our editor so we can add the code by grabbing the properties of the second date picker, select the onAfterChange event and click on the Edit button.



This will launch the Microsoft script editor for the proper event.

Of course after we’re done we should go back and add a call to the first date to the second date’s event in case the form’s user changes the first date after the second… heh.

But for now let’s just add the code. Thank goodness that the VB editor seems to have all the functionality that the Jscript lacked. At one point I was even considering a web-service call, but as it turns out that this VB script can handle this in a flash and without any of the nasty delays inherent in a web-service.

So, Step two is to write the code for the event. First we need to leave the premade code alone, it's only there in the event an "If eventObj.IsUnDoReDo" argument is present, and we don't care.

Below is the code, with further descriptions commented in the event you want to cut and paste it in as is...

‘==========
‘ date_from follows the XDocument path to get access to the forms SecondDate Field
‘ date_to is the same for the first date field
‘ dt1Val is a variable where we will load the value of the date_from field
‘dt2Val is a variable where we will load the value of the date_to field
‘daysElapsed is a variable where the difference is calculated
‘==========

Dim date_from
Dim date_to
Dim dt1Val
Dim dt2Val
Dim DaysElapsed

‘=====
‘ Now we set the date_from and date_to so we can read the fields
‘=====

Set date_from = XDocument.DOM.selectSingleNode("/my:myFields/my:SecondDate")
Set date_to = XDocument.DOM.selectSingleNode("/my:myFields/my:FirstDate")

‘======
‘ Then we test the fields to see if we have dates in both fields, if so then we calculate the difference and put

‘ it into the display field on the form, otherwise we make the display field blank
‘======
If (date_from.Text <> "") and (date_to.Text <> "") Then
dt1Val = CDate(date_from.Text)
dt2Val = CDate(date_to.Text)
daysElapsed = DateDiff("d",dt2Val,dt1Val)
XDocument.DOM.selectSingleNode("/my:myFields/my:Difference").Text = daysElapsed
else
XDocument.DOM.selectSingleNode("/my:myFields/my:Difference").Text = ""
End If

All that is left is to call this function when the first date changes by creating it’s event just as you did the second date fields and copying the function call, in this case:

msoxd_my_SecondDate_OnAfterChange(eventObj)

Not too shabby, but having Microsoft add the DateDiff function to InfoPath, as I believe they did in 2007 would have saved my people a lot of trouble.