Field Required based on dates - InfoPath Dev
in

InfoPath Dev

Field Required based on dates

Last post 03-19-2007 09:43 AM by Greg Collins. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 03-14-2007 06:07 AM

    Field Required based on dates

    I think I know the answer but want to ask anyay :)

    Using Infopath 2003.

    I have a field that I want to be required if a 'Needed by Date" is less than 10 days from today.

    Is there a way to compare today and the Date needed by to get the 10 day window without writing script?

    Thank you for your Help.

    Art
  • 03-14-2007 09:37 AM In reply to

    Re: Field Required based on dates

    If you are using InfoPath 2007 there is built-in date math for such things. If you are using InfoPath 2003, you need to write code.
  • 03-14-2007 03:15 PM In reply to

    Re: Field Required based on dates

    Yah for 2003 you have to use the datediff function in code to calculate 10 days out. Store this in a field and then you cna do date comparisons against it I can get you a sample if you need or there may be some already here if you search.
  • 03-15-2007 09:47 AM In reply to

    Re: Field Required based on dates

    If you are using managed code instead of script, date functions are built-in so you don't need to write them.
  • 03-16-2007 05:56 AM In reply to

    Re: Field Required based on dates

    Since everything I have done so far in IP 2003 has been throiugh wizards, you both have lost me.

    I understand the concept of creating a non-displayed field to calculate the difference between two dates, but how do I write that "formula".

    I have never scripted or coded in IP 2003.

    Thanks
    Thank you for your Help.

    Art
  • 03-16-2007 09:33 AM In reply to

    Re: Field Required based on dates

    With this requirement, you will of necessity have to be introduced to code (or move up to InfoPath 2007).

    There have been numerous discussions on how to write script for doing date math--both in these forums and across the internet. It shouldn't be to difficult to do a search to locate some code for that.

    If you have Visual Studio and are willing to do managed code (i.e. C# or VB.NET) then you will have built in date functions. Actually, I think VBScript also has date functions, whereas JScript does not.
  • 03-16-2007 01:58 PM In reply to

    Re: Field Required based on dates

    Here are a couple of sample for VBScript.

    This one calculates a date 90 days in advance of today and stores it in a field in my form so I can use it to find data that is within 90 days. You could make the amount 10 instead. If you go to tools and form options you can select whether you will use Java Script or VB Script.
    Then Tools -> programing -> On Load Event then you could paste in the line starting with XDocument. You would need to modify the field description to match your particular schema.

    Sub XDocument_OnLoad(eventObj)
    XDocument.DOM.selectSingleNode("//dfs:myFields/my:Header/my:Variable").text = DateAdd("d",90,date)
    End Sub

    This one is used to subtract 90 days from a date that has been entered by the user to set a start date for some workflow.

    This has been added to a button.

    Sub CTRL266_OnClick(eventObj)
    Dim job
    Dim action
    Dim SDate
    Dim nNode
    Dim flag
    Dim selectflag
    Dim formatDate
    Dim month
    Dim day
    Dim calcDate
    Dim newDate
    flag = "Manage"
    Set job = XDocument.DOM.selectNodes("//dfs:myFields/dfs:dataFields/d:WFPJob")
    job.reset()
    Set nNode = job.nextNode()
    While Not(nNode Is Nothing)
    selectflag = nNode.selectSingleNode("@SelectFlag").text
    If flag = selectflag Then
    SDate = nNode.selectSingleNode("@SDate").text
    End IF
    Set nNode = job.nextNode()
    WEnd

    Trim(SDate)
    formatDate = DateSerial(Mid(SDate, 1, 4), Mid(SDate, 6, 2), Mid(SDate, 9, 2))

    calcDate = DateAdd("d",-90,formatDate)
    month = DatePart("M", calcDate)
    If Len(month) = 1 Then
    month= "0" & month
    End If
    day = DatePart("D", calcDate)
    If Len(day) = 1 Then
    day = "0" & day
    End If

    newDate = DatePart("YYYY", calcDate) & "-" & month & "-" & day

    Set action = XDocument.DOM.selectNodes("//dfs:myFields/dfs:dataFields/d:WFPJob/d:WFPAction")
    action.reset()
    Set nNode = action.nextNode()
    While Not(nNode Is Nothing)
    selectflag =nNode.selectSingleNode("@SelectFlag").text
    If flag = selectflag Then
    nNode.selectSingleNode("@ActionDate").text = newDate
    End IF
    Set nNode = action.nextNode()
    WEnd

    End Sub

    I would do some research on DateAdd and DateDiff for VB if you need to do more.
  • 03-19-2007 09:43 AM In reply to

    Re: Field Required based on dates

    Thanks so much Clay for the code!
    Art, let us know if this solves your issue.
Page 1 of 1 (8 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.