Pre-selecting the year for an InfoPath Date Picker control

by S.Y.M. Wong-A-Ton | Filed under: InfoPath 2007

Learn how you can save time and increase efficiency by pre-selecting and setting the year at which an InfoPath date picker control should start displaying dates in its calendar.

Introduction

If you’ve ever used the InfoPath Date Picker control, you might know that one of its limitations is the ability to quickly select a particular year or month.

For example, it works fine for dates in the current year, but what if you want to select a date 10 or 20 years ago?

Currently, you have two options if you want to select a date such as for example March 1, 1995:

  1. Type the value directly into the date picker control.
  2. Click on the back arrow on the calendar of the date picker control to navigate through each and every month starting from 2009 until you reach March 1995.

Setting the year for the calendar of an InfoPath date picker control

If you want to avoid the two scenarios mentioned above, you can do the following:

  1. Add a Drop-Down List Box and Date Picker control to your InfoPath form template.
  2. Fill the Drop-Down List Box control with the dates you want by using either static values or retrieving the values from a secondary data source.

    For example, you could use static date values for years by double-clicking the drop-down list box to open its Properties dialog box and entering the values in the List box entries section on the Data tab.

    Depending on whether your date picker control is set to accept dates or dates and times, you must use the appropriate date format when entering the dates for the years. Here, my date picker control accepts only dates, so I used 1995 as the Display name and 1995-01-01 as the Value when adding an entry for the year 1995 to my drop-down list box.

  3. The last step is to add a Rule to the drop-down list box. The rule must set the value of the date picker control to the value of the drop-down list box as follows:

    Set a field's value: selectedDateDatePicker = .

    where selectedDateDatePicker is the name of the date picker control.

    Note: You can add a rule by clicking on the Rules button on the Properties dialog box of the drop-down list box.

Pre-selecting the year for an InfoPath date picker control
Figure 1. Pre-selecting the year for an InfoPath date picker control.

Now instead of having to click through every month in every year of the date picker to go from the date of today and arrive at a date in the past (or future), all you have to do is select the year from the drop-down list box, and the rule will kick in to set the date picker and its calendar to start at January 1 of the year you selected (depending on the values of the dates you used in your drop-down list box).

This technique allows you to click through less than 12 months to arrive at the date you require. You could extend this technique to also include pre-selection of months.

Related Posts

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. Usage of techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2010 Cookbook

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010