Calendar on Main Form to Use in Field of "Form of Focus"

April 11th, 2001
Need Step-By Step Instructions? Go here
Other Popup Calendar Links here, here and here

Of the methods shown, this is actually the best way, in general, to set up calendar controls, because the calendars are kept in a separate form to themselves and referenced on demand as needed. This neatly keeps the calendars out of the way when viewing the form in "design" view.

Assume the name of the date field in the form is beg_date, the name of the form which contains this field is search_form, and the name of the form with the calendar(s) is calendar_form. The name of the Calendar Control is cal_3. The Visual Basic to use in the beg_date field in the form would be:

Private Sub beg_date_DblClick(Cancel As Integer)
' Show cal_3 and set its date.
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "calendar_form"
DoCmd.OpenForm stDocName, , , stLinkCriteria
Forms![calendar_form]!cal_3.Visible = True
Forms![calendar_form]!cal_3.SetFocus
' Set to today if date_opened has no value.
Forms![calendar_form]!cal_3.Value = IIf(IsNull(beg_date), Date, beg_date.Value)
End Sub

The Visual Basic to use in the Calendar in the Calendar form would be:

Private Sub cal_3_Click()
' Set date_closed to the selected date and hide the cal_3.
Forms![search_form]!beg_date.Value = Forms![calendar_form]!cal_3.Value
DoCmd.Close
End Sub

****************************

If you have used this code before and are simply applying it to your database with everything else done, you can merely copy-paste the above code into a text editor, then search for the field "beg_date" and replace it with your applicable date field, and replace "cal_3" with the actual name of your calendar control.

****************************

Step By Step Instructions

The above instructions assume you already know how to set up the calendar control basics, and you merely need the Visual Basic code to make it work. If you need step by step instructions, here they are.

First, an introduction. The code contained here will activate a pop-up calendar whenever the user "double-clicks" the date field. (The other methods generally do it whenever the person single-clicks the box, which I think is inferior.) This method places the calendar control in a form of its own, and this form is called up from whatever form the database is currently in as the calendar is needed. Most of the other methods described in these pages involve placing the Calendar Control directly in the form it's needed. This poses a problem mainly because when the form is opened in "design" view later, the calendar control gets in the way. This prevents that. It also helps keep all Calendars centralized in one place, to prevent confusion--especially important if there are several date fields that will use calendars.

The ideal calendar design would have 1 Calendar Control which would work with all dates. Unfortunately, I have yet to figure out how to do this; there has to be a calendar inserted for every date field in existence. At least, however, with all the calendars being on one form they are kept out of the way. Also, since visual basic is involved and the calendars are referred to by their names, this method keeps things neater.

Now, the step-by-step instructions:


Assume you have a date field in a form entitled "main_form." The date field's name is "beg_date" (for beginning date).

  1. Create a new form in "design view." Do NOT link it to any table or query. Leave the form at its standard size, about 2 x 5 inches. Name the form "calendar_form"
  2. With the form open under "Design" view, select "Form, Properties" by double-clicking the small squre in the left-hand portion of the form. (Or, you can select the very top portion of the form, right-click, and select "properties." Then, setup the form properties as follows:
    Property Tab to Select Attribute Setting
    Format Caption Calendar Form (or whatever you wish)
      Scroll Bars Neither
      Record Selectors No
      Navigation Buttons No
    All Pop Up Yes
  3. Assuming the form is still open under "design" view, from the pull-down menu select "Insert....ActiveX Control.......Calendar Control 8.0"
  4. While hovering your mouse over the calendar, right-click and select "properties"
  5. A properties tab should pop up which says something like "ActiveX Control:ActiveXCt10." Setup the properties for the calendar as follows:
    Property Tab to Select Attribute Setting
    Format Visible No
    Other Name calendar_1
  6. Resize the form (by dragging) so that the form is almost the same size as the calendar itself.
  7. Once again, open up the properties for the Calendar Active X Control.
  8. Select the "event" tab.
  9. Select the On Enter Tab (click the "...."), then click "code builder"
  10. Near the top-right, you should see a drop-down box which says Enter. Select "click" from that drop-down box.
  11. Insert the following Visual Basic Code, between the "Private Sub calendar_1_(Click)" and "End Sub" lines:

    ' Set beg_date to the selected date and hide the calendar_1.
    Forms![main_form]!beg_date.Value = Forms![calendar_form]!calendar_1.Value
    DoCmd.Close

  12. Save the code by clicking the Floppy Disk Icon near the top-left, then "x" out of the Visual Basic Code
  13. Save/close the "calendar_form"
  14. Open up, in design view, the form which contains the date the calendar control would apply to (in this example, the form would be "main_form")
  15. Select the text box of the date which applies (in this case, "beg_date"), right-click, and select "properties"
  16. Under the "Event" tab, select "On Dbl Click" (click the "....."), then click "code builder"
  17. Insert the following Visual Basic Code, between the "Private Sub beg_date_DblClick(Cancel As Integer)" and "End Sub" statements:

    ' Show calendar_1 and set its date.
    Dim stDocName As String
    Dim stLinkCriteria As String
    stDocName = "calendar_form"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Forms![calendar_form]!calendar_1.Visible = True
    Forms![calendar_form]!calendar_1.SetFocus
    ' Set to today if beg_date has no value.
    Forms![calendar_form]!calendar_1.Value = IIf(IsNull(beg_date), Date, beg_date.Value)

  18. Save the code by clicking the Floppy Disk Icon near the top-left, then "x" out of the Visual Basic Code
  19. Close and Save "main_form"

This method works with any form and any date field. Simply substitute the proper form name in place of "main_form" and the proper date field in place of "beg_date." Also, if you have more than 1 date/form this applies to, you would insert a new Calendar Control on the "calendar_form" for every one of these, and the name of the calendar would change from calendar_1 to calendar_2, calendar_3, etc. Or, you could name it "cal_1" then "cal_2." The important thing is to make sure you keep the proper calendars matched up with the proper date fields.