Using the calendar control in Access

Access provides a calendar control that you can place on a form (form: An Access database object on which you place controls for taking actions or for entering, displaying, and editing data in fields.), report (report: An Access database object that you can print, which contains information that is formatted and organized according to your specifications. Examples of reports are sales summaries, phone lists, and mailing labels.), or data access page (data access page: A Web page designed for viewing and working with data from the Internet or an intranet. Its data is typically stored in an Access database.). You can use the control to display or enter a specific date. The following illustration shows a form that uses the calendar control to display and accept a new employee's hire date.

A form that provides a calendar for entering date values

As you can see, a calendar control looks different from regular Access controls, such as a text box or a combo box. A calendar control is an ActiveX control (ActiveX control: A control, such as a check box or button that offers options to users or runs macros or scripts that automate a task. You can write macros for the control in Microsoft Visual Basic for Applications or scripts in Microsoft Script Editor.), and has a custom behavior that you can manipulate by setting its properties. In the form illustrated above, you can specify a date by selecting a month and year in the combo boxes at the top of the control, and then clicking the box corresponding to the day portion of the date. When you click outside the calendar, the Hire Date text box is updated with the date you specified.

Adding a calendar control to a form, report, or data access page

  1. Open a form, report, or data access page in Design view (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.).
  2. Click the More Controls tool Button image in the toolbox (toolbox: A set of tools that is available in Design view for adding controls to a form or report.), and then click Calendar Control 11.0 in the list.

Security  Use caution when you are adding ActiveX controls to your Web page. ActiveX controls may be designed in such a way that their use could pose a security risk. We recommend that you use controls from trusted sources only.

  1. On the form, report, or data access page, click where you want to place the control. You can move or resize the control the same way you do other controls.

You can change the appearance and other attributes in the control's property sheet. To display the property sheet, select the control and press F4. For help on a specific property, click the property, and press F1. Some key properties are Name, ControlSource, Value, Day, Month, FirstDay, and so on.

The calendar control includes a set of reference topics that describe each of the properties, methods, and events of the control. The reference also includes several examples. To display the reference topics, click the Day property on the Other tab, press F1, and then click Help Topics on the toolbar.

Using the calendar control to display and accept the values in a date/time field

The Value property of the calendar control stores the date that is currently displayed in the calendar. This is the property that you set after getting a user specifies a new value.

 Note   If you want to get or change a portion of the selected date, such as the day, month, or year, use the Day, Month, and Year properties of the calendar control.

However, you can bypass these properties if you are using the calendar control to display and accept values for a date/time field in a table. In this case, you can simply set the ControlSource property of the calendar control to the field's name, and the calendar control will act like any other bound control: It will display the contents of the field for each record, and will automatically update the field when the user changes the selected date in the calendar.

Displaying the values in a date/time field using a calendar    As an example, you may want the calendar control on the Employees form to show the hire date of each employee as you navigate through the records. To do this, bind the calendar control to the HireDate field. When the user enters a hire date for a new employee by using the calendar control, Access will update the HireDate field in the Employees table.

  1. In Design view, select the calendar control and press F4 to display its property sheet.
  2. On the Data tab, set the ControlSource property to the name of the date/time field.

If you want the calendar control and a text box control to display the contents of a field, make sure that the ControlSource property of both controls is set to the name of the underlying field. The user can then view and enter the date with either of the controls — the values in the controls and the underlying field will automatically be updated when the user leaves the record.

 Note   Though you do not need to set or get the Value property when a calendar control is bound to a field, the property is internally kept up to date. If you are writing Visual Basic for Applications (VBA) code to manipulate a bound calendar control, you can set or get the Value property to update or read the field value of the current record. The Day, Month, and Year properties are also automatically updated.

Manipulating the calendar control programmatically     Often, an unbound calendar control requires you to write code to manipulate it. For example, you may want to use calendars on a form to accept a date range from the user. The date range is then used to limit the results of a query. In this case, the default values do not come from an underlying table, and the dates entered by the user are not stored in the database.

The following table shows code examples for common calendar-related tasks.

To do this Use this code Result
Make the calendar display today's date. EndDateCalendar.Value = Date The calendar displays today's date; the current month and year will be selected in the combo boxes at the top of the control, and the corresponding day box will appear selected.
Get the date that is currently selected in the calendar. dtCriteria1 = StartDateCalendar.Value If the user selected May and 2004 in the month and year combo boxes, and clicked day 13, the variable dtCriteria1 will be set to 5/13/2004.
Change the selected month and year in the Month and Year combo boxes on the calendar. StartDateCalendar.Month = 8
StartDateCalendar.Year = 2001
The Month and Year combo boxes will initially display Aug and 2001.

For more examples and help on how to write VBA code for the calendar control, see the Calendar Control Reference Help topics. To display the reference topics, in the calendar control's property sheet, click the Day property, and press F1. Then click Help Topics on the toolbar.

If you want to add clock functionality to a form that will allow users to enter time values by using a clock control instead of typing in a text box, see the topic Add clock control functionality to a form in Access.

Applies to:
Access 2003