3 4
Forms have a great many properties, which are divided into five tabs on a form’s properties sheet, as shown in Figure 5-40.
Figure 5-40. A form’s properties sheet divides form properties into five tabs.
Three of the tabs (Format, Data, and Other) list subsets of the form’s properties. The All tab lists all the form’s properties in a single list. The Event tab lists the form’s events, which are used for writing VBA code.
The same properties sheet displays the properties of the entire form, a form section, or a control on the form. To open a properties sheet, click the Properties button on the Form Design toolbar (see Figure 5-41), press Alt+Enter, or press F4, which is a new hot key in Access 2002.
Figure 5-41. You can open a form’s properties sheet by clicking the Properties button on the Form Design toolbar.
note
tip
Once the properties sheet is open, you can use the following techniques to display the appropriate information:
Figure 5-42. You can display form properties in the properties sheet.
Figure 5-43. You can display form header properties in the properties sheet.
All forms have a detail section. Forms can also have a form header and form footer or a page header and page footer. Headers and footers are paired sections; you can’t have a page header without a page footer, for example, although you can keep a section from being displayed by shrinking it to zero height. To add form header and form footer sections to a form, choose View, Form Header/Footer, as shown in Figure 5-44. The Page Header/Footer option on the same menu toggles a form’s page header and page footer sections.
Figure 5-44. You can choose to turn on and off a form’s header and footer sections.
caution
Form header and footer sections are quite useful. You can place controls such as record selector combo boxes or command buttons in these sections to perform actions that apply to the entire form. In the earliest versions of Access (before Access 2.0), page headers and footers were also useful when multipage forms were used to display more data than could be displayed at one time. However, multipage forms have not been useful (except for very unusual situations) since the tab control was introduced in Access 2.0, as the Tab control is a more efficient way of displaying large amounts of data on a form.
Figure 5-45 shows the primary form in the Crafts database, shrunk vertically to show only the form header and footer sections. The form header includes a record selector combo box (actually, two combo boxes; only one is visible at a time) and two command buttons, which are used to display one combo box or the other.
Figure 5-45. Only the header and footer sections of this form are visible.
tip
The form footer section has two command buttons: one to go to a new record (it could also be used to pop up a New Book form), and another to close the form and return to the main menu. The event procedures attached to the controls in this form’s header and footer sections are listed here:
Private Sub cboAuthorSearchList_AfterUpdate() On Error GoTo ErrorHandler strSearch = "[BookID] = " & Me![cboAuthorSearchList].Column(2) Debug.Print strSearch 'Find the record that matches the control. Me.Requery Me.RecordsetClone.FindFirst strSearch Me.Bookmark = Me.RecordsetClone.Bookmark ErrorHandlerExit: Exit Sub ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " _ & Err.Description Resume ErrorHandlerExit End Sub Private Sub cboTitleSearchList_AfterUpdate() On Error GoTo ErrorHandler strSearch = "[BookID] = " & Me![cboTitleSearchList].Column(1) Debug.Print strSearch 'Find the record that matches the control. Me.Requery Me.RecordsetClone.FindFirst strSearch Me.Bookmark = Me.RecordsetClone.Bookmark ErrorHandlerExit: Exit Sub ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " _ & Err.Description Resume ErrorHandlerExit End Sub Private Sub cmdSearchbyAuthor_Click() On Error GoTo ErrorHandler Me![cboTitleSearchList].Visible = False With Me![cboAuthorSearchList] .Visible = True .SetFocus .Dropdown End With ErrorHandlerExit: Exit Sub ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " _ & Err.Description Resume ErrorHandlerExit End Sub Private Sub cmdSearchbyTitle_Click() On Error GoTo ErrorHandler Me![cboAuthorSearchList].Visible = False With Me![cboTitleSearchList] .Visible = True .SetFocus .Dropdown End With ErrorHandlerExit: Exit Sub ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " _ & Err.Description Resume ErrorHandlerExit End Sub Private Sub cmdNew_Click() On Error GoTo ErrorHandler DoCmd.GoToRecord , , acNewRec Me![txtTitle].SetFocus ErrorHandlerExit: Exit Sub ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " _ & Err.Description Resume ErrorHandlerExit End Sub Private Sub cmdClose_Click() On Error GoTo ErrorHandler Dim dbs As Object Set dbs = Application.CurrentProject If dbs.AllForms("fmnuMain").IsLoaded Then Forms![fmnuMain].Visible = True Else DoCmd.OpenForm "fmnuMain" End If DoCmd.Close acForm, Me.Name ErrorHandlerExit: Exit Sub ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " _ & Err.Description Resume ErrorHandlerExit End Sub
See Chapter 20, "Customizing Your Database Using VBA Code," for more information about writing code behind forms procedures.
The properties displayed on the Format tab of a form’s properties sheet are related to the appearance of the form. Some of the more frequently changed properties are described in the following list. In most cases, the remaining properties can be left at their default values.
Figure 5-46. This form’s record selector is turned on.
Navigation Buttons. This property turns on and off the navigation bar at the bottom of the form.
Control Box, Min/Max Buttons, Close Button, What’s This Button. These properties control the small buttons in the upper left and right corners of a form.
tip
The following properties are the most frequently used properties on the Data tab of a form’s properties sheet:
The following properties are the most frequently used properties on the Other tab of a form’s properties sheet:
See Chapter 13, "Customizing Access Toolbars and Menus (Command Bars)," for more details on command bars.
Conditional formatting for form controls is a long-requested feature that was added in Access 2000. This feature lets you apply a specific color, a special effect, or another formatting choice to a control in certain circumstances, by means of a user-friendly dialog box. (You don’t have to write code, as you did with Access 97 and earlier.) For example, if you have a txtDue control bound to a Date field and you want the control to have a yellow background if the due date is earlier than today, you can select the control in Design view and choose Format, Conditional Formatting (or right-click the control, and choose Conditional Formatting from the shortcut menu) to open the Conditional Formatting dialog box, as shown in Figure 5-47.
Figure 5-47. The Conditional Formatting dialog box enables you to specify formatting that appears when certain conditions are met.
note
You can construct up to three conditions for formatting a control. Each condition has three parts: the formatting element, the comparison phrase, and the comparison expression.
The Formatting Element drop-down list provides the following three choices:
The Comparison Phrase drop-down list provides eight choices:
caution
To complete the condition, type a constant value or an expression in the text box. (If you’re using either Between or Not Between as the comparison phrase, specify two values or expressions.)
Figure 5-48 shows the Conditional Formatting dialog box with Field Value Is selected for the formatting element, Less Than selected for the comparison operator, and the Date function specified for the constant value. A yellow background color and bold formatting are selected as well.
Figure 5-48. The condition in this Conditional Formatting dialog box is complete.
Figure 5-49 shows the sample form in Form view, displaying a record for which the due date is earlier than today.
Figure 5-49. The conditional formatting has been applied to this form, shown in Form view.
Sometimes you’ll want to clarify which control has the focus by giving the current control a different background color. You can do this by selecting Field Has Focus for the formatting element (the other selections will disappear), as shown in Figure 5-50. You must set the same condition on all controls on the form to get the desired effect (or at least set the condition on all controls that display text, such as text boxes and combo boxes).
Figure 5-50. You can create a condition to clarify which field has the focus.
note
After applying this condition, as you tab from control to control in the form, the current control will appear in boldface and with a yellow background.