Introduction.
The Calendar ActiveX Control is a convenient tool for quickly inserting date values into fields with a simple click, eliminating the need for manual typing.
The only drawback of the Calendar ActiveX Control is its large size—it occupies considerable space on the form, especially when multiple date fields are involved. This can interfere with the placement of other controls and compromise the overall layout. As a result, despite its convenience, developers may hesitate to include it in their design and instead fall back on the traditional approach of manually typing the Date value—after all, it’s the user, not the developer, who will be doing the typing.
We’re going to use the Calendar Control—because we don’t accept defeat. The concerns mentioned earlier won’t stop us from using this powerful tool wisely. The method we’ll implement here may seem a bit complex at first, but once set up, it becomes very easy to reuse across other forms within the same project.
Microsoft Access Floating Calendar Project.
Open your database and select a form you've already designed, or create a new one with at least two Date Fields. Let’s begin with the easy part of our project. Copy and paste the following VBA code into a standard (global) module, then save it:
Option Compare DatabaseOption Explicit 'Global Declarations Public Const twips As Long = 1440 Dim mm_actctl As Control Public Function Calendar() Dim sngStart As Single, CalCtrl As Control Dim ctl As Control, frm As Form, t_height As Long Dim m_left As Long, m_top As Long, i As Double Dim w As Long, h As Long, y As Double Dim caltop As Long, calheight As Long Dim secHeight As Long Dim frmWidth As Long, t_width As Long On Error GoTo Calendar_Err Set ctl = Screen.ActiveControl Set mm_actctl = ctl Set CalCtrl = Screen.ActiveForm.Controls("Cal1") Set frm = Screen.ActiveForm CalCtrl.Width = 0.1458 * twips ' 0.1458" CalCtrl.Height = 0.1563 * twips ' 0.1563" m_left = ctl.Left + ctl.Width m_top = ctl.Top + ctl.Height caltop = m_top calheight = ctl.Height + (15 * twips * 0.106) '0.105" secHeight = frm.Section(acDetail).Height frmWidth = frm.Width t_height = caltop + calheight t_width = m_left + (15 * twips * 0.17) '0.17" If t_height > secHeight Then m_top = secHeight - (calheight + (0.106 * twips)) End If If t_width > frmWidth Then m_left = frmWidth - (15 * twips * 0.17) ' 0.17" End If CalCtrl.Left = m_left CalCtrl.Top = m_top CalCtrl.Visible = True sngStart = Timer i = 0.05: y = i Do While Timer < (sngStart + 0.75) If Timer >= sngStart + y Then y = y + i w = CalCtrl.Width + (0.17 * twips) ' 0.17" CalCtrl.Width = w h = CalCtrl.Height + (0.106 * twips) ' 0.105" CalCtrl.Height = h DoEvents End If Loop Calendar_Exit: Exit Function Calendar_Err: MsgBox Err.Description, , "Calendar" Resume Calendar_Exit End Function 'Insert Date into the active Field Public Function Cal1Click() Dim m_cal As Control, m_ctl As Control On Error GoTo Cal1Click_Err Set m_cal = Screen.ActiveForm.Controls("Cal1") mm_actctl.Value = m_cal.Value m_cal.Width = 0.1458 * twips ' 0.1458" m_cal.Height = 0.1563 * twips ' 0.1563" mm_actctl.SetFocus DoEvents m_cal.Visible = False Cal1Click_Exit: Exit Function Cal1Click_Err: MsgBox Err.Description, , "Cal1Click" Resume Cal1Click_Exit End Function
Note: If you encounter any errors while running this code, please refer to my earlier post titled Command Button Animation. It explains how to link the necessary library references to your project—an essential step for this code to work correctly.
MS-Access Calendar Control.
- Open the Form in Design View.
- Select Microsoft Access ActiveX Control from the Insert Menu.
- Scroll through the Displayed List and find Calendar Control as shown in the image below:
- Select it and click OK. A Calendar Control is inserted into your Form.
Temporarily position the control anywhere on the form where it's convenient.
Next, click on the control to select it, open its Property Sheet, and update the following property values as indicated below:
Ms-Access Calendar Control Properties
- Name = Cal1
- Visible = False
- Special Effect = Raised
- Border Color = 0
- Back Color = 11139322
- Month Length = System (Medium) - Access 2003, e.g.: Jul 2007
- Grid Lines Color = 2147483632
- Grid Font Color = 10485760
- Title Font Color = 10485760
- Width = .1458"
- Height = .1563"
After adjusting the Width and Height properties, the Calendar control becomes a small rectangle that you can easily position anywhere on the form, wherever it's most convenient.
Next, we need to copy a few additional lines of code into the Form's code module. While the form is in Design View, either click the Code button on the toolbar or choose View → Code from the menu. Then, paste the following code into the form’s module:
Private Sub Cal1_Click() Cal1Click End Sub Private Sub Detail_Click() Me.Cal1.Visible = False End Sub Private Sub Form_Load() Me.Cal1.Value = Date End Sub
When you click on a date in the Calendar, the Cal1Click()
routine is triggered, inserting the selected date into the currently active field and then hiding the Calendar.
However, if you decide not to select a date and simply want to hide the Calendar, clicking anywhere on the empty area of the form’s Detail section will do so. This behavior is handled by the Detail_Click()
routine.
Additionally, when the form opens, the Calendar is automatically initialized with the system's current date, thanks to the Form_Load()
event procedure. This ensures the Calendar always starts with today’s date selected.
If you're new to customizing Microsoft Access menus and toolbars, the next section might seem a bit unfamiliar. But don’t worry—we're just going to explore features that already exist within Access. It’s something every developer eventually needs to learn, and getting familiar with it now will save you time later.
Creating MS Access Toolbar Button.
We'll now create a new toolbar button and link it to the procedure you previously copied into the global module. For added convenience, we'll also place a copy of this toolbar button in a shortcut menu. This setup allows you to easily launch the animated floating Calendar control directly from your MS Access form.
Select the following Menu Option:
- View - - > Toolbar - - > Customize. Click New to create a new Toolbar and name it as ToolCal, then click OK. A new small empty Toolbar will show up on top.
- Select the Commands Tab. Click and drag the New button and place it on the new Toolbar.
- Right-click on the new toolbar button, and point the cursor on the Change Button Image option on the displayed menu to show several Button Images. You may select one of the Images you prefer to use.
If you'd like to create a custom button image, you can do so by choosing the Edit Button Image option from the menu. In this example, I selected a fish image, fitting, since it's for a Floating Calendar. After selecting the image, right-click the button again to bring up the menu and enter
&Calendar
the Name field. You can choose the button style as Image only, Text only, or Image and Text. The default style (Image and Text) is suitable for now, so you can keep that selected. Next, we need to link our
Calendar()
program to the toolbar button. Right-click on the toolbar button and select Properties from the context menu. In the On Action field, type:(Be sure to include the equal sign at the beginning.)
Click Close to save the changes.
Our toolbar button is now ready and can be used to launch the calendar on the form. If that’s all you need, you can stop here. However, we’ll take it a step further by adding a copy of this button to the form view’s shortcut menu. This will allow users to simply right-click on a date field and select the calendar option from the shortcut menu, making it even more convenient. The calendar remains hidden until the button is clicked.
- Select the Toolbars Tab on the Customize Dialogue Control and put a tick-mark for Shortcut Menus.
Before copying the button, let’s first locate the target area where we’ll place it. In the Shortcut Menus options, locate and expand the Form section. You’ll see a long list of sub-menu groups—find and select Form View Control. This group contains standard options like Filter by Selection, Cut, Copy, Paste, and more. These are the default items that appear when you right-click on a text control in Form View. Refer to the image below for guidance.
The shortcut menus vary depending on where you right-click—whether on an empty area of the form, the record selector, or a control. Therefore, it's essential to place a copy of the new button specifically on the Form View Control shortcut menu, which appears when you right-click on a text control in Form View.
Be aware that this method has a few side effects. For instance, if you click the new toolbar button on a form that doesn’t contain the Calendar control, or if the control isn't named
Cal1
You may encounter error messages.We have more articles that explore this topic in greater detail, including how to design custom Microsoft Access menubars, toolbars, and shortcut menus—and how to use them effectively on forms. Relevant links are provided at the end of this page for your reference.
Now, let’s proceed with placing the button. Hold down the Ctrl key with one hand, then click and drag the new toolbar button with the mouse. (Make sure to keep the Ctrl key pressed—otherwise, the button will be moved rather than copied.)
While dragging, hover over the Form menu in the Shortcut Menu Bar to reveal its submenu groups. Then move the pointer to Form View Control to display its shortcut menu options. Drop the button at the left edge of the menu, aligning it with the existing icons for a clean look.
Finally, click Close to exit the customization mode.
Trial Run.
We are ready to try out the Animated Floating Calendar. Open your Access Form in Form View and right-click on a Date Field. The new button on the Shortcut Menu, with the Fish Icon, and the Calendar Caption should be visible as shown below:
If we want to restrict calendar-based date selection to only date-type fields, we’ll need to validate the Control Source of the active control against the field type defined in the underlying table or query. Based on that, we can decide whether to display the calendar or show a warning and prevent it from appearing. While this refinement is possible, it requires additional code. For now, we’ll keep things simple and revisit those enhancements later.
Click on a date in the Calendar to insert it into the field you previously right-clicked. If you decide not to insert a date and simply want to close the Calendar instead, just click on any empty area in the detail section of the form. The Calendar will then disappear.
Normally, the Calendar appears to the right and below the control you right-clicked. If the Date field is located near the bottom or right edge of the form, the Calendar automatically repositions itself to fit within the visible area, typically as close as possible to the field. In such cases, it may slightly overlap the field, but you can still click on the Calendar to insert a date without any issue.
NB: The Program may not work correctly if you attempt to use this method on a Sub-form.
Download

