Setting a date from Code causes validation error - InfoPath Dev
in

InfoPath Dev

Setting a date from Code causes validation error

Last post 11-29-2010 08:45 AM by denfer06. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 07-20-2009 06:04 PM

    • djj66
    • Not Ranked
    • Joined on 05-27-2009
    • Posts 13

    Setting a date from Code causes validation error

    Hi all, I have got a piece of code which sets the date on a control on my InfoPath form (VBScript and IP 2003). The code I have is...

    Dim dtTargetComplete

    dtTargetComplete = XDocument.DOM.selectSingleNode ("/dfs:myFields/my:TargetCompleteDate").text dtTargetComplete = DateAdd("d",7,now)

    if XDocument.DOM.selectSingleNode ("/dfs:myFields/my:TargetCompleteDate").removeAttribute("xsi:nil") then

    XDocument.DOM.selectSingleNode ("/dfs:myFields/my:TargetCompleteDate").text = FormatDateTime(dtTargetComplete,2)

    else XDocument.DOM.selectSingleNode ("/dfs:myFields/my:TargetCompleteDate").text = FormatDateTime(dtTargetComplete,2)

    end if

    Now this code works OK, and correctly sets the date in the contol. But when the date is set in the control, the control appears with dotted red border, as with a validation error. If I then make a slight edit to the data in the contol (eg replace 2009 with 2009) then the validation error disappears. The control is defined as a Date, not a text or number.

    Does anyone know how to avoid this validation error when setting dates from Code. Many thanks!

     

     

     

  • 07-20-2009 10:15 PM In reply to

    Re: Setting a date from Code causes validation error

    Hi Djj66,

    Not sure, but you are using FormatDateTime and you said the element was a Date not a DateTime. If you change it to DateTime does that change your mileage?

    Just wondering...

    Patrick Halstead
    Project Manager at Qdabra
  • 07-20-2009 11:11 PM In reply to

    • djj66
    • Not Ranked
    • Joined on 05-27-2009
    • Posts 13

    Re: Setting a date from Code causes validation error

    Hi there, thanks, but this doesn't appear to make any difference...

    I can set the control as DateTime and from code set the control either as date only or date and time, and it always gets the validation error.

    Then as soon as I make even the smallest edit the the value, the validation problem disappears.

    From what I see, the code is working perfectly, but the form control initially rejects the set value with the validation. I've tried most things with the form control, and haven't been able to fix yet. Any other suggestions?

  • 07-20-2009 11:50 PM In reply to

    Re: Setting a date from Code causes validation error

     FormatDateTime(DATE, 2) will format the date with slashes in the order month, date, year (e.g. 7/21/2009), but an XML date must be of the form YYYY-MM-DD, i.e., year, month, date separated by hyphens and with padding zeros where necessary. FormatDateTime does not seem to have the option to format a date this way, so it may make sense to write your own convenience function to get the proper format.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 11-29-2010 08:45 AM In reply to

    Re: Setting a date from Code causes validation error

     Hello,

    for people that want a sample the below format solved be above issue on my side:

    ideaNav.SelectSingleNode("@my:InitalReviewDeliveryDate", NamespaceManager).SetValue(currentDate.ToString("yyyy-MM-ddTHH:mm:ss"));

    this is the default date&time format used by InfoPath that you can find in saved XML.

     thanks for your help

Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.