For this tip I am going to show you how to add an ActiveX Calendar control to a form, then use this control to add data (i.e. a date) to another field on your form.
First of all, an explanation:
The ActiveX Calendar control is a visual way users can view dates on a form. The dates are displayed in a Calendar format. To insert one into your form, open your form in Design view (choose a form that has a field that is bound to a date field in a table) by:
Clicking on ‘Insert’ on the menu bar; then choosing ‘ActiveX control’ from the drop down list.
The ‘Insert ActiveX control’ window will open.
Scroll down until you find Calendar Control 10.0 (or Calendar Control 9.0, or possibly another number, depending on your version of Access);
Select this then click on ‘OK’.
The ActiveX Calendar Control will be inserted automatically. The Calendar may be covering up your other controls, so move it to an appropriate place.
Change to Form view and select a date on the Calendar.
Note: the date in the date text box has not been updated.
To fix this we are going to add some code to the ‘On Click’ event for the Calendar.
Normally to do this, I would suggest right clicking on the Calendar, selecting properties and then clicking on the ‘Event’ tab. But, if you were to do this, you would not see an ‘On Click’ event.
The ‘On Click’ event is available; we just have to access it in a different way.
To do this:
Open the form in Design view;
Right-click on the outer edge of the Calendar control and select ‘Build Event’ from the drop-down list;
In the ‘Choose Builder’ window, select ‘Code Builder’ and then click ‘OK’ (if prompted).
A Subprocedure will be created as per below:
Private Sub Calendar4_Updated(Code As Integer)
The default Subprocedure for the ActiveX Calendar Control is ‘Updated’, as indicated above, not the ‘On Click’ event. You will need to create another event for the 'On Click' event
To do this:
Click on the drop-down arrow in the procedure section of the VBA window (this is where the ‘Updated’ event is currently displayed), in the upper right-hand section of the window, just below the toolbars;
Scroll down and select the ‘Click’ event;
A new Subprocedure will be created as per below:
Private Sub Calendar4_Click()
Then, to the ‘Click’ Subprocedure, add the following code:
Me!DateField = Me!Calender4
Change ‘DateField’ to the name of the field that contains your date data. You will also need to change the Calendar Number to coincide with the Calendar number on your form.
Test this code to make sure it works by closing the VBA (coding window) and opening the form and clicking on a date on the Calendar; your date field should be updated.
Delete the 'Update' Subprocedure as you do not need this from the coding window.
Microsoft Access 2007 and later
You do not have to worry about this AciveX Control in Microsoft Access 2007 and later, as a date picker/calendar will appear automantically for the user, when they put their cursor in a date field on a form.