COUNTING THE DAYS WITH THE CALENDAR CONTROL

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.

Figure B.5. The ActiveX Calendar control lets you create visual scheduling applications.


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.

Figure B.6. Being able to link a subform to a Calendar control is very handy.


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.

Understanding the Properties of the Calendar ActiveX 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.

Figure B.7. The Calendar control has many of its own properties.


Table B.1. Important Calendar Controls and Their Functions
Property Description
DayFont and GridFont Set the font for the display of the day titles and grid text (respectively) on the calendar. These two properties actually are made up of other properties, such as Bold, Italic, and so on. When the Builder button is clicked, the Calendar control property sheet appears. When you address the property in code—for example, the ocxCal Calendar control—use the following syntax:
ocxCal.DayFont = NameOfFont
ocxCal.DayFont.Name = NameOfFont
ocxCal.DayFont.Size = SizeOfFont
ocxCal.DayFont.Bold = True|False
ocxCal.DayFont.Italic = True|False
ocxCal.DayFont.Underline = True|False
ocxCal.DayFont.Strikethrough = True|False

The same syntax can be used for GridFont.
DayFontColor and GridFontColor Set the colors for the display of the day titles and the grid, respectively.
ShowDateSelectors When set to True, displays combo boxes on the top of the calendar, allowing users to pick the month and year.
ShowTitle When set to True, displays the month and year in whatever font and color are set by TitleFont and TitleFontColor.
DayLength Lets you specify how to display the Day title. For example, for Monday, Short would be M, Medium would be Mon, and Long would be Monday.
MonthLength Affects the length displayed for the month in the title. For example, for August, Short would be Aug and Long would be August.
GridLinesFormat Specifies the format for the gridlines: Raised, Sunken, or Flat.
GridLinesColor Specifies the color of the lines when GridLinesFormat is set to Flat.
Day, Month, and Year When incremented or decremented by one, allow you to cycle through a week, a month, or a year. These properties are actually most useful to manipulate during runtime.
Value Retrieves the date highlighted or moves the highlight on the calendar when set.
ValueIsNull Allows you to not have a date chosen on the calendar.

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.

Understanding the Methods of the Calendar ActiveX Control

The Calendar control actually uses a limited number of methods, all listed in Table B.2.

Table B.2. Calendar Methods and Their Functions
Method Description
AboutBox Displays the About box for the Calendar control
NextDay Increments the Value property by a day, taking care of the calendar fixup for months and years (calendar fixup means that the calendar graphic will show the correct month and year)
NextWeek Increments the Value property by a week, taking care of the calendar fixup for years
NextMonth Increments the Value property by a month, taking care of the calendar fixup for years
NextYear Increments the Value property by a year
PreviousDay Decrements the Value property by a day, taking care of the calendar fixup for months and years
PreviousWeek Decrements the Value property by a week, taking care of the calendar fixup for years
PreviousMonth Decrements the Value property by a month, taking care of the calendar fixup for years
PreviousYear Decrements the Value property by a year
Refresh Repaints the Calendar control
Today Sets the Value property to the current system date

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.
Open the frmNewReleases form in Design mode.

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.

Understanding the Calendar ActiveX Control's Events

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.

Programming VBA with the Calendar Control

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.

Listing B.1. AppB.mdb: Updating ocxCal with the Date of the Title Found
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.

Figure B.8. It's easy to perform impressive tasks with very few steps by using the Calendar control.


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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.68.14