MS Access - Calendar Example

This page was originally written for MS Access 97. Starting with MS Access 2007, a new method is used to accomplish this task and the ActiveX controls the rest of this page talks about are no longer available to use.

With MS Access 2010, the date method no longer returns the current date - instead it produces a compile error!

This has turned out to be much more involved than I thought. There are 2 Calendar Control examples

Overview | DTPicker | Calendar Control | Multiple Detail Records | Basic Code | Dialog Box | Dialog Box Code | MS Access 2007 | MS Access 2010


Overview

There are 3 calendar controls. They can be selected via either of 2 methods The following lists the controls (and their libraries) to select from the list.

Warning:

I sometimes work with the Toolbox docked at the bottom of the screen. In this configuration, the More Controls icon won't work - sometimes nothing happens, but more frequently the kernel produces a general protection fault and MS Access crashes. (Of course, you loose all un-saved changes.) The solution is to un-dock the Toolbox before selecting More Controls. Of course, you could simply use the menu instead.
(Unbelievable. Don't they test anything? Verified with Microsoft Access 97 SR-2.)

Note:


DTPicker

This control should be available on machines that have one of the Microsoft compilers installed - VisualBasic, VisualC++ and the like. Be careful, my previous experience with another ActiveX control indicates that it is fairly easy to get these to work on machines where Visual Studio is installed, but almost impossible to use them on other machines.

This control is easy to use - just place it on your form. It does not require any special code. Click the down arrow to display the calendar. As soon as you click on a date, the calendar closes and the date is copied to the edit field.

Warning: This control does not like null values. In its default configuration, if you create a record and don't automatically assign default values to all the date fields, you will get an error.

In order to allow null values, you must enable the integrated check box. When the box is not checked, the value is Null, and the field shows today's date as grayed out; when checked, you can set the date. (It looks pretty dorkey.)

I prefer the Calendar Control discussed next because it shows a null value as just a blank field.


Calendar Control

There is a significant issue with the Calendar Control Therefore, there is no way to indicate that you have made a selection and that the Calendar Control should close. The code provided in the next section provides one solution.

First, on a form, place 3 components

Make the following modifications to the form's code page. Refer to the code in the next section . To use the calendar, type a date in the text box and click the toggle button. The calendar will be displayed showing the date from the text box. Click in the calendar control to select another date. If you then click any other control on the form, the calendar will loose focus, the currently selected date is copied to the associated text box, the focus is moved to the associated text box, and the Calendar Control is hidden.

This code is designed so that, if there are several text controls associated with dates, you only need one Calendar Control and you don't need to manage several copies of almost identical code, one set per control. Instead, each toggle button calls ShowCalendar and passes the names of the appropriate Text Box / Toggle Button pair. The code even positions the calendar under the associated text box.

There are a number of instances where the autopositioning algorithm fails (when your text box is too close to the edge of the form, the algorithm places the calendar so that it overlaps the edge of the form) and you will get an error. To solve this, I have provided ShowCalendar_xy which requires a Text Box / Toggle Button pair and the x/y position where you want the calendar displayed. (The x/y values must be in inches.)

In order to get the correct x/y position values, I suggest manually placing the calendar control where you want it displayed and using its Left and Top properties as your x/y values, respectively.


More Events

Unless you know 2 tricks, documentation for MSCAL.Calendar.7 is poor at best.

Use this to update a text control when the calendar is clicked

and this to close the Calendar when it is double clicked
I spent 5 hours getting the double click to work. Every time Text14.SetFocus was executed, Access showed Eventually, I added another Calendar control and the code worked. Apparently, Access has some sort of design problem.


References

The references strongly suggest NOT using the Calendar control (MSCal.OCX) because I agree with all of these (but I also use the control ... sometimes).


Use with Multiple Detail Records

Some controls don't work well when several records are shown in the same form - the Toggle Button is one of them. When used in repeating detail sections - when one Toggle Button is depressed, they are all depressed. (Yuk!)

The "solution" is to use a Push Button (Command Button) control. This does not render the button as depressed while the Calendar is displayed (the Windows standard), but it is better than having them all depressed. Set the properties the same as for the Toggle Button above.

If you use the routines below, notice that they require you to pass a Toggle Button. I just place a hidden button (Visible = No) somewhere on the form and pass that.

For some forms (particularly those with multiple detail records and/or a subform), no section is large enough to display a Calendar. In those cases, I use a Calendar dialog box to provide this functionality.


Code that works with the "Calendar Control"


A Calendar Dialog Box

The Calendar Control needs to be 2 inches high. However, many forms have space limitations and no individual section is that large. For instance, when many records are shown at one time, they should be as narrow as possible (to show as many records as possible). In these case, it is better to place the Calendar in a dialog box. Creating it is simple The trick is to set the Calendar's date in Form_Load ... fails in Form_Open.

For a basic Calendar dialog box


Code for a Calendar Dialog Box

Code in the calling form - attached to a push button (command button) The following code is placed in the Calendar dialog box In Form_Load, either of these will work


MS Access 2007

Starting with MS Access 2007, the date/calendar functionality is built in and there is no longer a need for a separate ActiveX component. When the user clicks inside the text box, a calendar icon will be displayed to the right of the text field. When the icon is clicked, the calendar will be displayed.


MS Access 2010

Well, with the 2010 MS Office update, Microsoft has managed to screw customers yet again. I tried to use an application that has worked for several years and got with the word date highlighted in the following code. This is what it had to be changed to to work with the 2010 version. Though these are simple changes, they are not documented on any Microsoft page. In fact, as of July 2014, Microsoft still claims that the old (no longer working) date function still works. (This failure has been verified on 3 separate systems.)

(By the way, the spacing shown above is not allowed in MS Access - it simply removes the extra spaces to make the code less readable!)

I am going out on a limb here and making a guess. It appears that date is now a variable type and, therefore, no longer gets the current date. As a result, MS Access no longer knows what to do with it. (Unfortunately, I no longer have access to an older system to check any of this.)

At any rate, this problem happened because the corporate bosses pushed the MS Office "update" to every machine .. without warning. One day, everything was simply broken. Of course, these clueless morons were not able to fix (or even admit to) the disaster they caused.

My rule still stands - never update working software. EVER!!


Author: Robert Clemenzi
URL: http:// mc-computing.com / Databases / MSAccess / Calendar_Example.html