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.
****************************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).
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 |
Property Tab to Select | Attribute | Setting |
Format | Visible | No |
Other | Name | calendar_1 |
' 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) |
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.