Forms provide the most flexible way for viewing, adding, editing, and deleting your data. They are also used for switchboards (forms with buttons that provide navigation), dialog boxes that control the flow of the system, and the o displaying messages. Controls are the objects on forms such as labels, text boxes, buttons, and many others. In this chapter, you learn how to create different types of forms and get an understanding about the types of controls that are used on a form.
In this chapter, you use tblProducts in the Chapter07.accdb database to provide the data necessary to create the examples used in this chapter.
Use the Form group in the Create ribbon to add forms to your database. The commands in the Form group—shown in Figure 7-1—let you create the following different types of forms:
• Form: Creates a new form that lets you enter information for one record at a time. You must have a table, query, form, or report open or selected to use this command.
• Split Form: Creates a split form that shows a datasheet in the upper section and a form in the lower section for entering information about the record selected in the datasheet.
• Multiple Items: Creates a form that shows multiple records in a datasheet, with one record per row.
• PivotChart: Instantly creates a PivotChart form.
• Blank Form: Instantly creates a blank form with no controls.
• More Forms: This drop-down list lets you start the Form Wizard or instantly create a Datasheet, Modal Dialog, or PivotTable.
• Form Design: Creates a new blank form and displays it in Design View.
Figure 7-1
Use the Form command in the ribbon’s Form group to create a new form based on a table or query selected in the Navigation Pane. To create a form based on tblProducts, follow these steps:
1. Select tblProducts in the Navigation Pane.
2. Click the Create tab on the ribbon, and then click on the Form command in the Form group.
Access creates a new form containing all the fields from tblProducts displayed in Layout View, shown in Figure 7-2. Layout View lets you see the forms data while changing the layout of controls on the form.
Figure 7-2
Use the Split Form command in the ribbon’s Form group to create a split form based on a table or query selected in the Navigation Pane. This new feature gives you two views of the data at the same time, letting you select a record from a datasheet in the upper section and edit the information in a form in the lower section. To create a split form based on tblProducts, follow these steps:
1. Select tblProducts in the Navigation Pane.
2. Click the Create tab on the ribbon, and then click on the Split Form command in the Form group.
Access creates a new split form based on tblProducts displayed in Layout View, shown in Figure 7-3. Resize the form and use the splitter bar in the middle to make the lower section completely visible.
Figure 7-3
Use the Multiple Items command in the ribbon’s Form group to create a multiple-items form based on a table or query selected in the Navigation Pane. This new feature creates a form that looks like a datasheet, but it lets you add graphical elements, buttons, and other controls. To create a multiple-items form based on tblProducts, follow these steps:
1. Select tblProducts in the Navigation Pane.
2. Click the Create tab on the ribbon, and then click on the Multiple Items command in the Form group.
Access creates a new multiple items form based on tblProducts displayed in Layout View, shown in Figure 7-4. Although the form looks similar to a datasheet, you can only resize the rows and columns in Design View and Layout View.
Figure 7-4
Use the Form Wizard command in the Form group’s More Forms drop-down list to create a form using a wizard. The Form Wizard visually walks you through a series of questions about the form that you want to create and then creates it for you automatically. The Form Wizard lets you select which fields you want on the form, the layout (Columnar, Tabular, Datasheet, Justified) of the form, the style (Access 2003, Access 2007, Apex, and so on), and the title on the form.
To start the Form Wizard based on tblProducts, follow these steps:
1. Select tblProducts in the Navigation Pane.
2. Click the Create tab on the ribbon, and then click on the Form group’s More Forms drop-down and select Form Wizard.
Access starts the Form Wizard shown in Figure 7-5. Choose which table or query you want the form based on using the Tables/Queries drop-down list. Use the buttons in the middle of the form to add and remove fields to the Available Fields and Selected Fields list boxes.
Figure 7-5
You can also double-click any field in the Available Fields list box to add it to the Selected Fields list box.
The series of buttons at the bottom of the form let you navigate through the other steps of the wizard. The types of buttons available here are common to most wizard dialog boxes:
• Cancel: Cancel the wizard without creating a form
• Back: Return to the preceding step of the wizard
• Next: Go to the next step of the wizard
• Finish: End the wizard using the current selections
If you click Next or Finish without selecting any fields, Access tells you that you must select fields for the form before you can continue.
Use the Datasheet command in the Form group’s More Forms drop-down list to create a form that looks like a table or query’s datasheet. A datasheet form is useful when you want to see the data in a row and column format but want to limit which fields are displayed and editable. To create a datasheet form based on tblProducts, follow these steps:
1. Select tblProducts in the Navigation Pane.
2. Click the Create tab on the ribbon, and then click on the Form group’s More Forms drop-down and select Datasheet.
You can view any form you create as a datasheet by selecting Datasheet View from the ribbon’s View drop-down. A datasheet form appears in Datasheet View by default when you open it.
You can prevent users from viewing a form as a datasheet by setting the form’s properties. You’ll learn more about form properties later in this chapter.
Use the Blank Form command in the ribbon’s Form group to create a form without any controls. To create a blank form based on tblProducts, follow these steps:
1. Select tblProducts in the Navigation Pane.
2. Click the Create tab on the ribbon, and then click on the Blank Form command in the Form group.
Access creates a new blank form based on tblProducts displayed in Layout View. In the next section, you’ll learn how to add and customize controls on the form.
Use the Form Design command in the ribbon’s Form group to create a blank form and display it in Design View.
In this section, you’ll learn how to change a form’s design using Design View. You’ll add, move, and resize different controls, as well as customize other aspects of a form.
Click on Design View from the View drop-down in the ribbon’s View group to switch a form to Design View. The Design tab on the Form Tools ribbon—shown in Figure 7-6—lets you add and customize controls on your form.
Figure 7-6
The Controls group on the ribbon’s Design tab replaces the Toolbox from previous versions of Access.
The white area of the form is where you work. This is the size of the form when it is displayed. Resize the white area of the form by placing the cursor on any of the area borders and dragging the border of the area to make it larger or smaller. Figure 7-7 shows a blank form in Design View being resized.
Figure 7-7
You can save the form at any time by clicking on Save in the Quick Access toolbar. When you’re asked for a name for the form, give it a meaningful name (for example, frmProducts, frmCustomers, frmProductList). If you’ve already given the form a name, you won’t be prompted for a name when you click Save.
When you close a form, Access asks you to save it. If you don’t save a form, all changes since you opened the form (or the last time you pressed Save) are lost. You should frequently save the form while you work if you’re satisfied with the results.
If you are going to make extensive changes to a form, you might want to make a copy of the form. If you want to work on the form frmProducts, you can copy and then paste the form in the database window, giving it a name like frmProductsOriginal. Later, when you have completed your changes and tested them, you can delete the original copy.
Controls and properties form the basis of forms and reports. It is critical to understand the fundamental concepts of controls and properties before you begin to apply them to custom forms and reports.
Although this chapter is about forms, you will learn that forms and reports share many common characteristics including controls and what you can do with them. As you learn about controls in this chapter, you will be able to apply nearly everything you learn when you create reports.
The term control has many definitions in Access. Generally, a control is any object on a form or report, such as a label or text box. These are the same controls that you use in any Windows application, such as Access, Excel, or Web-based HTML forms, or those that are used in any language, such as .Net, Visual Basic, C++, or even C#. Although each language or product has different file formats and different properties, a text box in Access is the same as a text box in any other Windows product.
You enter data into controls and display data using controls. A control can be bound to a field in a table (when the value is entered in the control it is also saved in some underlying table field), or it can be unbound and displayed in the form but not saved when the form is closed. A control can also be an object, such as a line or rectangle. Calculated fields are also controls, as are pictures, graphs, option buttons, check boxes, and objects. Some controls that aren’t part of Access are developed separately—these are ActiveX controls. ActiveX controls extend the base feature set of Access 2007 and are available from a variety of vendors. Many ActiveX controls are shipped with Access 2007.
ActiveX controls are covered in Chapter 24.
Whether you’re working with forms or reports, essentially the same process is followed to create and use controls. In this chapter, we explain controls from the perspective of a form.
Forms and reports contain many different control types. You can add these controls to forms using the Controls group shown in Figure 7-6. Hovering the mouse over the control displays a ToolTip telling you what the control is. Table 7-1 briefly describes each control.
The Use Control Wizards command, located on the right side of the Controls group, doesn’t add a control to a form; instead, it determines whether a wizard is automatically activated when you add certain controls. The Option Group, Combo Box, List Box, Subform/Subreport, Bound and Unbound Object Frame, and Command Button controls all have wizards that Access starts when you add a new control. You can also use the ActiveX Controls command (found in the bottom-right corner of the Controls group) to display a list of ActiveX controls, which you can add to Access 2007.
These are the three basic categories of controls:
• Bound controls: These are controls that are bound to a table field. When you enter a value into a bound control, Access automatically updates the table field in the current record. Most of the controls that let you enter information can be bound; these include OLE (Object Linking and Embedding) fields. Controls can be bound to most data types, including text, dates, numbers, Yes/No, pictures, and memo fields.
• Unbound controls: Unbound controls retain the entered value, but they don’t update any table fields. You can use these controls for text label display, for controls such as lines and rectangles, or for holding unbound OLE objects (such as bitmap pictures or your logo) that aren’t stored in a table but on the form itself. Unbound controls are also known as variables or memory variables.
• Calculated controls: Calculated controls are based on expressions, such as functions or calculations. Calculated controls are also unbound because they don’t update table fields. An example of a calculated control is =[SalePrice] - [Cost]. This control calculates the total of two table fields for display on a form but is not bound to any table field.
You add a control to a form in either of two ways:
• Click a button in the Design ribbon’s Controls group and draw a new unbound control on the form.
• Drag a field from the Field List to add a bound control to the form.
A bound control is one that is linked to a table field, while an unbound control is one that is not bound to a table field. A control bound to a table places the data directly into the table by using the form.
By using the buttons in the Controls group to add a control, you decide which type of control to use for each field. The control you add is unbound (or not attached to the data in a table field) and has a default name such as Text21 or Combo11. After you create the control, you decide what table field to bind the control to, enter text for the label, and set any properties. You’ll learn more about setting properties later in this chapter.
You can add one control at a time using the Controls group. To create three different unbound controls, perform these steps:
1. Click the Create tab on the ribbon, and then click on the Form Design command in the Form group to create a new form in Design View.
2. Click the Design tab on the ribbon, and then click the Text Box button (ab|) in the Controls group.
The selected button appears with a colored background.
3. Move the mouse pointer to the Form Design window.
The cursor changes to the Text Box icon.
4. Click and hold down the mouse button where you want the control to begin, and drag the mouse to size the control.
5. Click the Option Button in the Controls group.
6. Move the mouse pointer in the Form Design window.
The cursor changes to the Option Button icon.
7. Click and hold down the mouse button where you want the control to begin, and drag the mouse to size the control.
8. Click the Check Box button in the Controls group.
9. Move the mouse pointer in the Form Design window.
The cursor changes to the Check Box icon.
10. Click and hold down the mouse button where you want the control to begin, and drag the mouse to size the control.
When you’re done, your screen should resemble the one shown in Figure 7-8.
Figure 7-8
Clicking the Form Design window with a control selected creates a default-sized control. If you want to add multiple controls of the same type, double-click on the icon in the Controls group, and then draw as many controls as you want on the form.
The Field List displays a list of fields from the table or query the form is based on. You add bound controls to the form by dragging fields from the Field List onto the form. Select and drag them one at a time, or select multiple fields by using the Ctrl key or Shift key.
• Select multiple contiguous fields by holding down the Shift key and clicking the first and last fields that you want.
• Select multiple noncontiguous fields by holding down the Ctrl key and clicking each field that you want.
Click the Add Existing Fields command in the Design ribbon’s Tools group to display the Field List. By default, the Field List appears docked on the right of the Access window, shown in Figure 7-9. This window is movable and resizable and displays a vertical scroll bar if it contains more fields than can fit in the window.
Figure 7-9
Generally, dragging a field from the Field List window adds a bound text box to the Form Design window. If you drag a Yes/No field from the Field List window, you add a check box. If you drag a field that has a Lookup property, you add a List Box control or Combo Box control. If you drag an OLE field from the Field List window, you create a bound object frame. Optionally, you can select the type of control by selecting a control from the Controls group and dragging the field to the Form Design window.
When you drag fields from the Field List window, the first control is placed where you release the mouse button. Make sure that you have enough space to the left of the control for the labels. If you don’t have sufficient space, the labels slide under the controls.
You gain several distinct advantages by dragging a field from the Field List window:
• The control is bound automatically to the field that you dragged it from.
• Field properties inherit table-level formats, status-bar text, and data-validation rules and messages.
• The label control and label text are created with the field name as the caption.
• The label control is attached to the field control, so they move together.
Select and drag the Description, Category, RetailPrice, and AuctionEndDate fields from the Field List window to the form, as shown in Figure 7-10. Double-clicking a field also adds it to the form.
Figure 7-10
You can see four new controls in the form’s Design View—each one consists of a Label control and a Text Box control (Access attaches the Label control to the text box automatically). You can work with these controls as a group or independently, and you can select, move, resize, or delete them. Notice that each control has a label with a caption matching the field name, and the Text Box control displays the bound field name used in the text box. If you want to resize just the control and not the label, you must work with the two controls separately.
Close the Field List by clicking the Add Existing Fields command in the ribbon’s Tools group or the Close button on the Field List.
In Access, you can change the type of control after you create it; then you can set all the properties for the control. For example, suppose that you add a field as a Text Box control and you want to change it to a List Box. You can right-click the control and select Change To from the pop-up menu to change the control type. However, you can change only from some types of controls to others. You can change anything to a Text Box control; option buttons, toggle buttons, and check boxes are interchangeable, as are List Boxes and Combo Boxes.
In Figure 7-10, notice the difference between the controls that were dragged from the Field List window and the controls that were created from the Controls group. The Field List window controls are bound to a field in tblProducts and are appropriately labeled and named. The controls created from the Controls group are unbound and have default names. The default names are automatically assigned a number according to the type of control.
Later, you learn how to change the control names, captions, and other properties. Using properties speeds the process of naming controls and binding them to specific fields. If you want to see the differences between bound and unbound controls, switch to Form View using the View command in the ribbon’s View group. The Description, Category, RetailPrice, and AuctionEndDate controls display data since they’re bound to tblProducts. The other three controls don’t display data because they aren’t bound to any data source.
If a form’s Record Source property isn’t set, you will not see a Field List window. You’ll learn more about form properties in Chapter 8.
If you first select a control type in the Controls group and then drag a field from the Field List, a control is created (using the selected control type) that is automatically bound to the data field in the Field List.
The deciding factor of whether to use the field list or the Controls group is this: Does the field exist in the table/query or do you want to create an unbound or calculated expression? By using the Field List window and the Controls group together, you can create bound controls of nearly any type. You will find, however, that some data types don’t allow all the control types found in the Controls group. For example, if you select the Chart control type from the Controls group and drag a single field to the form, a text box control is added instead of a chart control.
The following properties always inherit their settings from the field’s table definition:
• Format
• Decimal Places
• Status Bar Text (from the field Description)
• Input Mask
• Default Value
• Validation Rule
• Validation Text
Changes made to a control’s properties don’t affect the field properties in the source table.
Each type of control has a different set of properties, as do objects such as forms, reports, and sections within forms or reports. In the next few chapters, you learn about many of these properties as you use each of the control types to create more complex forms and reports.
After you add a control to the form, you can resize, move, or copy it. The first step is to select one or more controls. Depending on its size, a selected control may show from four to eight handles (small squares called moving and sizing handles) around the control—at the corners and midway along the sides. The Move handle in the upper-left corner is larger than the other handles and you use it to move the control. You use the other handles to size the control. Figure 7-11 displays some selected controls and their moving and sizing handles.
Figure 7-11
The Select command (top leftmost command) in the Controls group must be on for you to select a control. The pointer appears as an arrow pointing diagonally toward the upper-left corner. If you use the Controls group to create a single control, Access automatically reselects the pointer as the default.
Select any single control by clicking anywhere on the control. When you click a control, the handles appear. If the control has an attached label, the Move handle for the label also appears. If you select a label control that is part of an attached control, all the handles for the label control are displayed, and only the Move handle appears in the attached control.
You can select multiple controls in these ways:
• Click each desired control while holding down the Shift key.
• Drag the pointer through or around the controls that you want to select.
• Click and drag in the ruler to select a range of controls.
Figure 7-11 shows selecting the multiple bound controls graphically. When you select multiple controls by dragging the mouse, a rectangle appears as you drag the mouse. Be careful to only drag the rectangle through the controls you want to select. Any control you touch with the rectangle or enclose within it is selected. If you want to select labels only, make sure that the selection rectangle only encloses the labels.
If you find that controls are not selected when the rectangle passes through the control, you may have the Selection behavior global property set to fully enclosed. This means that a control is selected only if the selection rectangle completely encloses the entire control. The normal default for this option is partially enclosed. Change this option by clicking the Microsoft Office Button and selecting Access Options. Then select Object Designers and set the Forms/Reports Selection behavior to Partially Enclosed.
By holding down the Shift key, you can select several noncontiguous controls. This lets you select controls on totally different parts of the screen. Using Shift to select controls is different from using Shift to select files in Windows Explorer and fields in the Field List. You have to Shift+click on each control to add it to the selection.
Click on the form in Design View and then press Ctrl+A to select all the controls on the form. Press Shift and click on any selected control to remove it from the selection.
Deselect a control by clicking an unselected area of the form that doesn’t contain a control. When you do so, the handles disappear from any selected control. Selecting another control also deselects a selected control.
Creating a form is a multistep process. The next step is to make sure that your controls are properly sized and moved to their correct positions. The Arrange tab of the ribbon—shown in Figure 7-12—contains commands used to assist you in manipulating controls.
Figure 7-12
You can resize controls by using any of the smaller handles on the control. The handles in the control corners let you make the field larger or smaller in both width and height—and at the same time. Use the handles in the middle of the control sides to size the control larger or smaller in one direction only. The top and bottom handles control the height of the control; the left and right handles change the control’s width.
When the mouse pointer touches a corner handle of a selected control, the pointer becomes a diagonal double arrow. You can then drag the sizing handle until the control is the desired size. If the mouse pointer touches a side handle in a selected control, the pointer changes to a horizontal or vertical double-headed arrow. Figure 7-13 shows the Description control after being resized. Notice the double-headed arrow in the corner of the Description control.
Figure 7-13
You can resize a control in very small increments by holding the Shift key and pressing the arrow keys. This technique also works with multiple controls selected. Using this technique, a control changes by only 1 pixel at a time (or moves to the nearest grid line if Snap to Grid is selected in the Layout ribbon’s Control Layout group).
When you double-click on any of the sizing handles, Access resizes a control to a best fit for the text in the control. This is especially handy if you increase the font size and then notice that the text is cut off either at the bottom or to the right. For label controls, note that this best-fit sizing adjusts the size vertically and horizontally, though text controls are resized only vertically. This is because when Access is in form-design mode, it can’t predict how much of a field to display—the field name and field contents can be radically different. Sometimes, Access doesn’t correctly resize the label.
The Size group on the Layout ribbon has several commands that help size controls based on the value of the data, the grid, or other controls. Here are the Size commands:
• To Fit: Adjusts control height and width for the font of the text they contain.
• To Tallest: Makes selected controls the height of the tallest selected control.
• To Shortest: Makes selected controls the height of the shortest selected control.
• To Grid: Moves all sides of selected controls in or out to meet the nearest points on the grid.
• To Widest: Makes selected controls the width of the widest selected control.
• To Narrowest: Makes selected controls the height of the narrowest selected control.
You can access many commands by right-clicking after selecting multiple controls. When you right-click on multiple controls, a shortcut menu displays choices to size and align controls.
After you select a control, you can easily move it, using either one of these methods:
• Click on the control and hold the mouse button down; the cursor changes to a four-directional arrow. Drag the mouse to move the control to a new location.
• Click once to select the control and move the mouse over any of the highlighted edges; the cursor changes to a four-directional arrow. Drag the mouse to move the control to a new location.
• Select the control and use the arrow keys on the keyboard to move the control. Using this technique, a control changes by only 1 pixel at a time (or moves to the nearest grid line if Snap to Grid is selected in the Layout ribbon’s Control Layout group).
When an attached label is created automatically with another control, it is called a compound control. If a control has an attached label, the label and control move together; it doesn’t matter whether you click the control or the label.
You can move a control separately from an attached label by pointing to the Move handle of the control and then dragging it. Move the label control separately from the other control by pointing to the Move handle of the label control and dragging it separately.
Figure 7-14 shows a Label control that has been separately moved to the top of the Text Box control. The four-directional arrow cursor indicates that the controls are ready to be moved together. To see this cursor, the control(s) must already be selected.
Figure 7-14
Press Esc before you release the mouse button to cancel a moving or a resizing operation. After a move or resizing operation is complete, click the Undo button on the Quick Access toolbar to undo the changes.
You may want to move several controls so that they are all aligned (lined up). The Layout ribbon’s Control Alignment group has several options described in the following list:
• Left: Aligns the left edge of the selected controls with that of the leftmost selected control.
• Right: Aligns the right edge of the selected controls with that of the rightmost selected control.
• Top: Aligns the top edge of the selected controls with that of the topmost selected control.
• Bottom: Aligns the bottom edge of the selected controls with that of the bottommost selected control.
• To Grid: Aligns the top-left corners of the selected controls to the nearest grid point.
You can align any number of selected controls by selecting a command from the Control Alignment group. When you choose one of the commands, Access uses the control that is the closest to the desired selection as the model for the alignment. For example, suppose that you have three controls and you want to left-align them. They are aligned on the basis of the control farthest to the left in the group of the three controls.
Figure 7-15 shows several sets of controls. The first set of controls is not aligned. The Label controls in the second set of controls have been left-aligned. The Text Box controls in the second set have been right-aligned. Each label, along with its attached text box, has been bottom-aligned.
Figure 7-15
Each type of alignment must be done separately. In this example, you can left-align all the labels or right-align all the text boxes at once. However, you must bottom-align each label and its text control separately (three separate alignments).
The series of dots in the background of Figure 7-15 is the grid. The grid can assist you in aligning controls. Hide or display the grid by selecting the Show Grid command from the Layout ribbon’s Show/Hide group. You can also hide or display the ruler using the Ruler command in the Show/Hide group.
Use the Snap to Grid command in the Layout ribbon’s Control Layout group to align new controls to the grid as you draw or place them on a form. It also aligns existing controls to the grid when you move or resize them. Snap to Grid is on when it appears selected in the ribbon.
When Snap to Grid is on and you draw a new control by clicking on the form and dragging to size the control, Access aligns the four corners of the control to points on the grid. When you place a new control by clicking the control in the Field List and then dragging it to the form, only the upper-left corner is aligned.
As you move or resize existing controls, Access 2007 lets you move only from grid point to grid point. When Snap to Grid is off, Access 2007 ignores the grid and lets you place a control anywhere on the form or report.
You can temporarily turn Snap to Grid off by pressing the Ctrl key before you create a control (or while sizing or moving it). You can change the grid’s fineness (number of dots) from form to form by using the Grid X and Grid Y Form properties. (Higher numbers indicate greater fineness.) You’ll learn more about Form properties in Chapter 8.
The Layout ribbon’s Position group contains commands to adjust the space between multiple controls. These commands change the space between controls on the basis of the space between the first two selected controls. If the controls are across the screen, use horizontal spacing. If they are down the screen, use vertical spacing. These commands are as follows:
• Equal Horizontal: Makes the horizontal space between selected controls equal. You must select three or more controls for this command to work.
• Increase Horizontal: Increases the horizontal space between selected controls by one grid unit.
• Decrease Horizontal: Decreases the horizontal space between selected controls by one grid unit.
• Equal Vertical: Makes the vertical space between selected controls equal. You must select three or more controls for this command to work.
• Increase Vertical: Increases the vertical space between selected controls by one grid unit.
• Decrease Vertical: Decreases the vertical space between selected controls by one grid unit.
Aligning controls aligns the control boxes only. If you want to align the text within the controls (also known as justifying the text), you must use the Design ribbon’s Font group and select the Left, Right, or Center commands.
To modify the appearance of a control, select the control and click on commands that modify that control, such as commands in the Font group or Controls group. To change the text color and font of the Description label, follow these steps:
1. Click Description label on the form.
2. In the Design ribbon’s Font group, change the Font Size to 14, click the Bold command, and change the Font Color to blue.
3. Resize the Description label so the larger text fits (remember, you can double-click any of the sizing handles to autosize the label).
To modify the appearance of multiple controls at once, select the controls and click on commands to modify the controls, such as commands in the Font group or Controls group. To change the text color and font of the Description, Category, and Cost labels and text boxes, follow these steps:
1. Select the three labels and three text boxes by dragging a selection box through them (refer to Figure 7-11).
2. In the Design ribbon’s Font group, change the Font Size to 14, click the Bold command, and change the Font Color to blue.
3. Resize the labels and text boxes so the larger text fits (remember, you can double-click any of the sizing handles to autosize the controls).
As you click the commands, the controls’ appearances change to reflect the new selections (shown in Figure 7-16). The fonts in each control increase in size, become bold, and turn blue. Any changes you make apply to all selected controls.
Figure 7-16
When multiple controls are selected, you can also move the selected controls together. When the cursor changes to the four-directional arrow, click and drag to move the selected controls. You can also change the size of all the controls at once by resizing one of the controls in the selection. All the selected controls increase or decrease by the same number of units.
If you routinely change properties of multiple controls, you may want to group them together. To group controls together, select the controls by holding down the Shift key and clicking them or dragging the selection box through them. After the desired controls are selected, select the Group command from the Layout ribbon’s Control Layout group. A box appears around the selected controls, as shown in Figure 7-17, indicating they’re grouped together.
Figure 7-17
After you’ve grouped the controls together, whenever you click any of the controls inside the group, the entire group is selected. Double-click on a control to select just that one control. After a single control in the group is selected, you can click on any other control to select it.
To resize the entire group, put your mouse on the side you want to resize. After the double arrow appears, click and drag until you reach the desired size. Every control in the group changes in size. To move the entire group, click and drag the group to its new location. With grouped controls, you don’t have to select all the controls every time you change something about them.
To remove a group, select the group by clicking any field inside the group, then select the Ungroup command from the Layout ribbon’s Control Layout group.
You can delete a control by simply selecting it in the form’s Design View and pressing the Delete key on your keyboard. The control and any attached labels will disappear. You can bring them back by immediately selecting Undo from the Quick Access toolbar. You can also select Cut from the Home ribbon’s Clipboard group or Delete from the Home ribbon’s Records group.
You can delete more than one control at a time by selecting multiple controls and pressing Delete. You can delete an entire group of controls by selecting the group and pressing Delete. If you have a control with an attached label, you can delete only the label by clicking the label itself and then selecting one of the delete methods. If you select the control, both the control and the label are deleted. To delete only the label of the Description control, follow the next set of steps (this example assumes that you have the Description text box control in your Form Design window):
1. Select the Description label control only.
2. Press Delete to remove the label from the form.
If you accidentally delete a label from a control, you can reattach it. To create and then reattach a label to a control, follow these steps:
1. Click the Label button on the Controls group.
2. Place the mouse pointer in the Form Design window.
The mouse pointer becomes the Text Box button.
3. Click and hold down the mouse button where you want the control to begin; drag the mouse to size the control.
4. Type Description: and click outside the control.
5. Select the Description label control.
6. Select Cut from the Home ribbon’s Clipboard group.
7. Select the Description text box control.
8. Select Paste from the Home ribbon’s Clipboard group to attach the label control to the text box control.
Another way to attach a label to a control is to click the informational icon next to the label, shown in Figure 7-18. This informational icon lets you know that this label is unassociated with a control. Click the Associate Label with a Control command from the menu, and then select the control you want to associate the label with.
Figure 7-18
You can create copies of any control by copying it to the Clipboard and then pasting the copies where you want them. If you have a control for which you have entered many properties or specified a certain format, you can copy it and revise only the properties (such as the control name and bound field name) to make it a different control. This capability is useful with a multiple-page form when you want to display the same values on different pages and in different locations, or when copying a control from one form to another.
In Figure 7-19, the Complete control is a check box. Although there are times you may want to use a check box to display a Boolean (Yes/No) data type, there are other ways to display the value, such as a toggle button. A toggle button is raised if it’s true and depressed (or at least very unhappy) if it’s false.
Figure 7-19
Use these steps to turn the check box into a toggle button:
1. Select the Complete label control (just the label control, not the check box).
2. Press the Delete key to delete the label control because it is not needed.
3. Right-click the Complete check box, and choose Change To⇒Toggle Button from the pop-up menu.
4. Resize the toggle button and click inside it to get the blinking cursor; then type Complete on the button as its caption (shown on the right of Figure 7-19).
Properties are named attributes of controls, fields, or database objects that are used to modify the characteristics of a control, field, or object. Examples of these attributes are the size, color, appearance, or name of an object. A property can also modify the behavior of a control, determining, for example, whether the control is read-only or editable and visible or not visible.
Properties are used extensively in forms and reports to change the characteristics of controls. Each control on the form has properties. The form itself also has properties, as does each of its sections. The same is true for reports; the report itself has properties, as does each report section and individual control. The label control also has its own properties, even if it is attached to another control.
Everything from moving and resizing controls to changing fonts and colors that you do with the ribbon commands can be done by setting properties. In fact, all these commands do is change properties of the selected controls.
Properties are displayed in a Property Sheet (sometimes called a Property window). To display the Property Sheet for the Description text box, follow the steps below. You will be creating a new blank form.
1. Drag the first five fields, ProductID through Cost, from the Field List window to the form’s Design View.
2. Click the Description text box control to select it.
3. Click the Property Sheet command in the Design ribbon’s Tools group to display the Property Sheet—which appears docked to the right side of the Access window, taking the place of the Field List.
The screen should look like the one shown in Figure 7-20.
Figure 7-20
Because the Property Sheet is a window, it can be undocked, moved, and resized. It does not, however, have Maximize or Minimize buttons. There are several ways to display a control’s Property Sheet if it’s not visible:
• Select a control and click the Property Sheet command in the Design ribbon’s Tools group.
• Double-click any control.
• Right-click any control and select Properties from the pop-up menu.
With the Property Sheet displayed, click on any control in Design View to display the properties for that control. Select multiple controls to display similar properties for the selected controls.
In Figure 7-20, the Property Sheet has been sized to fit the screen. By widening the Property Sheet, you can see more of its values; by increasing the length, you can see more controls at one time. The vertical scroll bar lets you move between various properties.
The Property Sheet has an All tab that lets you see all the properties for a control. Or you can choose another tab to limit the view to a specific group of properties. The specific tabs and groups of properties are as follows:
• Format: These properties determine how a label or value looks: font, size, color, special effects, borders, and scroll bars.
• Data: These properties affect how a value is displayed and the data source it is bound to: control source, input masks, validation, default value, and other data type properties.
• Event: Event properties are named events, such as clicking a mouse button, adding a record, pressing a key for which you can define a response (in the form of a call to a macro or a VBA procedure), and so on.
• Other: Other properties show additional characteristics of the control, such as the name of the control or the description that displays in the status bar.
The number of properties available in Access has increased greatly since early versions of Access. The most important properties are described in various chapters of this book. For a discussion of Event properties and Event procedures, see Part II of this book.
Figure 7-20 shows the Property Sheet for the Description text box. The first column lists the property names; the second column is where you enter or select property settings or options.
There are many different methods for changing property settings, including the following:
• Entering or selecting the desired value in a Property window
• Changing a property directly by changing the control itself, such as changing its size
• Using inherited properties from the bound field or the control’s default properties
• Entering color selections for the control by using the ribbon commands
• Changing label text style, size, color, and alignment by using the ribbon commands
You can change a control’s properties by clicking a property and typing the desired value.
In Figure 7-21, you can see a down arrow and a button with three dots to the right of the Control Source property-entry area. Some properties display a drop-down arrow in the property-entry area when you click in the area. The drop-down arrow tells you that Access has a list of values from which you can choose. If you click the down arrow in the Control Source property, you find that the drop-down list displays a list of all fields in the data source—tblProducts. Setting the Control Source property to a field in a table creates a bound control.
Figure 7-21
Some properties have a list of standard values such as Yes or No; others display varying lists of fields, forms, reports, or macros. The properties of each object are determined by the object itself and what the object is used for.
A nice feature in Access 2007 is the capability to cycle through property choices by repeatedly double-clicking on the choice. For example, double-clicking on the Display When property alternately selects Always, Print Only, and Screen Only.
The Builder button contains an ellipsis (three dots) and opens one of the many builders in Access—including the Macro Builder, the Expression Builder, and the Module Builder. When you open a builder and make some selections, the property is filled in for you. You will learn about builders later in this book.
Each type of object has its own property window and properties. These include the form itself, each of the form sections, and each of the form’s controls. You display each of the property windows by clicking on the object first. The property window will instantly change to show the properties for the selected object.
You might notice that each of the data fields has a Label control and a Text Box control. Normally, the label’s Caption property is the same as the text box’s Name property. The text box’s Name property is usually the same as the table’s field name—shown in the Control Source property. Sometimes, the label’s Caption is different because a value was entered into the Caption property for each field in the table.
When creating controls on a form, it’s a good idea to use standard naming conventions when setting the control’s Name property. Name each control with a prefix followed by a meaningful name that you’ll recognize later (for example, txtTotalCost, cboState, lblTitle). Table 7-2 shows the naming conventions for form and report controls. You can find a very complete, well-established naming convention online by searching for “Reddick Naming Convention.”
The properties displayed in Figure 7-21 are the specific properties for the Description text box. The first two properties, Name and Control Source, are set to Description.
The Name is simply the name of the field itself. When a control is bound to a field, Access automatically assigns the Name property to the bound field’s name. Unbound controls are given names such as Field11 or Button13. However, you can give the control any name you want.
With bound controls, the Control Source property is the name of the table field to which the control is bound. In this example, Description refers to the field with the same name in tblProducts. An unbound control has no control source, whereas the control source of a calculated control is the actual expression for the calculation, as in the example =[SalePrice] - [Cost].
In this chapter, you learned how to add different types of forms to your database using the Create ribbon’s Form group. You learned about the different types of controls and how to add them to the form. Then you learned how to move and resize these controls.
You also learned how properties are the building blocks of an object. The Property Sheet contains every attribute of the control, from where it’s located on the form to what data it displays to what font it’s displayed in. You learned how to display the Property Sheet and how to change a few properties, including the Name property using naming conventions.