Hi,
Please accept my apologies for the delay as I was unexpectedly out of the
office for a few days.
Here are steps to a basic sample - with the following caveats:
1) This is using VBScript and not JScript
2) There is no real error handling here - so you will need to add
additional functionality, such as: confirmation that one date is greater
than or equal to the other, both fields are not empty, etc.
- Create a new, blank form
- Add the following controls and names:
- Date Picker, EffectiveDate
- Date Picker, ReturnDate
- Text box, txtDiff
- Right-click on ReturnDate and select Properties
- Click the Data Validation button
- In the Events box, choose OnAfterChange and then click Edit
- You should now see basically the following:
Sub msoxd_my_ReturnDate_OnAfterChange(eventObj)
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
- Place your cursor in the space just before the words "End Sub" and paste
in the following code:
'When you add/edit text in a control, we go through a "delete" process to
remove any existing text
'and then an "Insert" process to enter what you just selected or typed -
we only want this code to execute
'during the "Insert" process - as such, we need to test for the correct
operation
If eventObj.Operation = "Insert" Then
Dim objEffDate
Dim objRetDate
Dim objDiff
'Get references to the XML nodes that correspond to our controls
Set objEffDate =
XDocument.DOM.selectSingleNode("//my:myFields/my:EffectiveDate")
Set objRetDate =
XDocument.DOM.selectSingleNode("//my:myFields/my:ReturnDate")
Set objDiff = XDocument.DOM.selectSingleNode("//my:myFields/my:txtDiff")
'If the EffectiveDate field is not an empty string (in other words a date
was selected)
'then compare this (the ReturnDate) to the EffectiveDate field.
If objEffDate.text <> "" Then
objDiff.text = DateDiff("d", objEffDate.text, objRetDate.text)
End If
'Clean up
Set objEffDate = Nothing
Set objRetDate = Nothing
Set objDiff = Nothing
End If
- Preview the form and test
I hope this helps!
Best Regards,
Scott L. Heim
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.