Hi Smitty,
Here are sample steps and code:
- Create a new, blank InfoPath form
- Add 2 Date Picker controls (field1 and field2) and add a check box
control (field3)
- From the Tools menu, choose Form Options, select the Advanced Tab, make
sure the Form Code Language is set to VBScript and click OK
- Right-click on field1, choose Properties, click the Data Validation
button, from the Events box choose OnAfterChange and click Edit - you
should see the following:
Sub msoxd_my_field1_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 this code:
If eventObj.Operation = "Insert" Then
Dim objDate2
Dim objCheckBox
Set objDate2 = XDocument.DOM.selectSingleNode("//my:myFields/my:field2")
Set objCheckBox = XDocument.DOM.selectSingleNode("//my:myFields/my:field3")
If objDate2.text <> "" Then
If CalcDays(eventObj.Site.text, objDate2.text) > 365 Then
objCheckBox.text = "true"
Else
objCheckBox.text = "false"
End If
End If
End If
- Switch back to the InfoPath designer
- Right-click on field2, choose Properties, click the Data Validation
button, from the Events box choose OnAfterChange and click Edit - again,
just before the End Sub line, add this code:
If eventObj.Operation = "Insert" Then
Dim objDate1
Dim objCheckBox
Set objDate1 = XDocument.DOM.selectSingleNode("//my:myFields/my:field1")
Set objCheckBox = XDocument.DOM.selectSingleNode("//my:myFields/my:field3")
If objDate1.text <> "" Then
If CalcDays(objDate1.text, eventObj.Site.text) > 365 Then
objCheckBox.text = "true"
Else
objCheckBox.text = "false"
End If
End If
End If
- Now, scroll all the way down past all code, make sure your mouse cursor
is blinking in the "white" area and then add this code:
Function CalcDays(date1, date2)
CalcDays = DateDiff("d", date1, date2)
End Function
- Save and close the Script Editor
- Right-click on field2, choose Properties, select the Display tab and
click the Conditional Formatting button
- From the first box, choose: field3
- From the second box, choose: is equal to
- From the third box, choose: TRUE
- From the Shading box, choose Red
- Click OK until you are back to your form
- Preview and enter: 9/1/2005 in field1 and 9/2/2006 in field2 - does
field2 change to red? If so, modify field2 to: 9/1/2006 - it should now go
back to white!
If this works as you need, you can actually delete the check box from the
form (not the Data Source - just the form) as it is not necessary to have
this on the form.
Let me know if this works for you!
Scott L. Heim
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.