Windows programming in general, and Access programming in particular, isn't limited to just writing code. Your ability to design a user-friendly interface can make or break the success of your application. Access and the Windows programming environment offer a variety of controls, and each one is appropriate in different situations. The following sections discuss each type of control, outlining when and how it should be used.
You generally use labels to display information to your users. Attached labels are automatically added to your form when you add other controls, such as text boxes, combo boxes, and so on, and they can be deleted or modified as necessary. Their default captions are based on the Caption property of the field that underlies the control they're attached to. If nothing has been entered into a field's Caption property, the field name is used for the label's caption.
The Label tool, found in the toolbox, can be used to add any text to the form. Click the Label tool, then click and drag the label to place it on the form. Labels are often used to provide a description of the form or to supply instructions to users. Labels can be customized by modifying their font, size, color, and so on. Although developers can use Visual Basic for Applications (VBA) code to modify label properties at runtime, users don't have this ability.
TIP
Sometimes attached labels get detached from their associated text boxes. This means that the label will no longer move, size, and become selected with the text box that it applies to. To re-associate the label with the text box, cut the label (Ctrl+X), click to select the text box, and then press Ctrl+V to paste.
If you purposely want to disassociate a label from its attached control, simply cut the label and then paste it back on the form
without selecting the control that it was attached to. This allows you to perform tasks such as hiding the control without hiding the label.
Text boxes are used to get information from the user. Bound text boxes display and retrieve field information stored in a table; unbound text boxes gather information from the user that's not related to a specific field in a specific record. For example, a text box can be used to gather information about report criteria from a user.
Text boxes are automatically added to a form when you click and drag a field from the field list to the form. The Display control for the field must be set to Text Box. (The Display control is the default control type for an object; this default is set in the design of the underlying table.) Another way to add a text box is to select the Text Box tool from the toolbox, and then click and drag to place the text box on the form. This process adds an unbound text box to the form. If you want to bind the text box to data, you must set its Control Source property.
Combo boxes allow a user to select from a list of appropriate choices. Access offers several easy ways to add a combo box to a form. If a field's Display Control property has been set to Combo Box, a combo box is automatically added to a form when the field is added. The combo box automatically knows the source of its data as well as all its other important properties.
If a field's Display Control property hasn't been set to Combo Box, the easiest way to add a combo box to a form is to use the Control Wizard. When selected, the Control Wizards tool helps you add combo boxes, list boxes, option groups, and subforms to your forms. Although all the properties set by the Combo Box Wizard can be set manually, using the wizard saves both time and energy. If you want the Combo Box Wizard to be launched when you add a combo box to the form, make sure the Control Wizards tool in the toolbox has been clicked (switched on) before you add the combo box.
Select the Combo Box tool in the toolbox and then click and drag to place the combo box on the form. This launches the Combo Box Wizard; its first step is shown in Figure 5.17. You're offered three sources for the combo box's data. Use the first option if your combo box will select the data that's stored in a field, such as the state associated with a particular client. I rarely, if ever, use the second option, which requires that you type the values for the combo box. Populating a combo box this way makes it difficult to maintain. Every time you want to add an entry to the combo box, your application must be modified. The third and final option is appropriate when you want the combo box to be used as a tool to search for a specific record. For example, a combo box can be placed in the form's header to display a list of valid customers. After selecting a customer, the user is then moved to the appropriate record. This option is available only when the form is bound to a record source.
In the second step of the Combo Box Wizard, you select a table or query to populate the combo box. For optimal performance, you should select a query. In the third step, you select the fields that appear in your combo box (see Figure 5.18). The combo box being built in the example will be used to select the client associated with a particular project. Although the CompanyName field will be the only field visible in the combo box, ClientID and CompanyName have both been selected because ClientID is a necessary element of the combo box. After a company name has been selected from the combo box, the client ID associated with the company name will be stored in the ClientID field of the tblProjects table.
The fourth step is new to Access 2003, and allows you to select a sort order for your list (see Figure 5.19). The fifth step lets you specify the width of each field in the combo box. Notice in Figure 5.20 that Access recommends that the key column, ClientID, be hidden. The idea is that the user will see the meaningful English description while Access worries about storing the appropriate key value in the record.
In the wizard's sixth step, you are asked to designate a field in the combo box that uniquely identifies the row (see Figure 5.21). Select a field and click Next. In the wizard's seventh step, specify whether you want Access to simply remember the selected value or store it in a particular field in a table. In the example shown in Figure 5.22, the selected combo box value will be stored in the ClientID field of the tblProjects table.
The eighth and final step of the Combo Box Wizard prompts for the text that will become the attached label for the combo box. Pressing the Finish button completes the process, building the combo box and filling in all its properties with the appropriate values.
Although the Combo Box Wizard is a helpful tool, it's important to understand the properties it sets. Figure 5.23 shows the Properties window for a combo box. Many of the Combo Box properties are covered in other chapters, but take a moment to go over the properties set by the Combo Box Wizard in this example.
The Control Source property indicates the field in which the selected entry is stored. In Chapter 9 covers the advanced aspects of combo boxes.
List boxes are similar to combo boxes, but differ from them in three major ways:
They consume more screen space.
They allow you to select only from the list that's displayed. This means you can't type new values into a list box (as you can with a combo box).
They can be configured to let you select multiple items.
As with a combo box, the Display Control of a field can be set to List Box, and a list box will be added to the form when the field is clicked and dragged from the field list to the form.
The List Box Wizard is almost identical to the Combo Box Wizard. After running the List Box Wizard, the List Box properties affected by the wizard are the same as the Combo Box properties. Advanced list box techniques are covered in Chapter 9.
Check boxes are used when you want to limit your user to entering one of two values, such as Yes/No, True/False, or On/Off. You can add a check box to a form in several ways:
Set the Display Control of the underlying field to Check Box; then click and drag the field from the field list to the form.
Click the Check Box tool in the toolbox, then click and drag a field from the field list to the form. This method adds a check box to the form even if the Display Control of the underlying field isn't a check box.
Click the Check Box tool in the toolbox, then click and drag to add a check box to the form. The check box you have added will be unbound. To bind the check box to data, you must set the control's Control Source property.
TIP
Use the Triple state property of a check box to add a third value, Null, to the possible choices for the check box value.
You can use option buttons and toggle buttons alone or as part of an option group. You can use an option button or toggle button alone to display a True/False value, but this isn't a standard use of an option or toggle button. (Check boxes are standard for this purpose.) As part of an option group, option buttons and toggle buttons force the user to select from a mutually exclusive set of options, such as choosing from American Express, MasterCard, Visa, or Discover for a payment type. This use of option buttons and toggle buttons is covered in the next section, "Option Groups."
The difference between option buttons and toggle buttons is in their appearance. Personally, I find toggle buttons confusing to users. I find that option buttons provide a much more intuitive interface.
Option groups allow the user to select from a mutually exclusive set of options. They can include check boxes, toggle buttons, or option buttons, but the most common implementation of an option group is option buttons.
The easiest way to add an option group to a form is to use the Option Group Wizard. Make sure the Control Wizards button in the toolbox is selected, click Option Group in the toolbox, and then click and drag to add the option group to the form. This launches the Option Group Wizard.
The first step of the Option Group Wizard, shown in Figure 5.24, allows you to type the text associated with each item in the option group. The second step gives you the option of selecting a default choice for the option group. This choice comes into effect when the user adds a new record to the table underlying the form. The third step of the wizard lets you select values associated with each option button (see Figure 5.25). The text displayed with the option button isn't stored in the record; instead, the underlying numeric value is stored in the record. In Figure 5.25, the number 1 is stored in the field if Credit Card is selected.
The fourth step of the Option Group Wizard asks whether you want to remember the option group value for later use or store the value in a field. In Figure 5.26, the option group value is stored in the PaymentMethodID field. In the fifth step, you can select from a variety of styles for the option group buttons, including option buttons, check boxes, and toggle buttons. You can also select from etched, flat, raised, shadowed, or sunken effects for your buttons. The wizard lets you preview each option. The sixth and final step of the wizard allows you to add an appropriate caption to the option group. The completed group of option buttons is shown in Figure 5.27.
It's important to understand that the Option Group Wizard sets properties of the frame, the option buttons within the frame, and the labels attached to the option buttons. The properties of the frame are shown in Figure 5.28. The control source of the frame and the default value of the option group are set by the Option Group Wizard. Each individual option button is assigned a value, and the caption of the attached labels associated with each button is set.