Thanks I have found the post you did, I will try this tomorrow
and I am sure you won't get flamed
Thanks for the work I hope it works
Robbie
I am sure I'll get "flamed" for this <G> but for date calculations, I
much
prefer VBScript. So here are sample steps and code so you can first
test to
see how this works:
- Create a new, blank InfoPath form
- Add 2 Date Picker controls named: date1 and date2
- Add a text box control named: txtElapsedDays
- From the Tools menu, choose Form Options, select the Advanced tab
and
make sure the "Form Code Language" is set to: VBScript
- Click OK
- Right-click on date1, choose Properties, click the Data Validation
button, select OnAfterChange from the Events box and click Edit
- You should see the following:
Sub msoxd_my_date1_OnAfterChange(eventObj)
' Write code here to restore the global state.
If eventObj.IsUndoRedo Then
' An undo or redo operation has occurred and the DOM is
read-only.
Exit Sub
End If
' A field change has occurred and the DOM is writable. Write code here
to
respond to the changes.
End Sub
- Just before the "End Sub" line, add the following code:
If eventObj.Operation = "Insert" Then
Dim objDate2
Dim objElapsedDays
Set objDate2 =
XDocument.DOM.selectSingleNode("//my:myFields/my:date2")
Set objElapsedDays =
XDocument.DOM.selectSingleNode("//my:myFields/my:txtElapsedDays")
If objDate2.text <> "" Then
objElapsedDays.text = CalcDays(eventObj.Site.text,
objDate2.text)
End If
End If
- Switch back to the InfoPath designer, right-click on date2, choose
Properties, click the Data Validation button, select OnAfterChange
from the
Events box and click Edit
- This will put you back in the Script Editor but with the same basic
code
structure for date2. Just before the "End Sub" line in date2 add the
following:
If eventObj.Operation = "Insert" Then
Dim objDate1
Dim objElapsedDays
Set objDate1 =
XDocument.DOM.selectSingleNode("//my:myFields/my:date1")
Set objElapsedDays =
XDocument.DOM.selectSingleNode("//my:myFields/my:txtElapsedDays")
If objDate1.text <> "" Then
objElapsedDays.text = CalcDays(objDate1.text,
eventObj.Site.text)
End If
End If
- While you are still in the Script Editor, scroll all the way down
past
the last "End Sub", make sure your cursor is in the "blank area" and
add
the following function:
Function CalcDays(date1, date2)
CalcDays = DateDiff("d", date1, date2)
End Function
- Save and close the Script Editor
- Preview the form and select a date from each control - does it work
as
you need?
Scott L. Heim
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no
rights