MS Access - Calendar Example

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


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


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Databases / MSAccess / Calendar_Example.html