The Calendar control (ocxCal) presents a graphical representation of a calendar. In Figure B.5, the Calendar control used in the World Wide Video Application allows users to view when new releases are due.
The frmNewReleases form not only displays movies for the different days as users move through the calendar, but it also allows users to enter the name of the new release in which they're interested. The system then looks up the title in the tblNewReleases table and positions the calendar on the date the title is due to be released.
Tip
The power behind this form is that the Calendar control's value is a date and is actually used for the Link Master Fields property for the frmNewReleasesSubform subform. This makes it easy to show tasks or activities for a given date. Value is the default property, or the one bound to the underlying table field.
Figure B.6 shows the property sheet for the subform linked to the Calendar control. You can find the frmNewReleases form in AppB.mdb, which is on the CD-ROM in the ExamplesAppB folder.
Before going into the sample code found in AppB.mdb, let's look at some of the properties, methods, and events found on the Calendar control.
Throughout this appendix and Chapter 14, only select properties, methods, and events are mentioned for each ActiveX control because there are too many to cover completely in two chapters. The properties, methods, and events examined here are those deemed more important—or more likely to be interesting—to you. For complete listings of the properties, methods, and events for each ActiveX control, examine each control's help file.
Table B.1 lists some of more useful properties of the Calendar control. You can follow this table by referring to the property sheet shown in Figure B.7. The next two sections list the methods and events used within the Calendar control.
Tip
When set to True, the ShowDateSelectors property is useful as the title. However, you then need to set the ShowTitle property to False to avoid redundancy. If you don't want users to be able to choose the year and month, set ShowDateSelectors to False and ShowTitle to True.
A few more properties are available for use with the Calendar control. For a complete list of properties for the Calendar control, look in the Object Browser. Follow these steps:
1. |
Open the VBE via Tools, Macro, Visual Basic Editor and choose View, Object Browser. |
2. |
Select MSACAL for the library to examine. |
3. |
Select the Calendar class from the list of classes. |
The Calendar control actually uses a limited number of methods, all listed in Table B.2.
Note
When used, all methods except AboutBox cause the calendar to repaint.
When you use the Calendar control, forms that allow users to move around on the calendar will use most of these methods.
On the frmNewReleases form, many of the methods listed here are used in event procedures. Four of the methods—PreviousDay, NextDay, PreviousMonth, and NextMonth—are used with command buttons. To see an example of using the PreviousDay method, follow these steps:
1. | |
2. |
Choose Code from the View menu. The module editor appears with the Declarations section of the frmNewReleases form module displayed. |
3. |
From the Objects drop-down list, choose the cmdPreviousDay command button. The procedure, cmdPreviousDay_Click(), appears in the module editor, as follows: Private Sub cmdPreviousDay_Click() Me!ocxCal.PreviousDay End Sub |
By viewing a few of the command buttons' OnClick events on the frmNewReleases form, you also can see examples of the other methods. The following examples show the command buttons that use the Next methods:
Private Sub cmdNextDay_Click() Me!ocxCal.NextDay End Sub Private Sub cmdNextMonth_Click() Me!ocxCal.NextMonth End Sub
You can see by these examples, and when you run the form, that the Calendar control does a lot of the work by fixing up the months and years, thus repainting itself.
The Calendar control supports these standard Access events: Click, DblClick, GotFocus, KeyDown, KeyPress, KeyUp, LostFocus, BeforeUpdate, and AfterUpdate. These events are triggered the same way other events are triggered. Two events specific to the Calendar control, NewMonth and NewYear, are triggered when the date in a new month or new year is clicked.
Some VBA code has already been shown with the events displayed. Another example of programming the Calendar control is the New Title to Find text box on the bottom of the frmNewReleases form. The actual manipulation of the Calendar control ocxCal is performed in this single line of code:
Me!ocxCal.Value = snpNewReleases!DateDueOut
Listing B.1 shows the complete code attached to the txtTitleToFind text box.
Private Sub txtTitleToFind_AfterUpdate() Dim dbLocal As Database, snpNewReleases As Recordset On Error GoTo Error_txtTitleToFind_AfterUpdate Set dbLocal = CurrentDb() Set snpNewReleases = dbLocal.OpenRecordset("tblNewReleases", _ dbOpenSnapshot) snpNewReleases.FindFirst "[Title] = '" & txtTitleToFind & "'" If snpNewReleases.NoMatch Then Beep MsgBox "New Title not found!", acCritical, "Find Title Error" Else Me!ocxCal.Value = snpNewReleases!DateDueOut End If snpNewReleases.Close dbLocal.Close Exit_txtTitleToFind_AfterUpdate: Exit Sub Error_txtTitleToFind_AfterUpdate: MsgBox Error$ Resume Exit_txtTitleToFind_AfterUpdate End Sub |
The code in Listing B.1 performs these steps:
1. |
The routine opens the current database. |
2. |
It opens the tblNewReleases table as a snapshot recordset. |
3. |
The routine looks for the text entered in txtTitleToFind. |
4. |
It displays an error message if the text isn't found; otherwise, it sets the Value property of the Calendar control ocxCal to the DateDueOut field for the title. |
5. |
It closes the variables. |
Figure B.8 shows the frmNewReleases form just after a title is found.
You can use the Calendar control in several other ways. This is just one simple example, although again a powerful example because you can control a subform with it by using very little programming.
The simple-to-use Calendar control allows for adding a unique feature to your forms. Another ActiveX control that greatly enhances applications is the Common Dialog ActiveX control.
3.17.68.14