[ Team LiB ] |
Recipe 9.9 Use an ActiveX Control9.9.1 ProblemAccess ships with the ActiveX Calendar control. How can you incorporate this and other custom controls into your Access applications? 9.9.2 SolutionActiveX controls are not as commonly used in Access as they are in development environments such as Visual Basic, and some controls that work in other environments don't work well in Access. However, a number of controls have been created to work well in Access, and Microsoft ships one such control with the product: a very useful Calendar control. This solution shows you how to use the Calendar control in both bound and unbound modes. You'll also learn how to create a general-purpose reusable pop-up calendar form. Load the 09-09.MDB database and open frmAppointment1 in form view (see Figure 9-27). Create a new record, selecting a date by using the Calendar control's Month and Year combo box controls to navigate to the desired month and then clicking on the date on the calendar. Complete the rest of the record and close the form. Now open the tblAppointment table to verify that the date you selected was stored in the ApptDate field of that record. Figure 9-27. The frmAppointment1 formOpen frmAppointment2 in form view and select a date by clicking on the calendar button to the right of the ApptDate text box. A pop-up form will be displayed, where you can select a date again using the Calendar control (see Figure 9-28). Double-click on a date to select it and close the calendar pop-up form, or click once on a date and use the OK button. You may also wish to experiment with the Go to Today button, the Month and Year navigation buttons, and the Cancel button. Figure 9-28. Selecting a date using the frmPopupCal form9.9.2.1 Add a bound Calendar control to your formFollow these steps to add the Calendar control to an existing form to replace a text box for selecting dates:
Figure 9-29. The Insert ActiveX Control dialog
Figure 9-30. The calendar control can be directly bound to a field
Figure 9-31. The custom properties sheet for the Calendar control
9.9.2.2 Create a generic unbound pop-up calendar formFollow these steps to create a generic unbound pop-up calendar form:
9.9.3 DiscussionYou insert a custom control into an Access form using the Insert Custom Control command. The control can then be moved and resized as necessary. When you insert a custom control into an Access form, Access merges the properties of the control's container (a bound or unbound OLE frame control) with the properties of the custom control. The custom control's unique properties are placed on the Other tab of the control's regular properties sheet, but you can also manipulate these properties using the custom properties sheet created by the control's creator. You do this by right-clicking on the control and selecting Calendar Control Object Properties from the shortcut menu.
In Step 3 of adding a bound Calendar control, you bound the Calendar control directly to a field in the form's underlying record source. In the steps for creating a generic unbound pop-up calendar form, you created code that manipulated five different methods of the Calendar control: PreviousYear, NextYear, PreviousMonth, NextMonth, and Today. For example, in the event procedure attached to cmdPreviousMonth, you added the following line of code: Me.ocxCal.PreviousMonth
The frmPopupCal form contains two special procedures, called property procedures, that you may not have seen before. Using property procedures, you can create custom properties for a form that can be called from outside the form. This allows you to expose certain elements of the form to the outer world while keeping all of the form's controls and procedures—the form's inner workings—encapsulated within the form. The Let property procedure creates a user-defined property for the form, controlling what happens when a calling routine sets the value of the form's property. The Get property procedure controls what happens when a calling routine requests the value of the property. The property procedure for frmPopupCal is simple, consisting of only an assignment statement, but you can do anything in a property procedure that you could do in a normal event procedure. For example, you can count the number of text box controls on a form in a Get property procedure, or you can set all the labels on a form to a certain color in a Let property procedure. The Solution in Recipe 9.10 contains examples of more complex property procedures.
The basCalendar module contains a wrapper function for the frmPopupCal pop-up calendar form. The acbGetDate wrapper function is shown here: Function acbGetDate(varDate As Variant) As Variant Const acbcCalForm = "frmPopupCal" ' Open calendar form in dialog mode, passing it the current ' date using OpenArgs. DoCmd.OpenForm acbcCalForm, WindowMode:=acDialog, OpenArgs:=Nz(varDate) ' Check if the form is open; if so, return the date selected ' in the Calendar control, close the pop-up calendar form, ' and pass the new date back to the control. Otherwise, ' just return Null. If IsOpen(acbcCalForm) Then acbGetDate = Forms(acbcCalForm).CalDate DoCmd.Close acForm, acbcCalForm Else acbGetDate = Null End If End Function acbGetDate sends the calendar a date by using the OpenArgs property of the form (discussed in the Solution in Recipe 9.6) and requests a date from the form by using the CalDate user-defined property created using the Get property procedure. The Load event procedure of frmPopupCal sets the CalDate property to the OpenArgs property. In this case, it's necessary to use the OpenArgs property because you are opening the form in dialog mode, which makes it impossible to manipulate its properties directly. Calling the acbGetDate wrapper function whenever you wish to use the pop-up calendar form to provide a date to your application ensures that you are always going through a single, consistent entry point. Thus, you never need to bother with opening or closing the form or worry about the names of the controls on frmPopupCal. Just use the following syntax to get a date using the pop-up form: variable = acbGetDate(current value) The pop-up calendar's AutoCenter property has been set to Yes so it will always appear in the center of the screen. You may wish to extend acbGetDate with optional left and top parameters so you can precisely position the pop-up calendar form on the screen when it is first opened. The techniques presented in this solution can be applied to other Microsoft and third-party vendor custom controls, including controls that ship as part of the Visual Basic development environment. |
[ Team LiB ] |