Sidebar


Interacting with ActiveX

By Erin M. Carroll

The Parrothead Club of New Hampshire is a nonprofit organization that participates in and organizes events to benefit the community and the environment. In a recent project, I created a page for their site that allows the club to schedule events, meetings, and outings using a calendar. Knowing that VBScript can be used to configure and control ActiveX components, I found a calendar control in Microsoft's ActiveX Gallery that fit well with my plan for the Parrothead site.

Microsoft's ActiveX calendar control displays the dates of an entire month, with a few days from the previous and next months distinguished in gray. The user can select which month and year to display through combo boxes. The programmer can use VBScript to access and run the control's properties and methods when an event such as a click occurs. If something is scheduled for a day on which a member clicks, a message box will pop up with information about the scheduled event. Club events are also summarized in text, which is more practical for rescheduled events.

Details

Getting the calendar control into the HTML document is very easy. I installed the controls from Microsoft's ActiveX Gallery and then embedded the Calendar object in the HTML code using the syntax shown in Listing One. The <OBJECT> tags identify the portion of the HTML code where the object will be described. Calendar1 is the ActiveX calendar control I use to reference the calendar in the VBScript code. The control's initial properties may be set using the PARAM tags.

Embedding the VBScript code into the document is also straightforward. It doesn't really matter where the VBScript code goes within the HTML document, unless the code is going to interact with the page while it is being set up, in which case the VBScript code must be placed at the beginning of the document. I found it more convenient to keep all of the VBScript code together at the beginning of the HTML document. The VBScript code is placed within <SCRIPT> and </SCRIPT> tags, and comments are included to avoid confusion in nonVBScript-aware browsers. Within the identified block, you can declare and use any Visual Basic functions and subroutines.

Listing Two shows the script I created to manipulate the Calendar control. When the calendar receives a double-click message, a message box is displayed, informing the user that clicking on a specific day will show if an event is scheduled for that day. Click messages are trapped, and several VBScript subroutines may be executed to determine the day, month, and year being clicked on and whether an event is scheduled for it.

The subroutine that traps the click event is Calendar1_Click, which is invoked when the calendar is clicked. It first checks the year selected for the calendar. The CurrentYear function returns the current year, which is compared to the year selected for the calendar control. If the year selected is less than the current year, a "Past events cannot be attended" message is displayed. If the year passes validation, the month selected in the calendar control is determined using the Select Case statement on Calendar1.Month. It is valid if the month is in the current year, but is either before or after the current month. You can show calendar events within the current year, even if the event has passed.

What Next?

In the future, I plan to enhance the Parrothead site by enabling online membership sign-up using forms validated by VBScript functions. The possibilities are limited only by time and, for the moment, by browser support.