This is a great tool, and with some modification, I was able to make it work in a sub-form scenario.
ReplyDeleteFirst, you will have to give up the ability for a person to manually enter a date. The calendar becomes the only form of entry. Second, (for best results) the calendar should be set to appear in a fixed point, on the screen.
Assuming you can live with the above, then making the following code changes should help make things work on a sub-form.
Step 1: Install the calendar contorl on the main form.
Step 2: The three Private Sub code-sets, listed after step 5, should be added to the Main Form, and not the sub-form.
Step 3: Change the third sub (Form_Load) to read:
Private Sub Form_Load()
[Forms]![mainform]![subform]![Cal1].Value = Date
End Sub
Even though you are selecting a control from a sub-form, that control is considered a member of the main form, and thus triggers the code from the main form.
Step 4: In the code for function Calendar, add the following line before the line "Set CalCtrl = Screen.ActiveForm.Controls("Cal1")". This tells the calendar to always open to the date on the control you selected, rather than the last date entered into the calendar.:
Screen.ActiveForm.Controls("Cal1") = ctl
Step 5: Modify the m_left & m_top paramters to be actual numbers. These are in twips so perhaps 3000 & 800 respectively, would be good starting points. Adjust as needed.
Step 6: Within the code of function Cal1Click() add a line between "DoEvents" and "m_cal.Visible = False" like:
[forms]![formname]![controlname].setfocus
Otherwise the calendar control can't be hidden again...
Also, if you wish to change the size of your calendar, you can modify the twips multiplier (.17 and .105), within the following code. (Code is from Calendar function):
w = CalCtrl.Width + (0.17 * twips) ' 0.17"
h = CalCtrl.Height + (0.105 * twips) ' 0.105"
Hope this helps.
Sharkbyte
Hi,
ReplyDeleteThanks a lot for the good review and suggestions for improvement. I am glad that you like it and using it too.
Regards,
Interesting article for all the .net framework students and learners
ReplyDeletenice to know new and interesting topics like this