Date calculation for a non-programmer

R

rjblanch

Hi all,

I have 2 fields (date pickers) on a form, that I need to validate a setting
of minimum of 3 days between the 2. To put plainly - DateRequired is
after(DateRequested + 3 days).

I have gone through all of the postings and i have tried all of the
solutions offered and checked the listings. I understand that calculations
cannot be perfomed on date fields (happy with this), I understand that they
need to be transferred to the ISO DateTime standard (happy with this) and
then from here a calculation needs to be made.

The only issue I am having at the moment is I do not know how to "program"
this. I have copied some of the code and then gone to tools->programming and
pasted it in, but keep coming up with errrors (I did change the field names
that were required).

If anyone could let me know - very plainly - how to do my issue, that would
be appreciated.
 
S

Scott L. Heim [MSFT]

Hi,

If you are using VBScript, here is some sample code that calculates the
difference between 2 date fields, named date1 and date2:

Sub CTRL3_5_OnClick(eventObj)
Dim nodeDate1
Dim nodeDate2
Dim dt1Val
Dim dt2Val
Dim daysElapsed

Set nodeDate1 = XDocument.DOM.selectSingleNode("//my:myFields/my:date1")
Set nodeDate2 = XDocument.DOM.selectSingleNode("//my:myFields/my:date2")

dt1Val = CDate(nodeDate1.Text)
dt2Val = CDate(nodeDate2.Text)

daysElapsed = DateDiff("d", dt1Val, dt2Val)
If daysElapsed < 3 Then
Msgbox "Date #2 must be at least 3 days in the future!"
End If
End Sub

I hope this helps!

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

rjblanch

Thanks for the information - i can see how this would work.

I guess the major part of my issue is that I go to programming and put this
in, but it still will not work. Do I need to wrap it up in a <!----Script >
tag or something. I guess I need to start a little bit further forward in
the book....

Thanks.
 
S

Scott L. Heim [MSFT]

Hi,

I had made a couple of assumptions - I am sorry about that. Let me provide
some explicit steps for getting to the "Click" event for a button.

** NOTE: The code I had previously provided was in VBScript. The steps I am
going to provide are to insure VBScript is the selected language. If you
have already tested the code I provided and your selected language was
Jscript, then this will not work.

1) Open your template in Design View
2) Right-click on the button and choose Button Properties
3) Click the Edit Form Code button

This should automatically open the script editor with an empty procedure
similar to the following:

Sub CTRL1_5_OnClick(eventObj)
' Write your code here
End Sub

The code you would need to paste in this procedure is:

Dim nodeDate1
Dim nodeDate2
Dim dt1Val
Dim dt2Val
Dim daysElapsed

Set nodeDate1 = XDocument.DOM.selectSingleNode("//my:myFields/my:date1")
Set nodeDate2 = XDocument.DOM.selectSingleNode("//my:myFields/my:date2")

dt1Val = CDate(nodeDate1.Text)
dt2Val = CDate(nodeDate2.Text)

daysElapsed = DateDiff("d", dt1Val, dt2Val)
If daysElapsed < 3 Then
Msgbox "Date #2 must be at least 3 days in the future!"
End If

** NOTE: You will need to change the path in the selectSingleNode arguments
above so that this correctly traverses your data source to your date fields.

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.
 
R

rjblanch

Thank you for the information. I must admit that I jumped into Infopath
without really reading anything about the back end. I now have a book that
explains about the code programming side. I can now see how to associate
your earlier post to the coding that was required.

Thank you for your assistance with this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top