Evaluating dates to determine if they are before today - totally clueless on where to begin - InfoPath Dev Sign in | Join | Help in Code InfoPath (Entire Site) InfoPath Dev InfoPath Dev is dedicated to bringing you the information and tools you need to be successful in your Microsoft Office InfoPath development projects. Home Blogs Forums Photos Downloads InfoPath Dev » InfoPath » Code » Evaluating dates to determine if they are before today - totally clueless on where to begin Evaluating dates to determine if they are before today - totally clueless on where to begin Last post 07-13-2013 01:57 PM by Patrick Halstead. 9 replies. Page 1 of 1 (10 items) Sort Posts: Oldest to newest Newest to oldest Previous Next 07-09-2013 02:12 PM HelenW Joined on 02-24-2011 Posts 29 Evaluating dates to determine if they are before today - totally clueless on where to begin Reply Contact Hi, I'm working in an IP 2007 client form. I have a repeating table that tracks milestone dates. It defaults to 3 rows and I have disabled the users ability to add or remove rows. We have forms for a bunch of different countries; at the end of each quarter, we merge them all together to create a global form. When the forms are merged, each country has three milestone dates. The structure of the milestone group is as follows: gpRepeating (repeating group) dt_NextPRMilestone (date field) dt_PRMilestoneComment (text field) dt_prmMarket (text field) dt_prmBrand (text field) To create the next quarter's form, I take the data from the global form and via C# create a new form that extracts a selected country's values into a new form. This way, the users don't have to re-enter the same data for each quarter. I have adapted the code in this article to work for my milestone dates: http://www.infopathdev.com/blogs/hilary/archive/2008/07/04/how-do-i-copy-repeating-data-from-an-external-source-into-my-form.aspx The part of the code that iterates through the milestones looks like this: //get the list of Milestones for this market XPathNodeIterator prmRows = MainDataSource.CreateNavigator().Select("/my:myFields/my:gpBrands/my:gpBrandDetails[my:dt_Market ='" + market + "']/my:gpRepeating", NamespaceManager); //Add the milestone rows to the xml foreach (XPathNavigator gpRepeating in prmRows) { string gNextMilestone = gpRepeating.SelectSingleNode("my:dt_NextPRMilestone", NamespaceManager).Value; string gMilestoneComments = gpRepeating.SelectSingleNode("my:dt_PRMilestoneComments", NamespaceManager).Value; string gprmBrand = gpRepeating.SelectSingleNode("my:dt_prmBrand", NamespaceManager).Value; if (gNextMilestone != "") { if (gprmBrand == strGDetailBrand) brandXml += "" + "<my:dt_NextPRMilestone>" + gNextMilestone + "</my:dt_NextPRMilestone>" + "<my:dt_PRMilestoneComments>" + gMilestoneComments + "</my:dt_PRMilestoneComments>" + "<my:dt_prmMarket>" + strGDetailMarket + "</my:dt_prmMarket>" + "<my:dt_prmBrand>" + gprmBrand + "</my:dt_prmBrand>" + "</my:gpRepeating>"; } else { if (gprmBrand == strGDetailBrand) brandXml += "<my:gpRepeating>" + "<my:dt_NextPRMilestone xsi:nil=\"true\">" + "" + "</my:dt_NextPRMilestone>" + "<my:dt_PRMilestoneComments>" + "" + "</my:dt_PRMilestoneComments>" + "<my:dt_prmMarket>" + strGDetailMarket + "</my:dt_prmMarket>" + "<my:dt_prmBrand>" + gprmBrand + "</my:dt_prmBrand>" + "</my:gpRepeating>"; So, here's where my question lies - when my users have a milestone date entered, if the date is already passed, I want to be able to remove that date and just keep the dates that are in the future. Further, if I have three dates, let's say August 1, 2013, December 1, 2013, and March 1, 2014, and it is October 2013, I want to clear out the August milestone date and move December 1 to the first row, March 2014 to the second row and have the third row be blank. If it's January, I want it to loop through it a second time, remove December and move March to row 1 and have 2 blank rows below. So any ideas, guidance, code samples, whatever would be so greatly appreciated!! Thanks in advance, Helen 07-09-2013 04:39 PM In reply to Patrick Halstead Joined on 08-08-2006 Posts 5,112 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact Hi Helen, You don't need to write code for this. Just use the out-of-box technique. Select the repeating table in the InfoPath designer. Add a rule (Home->Manage Rules) with a condition where the expression is: translate(my:dt_NextPRMilestone, ":-T", "") < translate(xdDate:AddDays(xdDate:Today(), 0), ":-T", "") Check the box to hide the control. Now all milestones in the past will disappear. How does it work? The translate function removes the special XML formatting out of the string so that it can compare numbers to numbers. Good luck! Patrick HalsteadProject Manager at Qdabra 07-10-2013 05:19 AM In reply to HelenW Joined on 02-24-2011 Posts 29 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact Hey Patrick, Thanks for the speedy response. I love that trick; it hides the past date quite nicely!! However, since it hides the rows, I need to have a blank row added to the end so that there are always three rows available. As I mentioned, I do not allow the users to add or remove rows to the table; is there a codeless way to add a new blank row to the end? Or more specifically add a new row 3 or 6 depending on which one was hidden? If it hides rows 1, 2, or 3, it needs to add a new row 3, if it hides rows 4, 5, or 6, it needs to add a new row 6. The other thing I was thinking was instead of hiding the rows, to set row 1 to row 2, set row 2 to row 3 and just set row 3 as blank. Not sure if that would be a clunky solution or not. Getting closer, for sure. Thanks again, Helen 07-11-2013 05:39 AM In reply to HelenW Joined on 02-24-2011 Posts 29 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact A full day later and I'm no closer to figuring this out. Does anyone else have any ideas? I'm kind of desperate. Thanks, Helen 07-11-2013 07:45 AM In reply to Hilary Stoupa Joined on 06-20-2008 Posts 13,297 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact There is no codeless way to add a new blank row. I wonder if it may help to have multiple instances of the same repeating table on the form - with one formatted to display only rows where certain values are blank? Can you attach a form that displays the problem you are trying to solve under the options tab? That may help me understand the issue better. :) Hilary Stoupa 07-11-2013 08:13 AM In reply to HelenW Joined on 02-24-2011 Posts 29 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact Thanks for the reply Hilary. The solution does not have to be codeless. This morning, I finally did figure something out by way of code, but I'm not 100% happy with it. What I did was as follows (still using the code based on your posting that i mentioned in the initial thread): //Add the milestone rows to the xml foreach (XPathNavigator gpRepeating in prmRows) { string gNextMilestone = gpRepeating.SelectSingleNode("my:dt_NextPRMilestone", NamespaceManager).Value; string gMilestoneComments = gpRepeating.SelectSingleNode("my:dt_PRMilestoneComments", NamespaceManager).Value; string gprmBrand = gpRepeating.SelectSingleNode("my:dt_prmBrand", NamespaceManager).Value; if (gNextMilestone != "") { string Year = gNextMilestone.Substring(0, 4); string Month = gNextMilestone.Substring(5, 2); string Day = gNextMilestone.Substring(8, 2); string FullDate = Month + "/" + Day + "/" + Year; DateTime currentDate = DateTime.Today; DateTime enteredDate = System.Convert.ToDateTime(FullDate); if (gprmBrand == strGDetailBrand) { if (enteredDate >= currentDate) { brandXml += "" + "<my:dt_NextPRMilestone>" + gNextMilestone + "</my:dt_NextPRMilestone>" + "<my:dt_PRMilestoneComments>" + gMilestoneComments + "</my:dt_PRMilestoneComments>" + "<my:dt_prmMarket>" + strGDetailMarket + "</my:dt_prmMarket>" + "<my:dt_prmBrand>" + gprmBrand + "</my:dt_prmBrand>" + "</my:gpRepeating>"; } if (enteredDate < currentDate) { brandXml += "<my:gpRepeating>" + "<my:dt_NextPRMilestone xsi:nil=\"true\">" + "" + "</my:dt_NextPRMilestone>" + "<my:dt_PRMilestoneComments>" + "(" + Month +"/" + Year + " milestone has elapsed)" + "</my:dt_PRMilestoneComments>" + "<my:dt_prmMarket>" + strGDetailMarket + "</my:dt_prmMarket>" + "<my:dt_prmBrand>" + gprmBrand + "</my:dt_prmBrand>" + "</my:gpRepeating>"; } } } else { if (gprmBrand == strGDetailBrand) brandXml += "<my:gpRepeating>" + "<my:dt_NextPRMilestone xsi:nil=\"true\">" + "" + "</my:dt_NextPRMilestone>" + "<my:dt_PRMilestoneComments>" + "" + "</my:dt_PRMilestoneComments>" + "<my:dt_prmMarket>" + strGDetailMarket + "</my:dt_prmMarket>" + "<my:dt_prmBrand>" + gprmBrand + "</my:dt_prmBrand>" + "</my:gpRepeating>"; } } What this did was allow me to blank out the row and keep it in place. (actually, I put a message in the comments field that the old milestone date elapsed so that the empty field wouldn't be too confusing). What I would like to do it put that blank row at the end of the line. If you want me to upload the form, I can, but I'll need to strip some proprietary info out before I can send it to you. If you think you can do it just using my code sample, that'd be easiest. Just let me know. Thanks in advance for your help Hilary. You're always so helpful when I post on this site!! Helen 07-11-2013 08:40 AM In reply to Hilary Stoupa Joined on 06-20-2008 Posts 13,297 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact What is it you are trying to achieve, exactly? Do you want to just remove the expired milestones and then add a blank row at the end of the table? Sorry if I'm being a bit dense this morning - but I think it might be simplest to start with the end goal. :) Hilary Stoupa 07-11-2013 08:54 AM In reply to HelenW Joined on 02-24-2011 Posts 29 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact yup - the end goal is to remove all milestone dates that have passed, but still have the space for 3 milestones. A form can have multiple brands, each brand has up to three milestones. If they have less than three, there should be a blank row to add the third date. Users cannot add or remove rows. Right now it removes the past date and leaves the row in place. That looks kind of icky to have a blank row then 2 populated rows. And usually the milestones are entered in ascending order, so the first row will fall off first. Does that help? Helen 07-11-2013 09:26 AM In reply to Hilary Stoupa Joined on 06-20-2008 Posts 13,297 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact Okay, so that makes sense. I guess I'd probably sort the rows - here's an MSDN link on that: http://msdn.microsoft.com/en-us/library/b2hhe5h7(v=vs.80).aspx Hilary Stoupa 07-13-2013 01:57 PM In reply to Patrick Halstead Joined on 08-08-2006 Posts 5,112 Re: Evaluating dates to determine if they are before today - totally clueless on where to begin Mark as Not AnswerMark as Answer... Reply Contact May I humbly suggest using a bunch of blank rows as default data and only showing three of them? Then, you are codeless and everyone knows codeless is the way to go for minimizing future maintenance cost. Patrick HalsteadProject Manager at Qdabra Page 1 of 1 (10 items) Copyright © 2003-2019 Qdabra Software. All rights reserved.View our Terms of Use.
Hi Helen,
You don't need to write code for this. Just use the out-of-box technique. Select the repeating table in the InfoPath designer. Add a rule (Home->Manage Rules) with a condition where the expression is:
translate(my:dt_NextPRMilestone, ":-T", "") < translate(xdDate:AddDays(xdDate:Today(), 0), ":-T", "")
Check the box to hide the control. Now all milestones in the past will disappear.
How does it work? The translate function removes the special XML formatting out of the string so that it can compare numbers to numbers.
Good luck!
There is no codeless way to add a new blank row. I wonder if it may help to have multiple instances of the same repeating table on the form - with one formatted to display only rows where certain values are blank? Can you attach a form that displays the problem you are trying to solve under the options tab? That may help me understand the issue better. :)
What is it you are trying to achieve, exactly? Do you want to just remove the expired milestones and then add a blank row at the end of the table? Sorry if I'm being a bit dense this morning - but I think it might be simplest to start with the end goal. :)
Okay, so that makes sense. I guess I'd probably sort the rows - here's an MSDN link on that: http://msdn.microsoft.com/en-us/library/b2hhe5h7(v=vs.80).aspx
May I humbly suggest using a bunch of blank rows as default data and only showing three of them?
Then, you are codeless and everyone knows codeless is the way to go for minimizing future maintenance cost.