Access Cookbook - Ken Getz [196]
' Size to fit the form to expand or contract the form's
' borders to match the visibility of the section.
DoCmd.RunCommand acCmdSizeToFitForm
The function then changes the caption of the button, turns painting back on if it was turned off, and finally moves the focus to the first control of the appropriate section. This last step is not absolutely necessary, but it's a nice touch because the normal tab sequence will not jump across sections. The relevant code is:
' Change the button caption and repaint if necessary.
If Not blnExpanded Then
Me.cmdExpand.Caption = "Basic <<"
Me.Painting = True
Me(acbcFirstAdvancedCtl).SetFocus
Else
Me.cmdExpand.Caption = "Advanced >>"
Me(acbcFirstBasicCtl).SetFocus
End If
You can also apply this technique to non-dialog and bound forms. Although it's not commonly done, there's nothing to stop you from placing bound controls in the footer section of a form. On the other hand, it may be more appropriate to use a tabbed form for bound forms. See the Solution in Recipe 2.5 for more details.
9.9. Use an ActiveX Control
Problem
Access ships with the ActiveX Calendar control. How can you incorporate this and other custom controls into your Access applications?
Solution
ActiveX 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 form
Open 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 form
Add a bound Calendar control to your form
Follow these steps to add the Calendar control to an existing form to replace a text box for selecting dates:
Create a form (or edit an existing one) bound to a table that has a date/time field formatted as a date without time.
Select Insert → ActiveX Control. The Insert ActiveX Control dialog will appear, as shown in Figure 9-29. (The list of available controls that appear on your screen will likely differ from the list displayed here.)
Select the Calendar control and click OK to close the dialog. Move and resize the control as needed. On the frmAppointment1 form, we resized the control to a width of 2.375" and a height of 1.8333".
Figure 9-29. The Insert ActiveX Control dialog
Set the control's ControlSource property to point to the date field in the underlying record source for the form (see Figure 9-30).
Figure 9-30. The calendar control can be directly bound to a field
Right-click anywhere on the embedded custom control to display its shortcut menu. Select Calendar Control Object → Properties from the shortcut menu, and the Calendar control properties sheet will appear (see Figure 9-31).
Use this to customize the various properties of the control. For example, we changed the properties shown in Table 9-7