Aligning and Sizing Controls in Design View

To learn how to customize a form, open the HousingDataCopy.accdb sample client database. You need a form to edit and display employee data, and the easiest way to get started is to create a blank form based on the tblEmployees table. On the Create tab, in the Forms group, click the Blank Form button. A blank form grid appears in Layout view with the field list showing on the right side of the screen. Switch to Design view by clicking the small arrow below the View button in the Views group on either the Home tab or the Design contextual tab under Form Layout Tools. Right now the form is unbound—meaning there is no Record Source—so you need to bind this form to the tblEmployees table. Click the Property Sheet button in the Tools group on the Design tab under Form Design Tools. Find the Record Source property on the All or Data tab of the Property Sheet window for the form, and select tblEmployees from the list of table and query names to bind this form to the tblEmployees table.

Drag down the bottom margin of the Detail area to give yourself some room to work. Click the Arrange contextual tab under Form Design Tools, click the Size/Space command in the Sizing & Ordering group and make sure Snap To Grid is not selected. (We’re asking you to do this on purpose so you can learn ways to line up and evenly space controls.) Open the field list by clicking the Add Existing Fields button in the Tools group on the Design tab. If the field list displays other tables in the bottom half of the field list, click the Show Only Fields In The Current Record Source link at the top of the Field List window to show only the fields in the tblEmployees table. Drag each field from the field list into a vertical column on your form about 1.5 inches from the left edge, beginning with the EmployeeNumber field and ending with the BirthDate field. Your starting point should look something like Figure 14-1. (If you don’t want to do the work yourself to get to this point, you can find this form saved as frmXmplEmployee1 in the sample database.)

Start to build a form in Design view to display and edit employee data.

Figure 14-1. Start to build a form in Design view to display and edit employee data.

Note

The HousingDataCopy.accdb sample database has a special template form called Normal that has its default control properties set to preserve the sunken and etched special effects. This ensures that you’ll see the default sunken text boxes when you follow the exercises in this chapter, even when you’re using a computer running Windows Vista or Windows 7 with themed controls enabled. You’ll learn more about creating template forms later in this chapter.

If you threw the form together quickly to help you enter some data (as you did in Chapter 13 to create a simple Companies input form in the ContactsDataCopy.accdb database), it probably doesn’t matter if the form doesn’t look perfect. But all the text boxes except EmailName are the same size, which means some are too large and some are too small to display the data. The long EmailName control looks completely out of place compared to the other controls. Also, the labels are different sizes and not right-aligned. Finally, all the text boxes and labels are out of alignment. If you’re designing the form to be used continuously in an application, it’s worth the extra effort to fine-tune the design so that it will look professional and be easy to use.

Note

Even if you follow along precisely with the steps described in this chapter, your results might vary slightly. All the alignment commands are sensitive to your current screen resolution. When your screen driver is set to a high resolution (for example, 1280 × 1024), the distance between grid points is logically smaller than it is when the screen driver is set to a low resolution (such as 800 × 600). You should design your forms at the same resolution as the computers that will run your application.

To examine the alignment and relative size of controls on your form, you can open the property sheet in Design view and click various controls. For example, Figure 14-2 shows the property sheets for the EmployeeNumber and the FirstName text box controls. You can see by looking at the values for the Left property (the distance from the left edge of the form) that the EmployeeNumber control is a bit closer to the left margin than the FirstName control.

You can see on the Property Sheet windows for the EmployeeNumber and FirstName text box controls that the two controls are not aligned vertically.

Figure 14-2. You can see on the Property Sheet windows for the EmployeeNumber and FirstName text box controls that the two controls are not aligned vertically.

You could move around the form and adjust controls so that they fit your data. You could painstakingly enter values for each control’s Left property to get all controls in a column to line up exactly and then set the Top property (defining the distance from the top of the Detail section) for controls that you want to appear in a row. You could also adjust the values for the Width and Height properties so that controls and labels are the same width and height where appropriate. Fortunately, there are easier ways to make all these adjustments.

One adjustment you might want to make on this employees form is to bold the font for all the labels. Remember from Chapter 13 that you can click the horizontal ruler at the top of the design area to select all controls in a column, so do this to select all the label controls on the left. You can then hold down the Shift key and click the Administrator? label that’s not in the column to include it in your selection. Click the Bold button in the Font group on the Format tab to change the font in all selected controls.

However, now that you have changed the font, the label controls are no longer large enough to display all the characters, as shown in Figure 14-3. Notice, for example, that the last two letters and the colon in the Employee Number label appear clipped off. Also, although all the text boxes and the combo box appear high enough to adequately display the data in the default Tahoma 8-point font, they’re actually too small.

Microsoft Access 2010 has a command called Size To Fit that sizes label controls to fit the width of the caption text in the label. This command also ensures that text boxes and combo boxes are tall enough to display your data using the font size you’ve selected. You can, if you like, select all the controls so that you can resize them all at once. You can click the Select All button in the Selection group on the Format tab to highlight all the controls on your form. To select a specific group of controls, click the first one and then hold down the Shift key as you click each additional control that you want to select. You can also drag the mouse pointer across the form—so long as you don’t start dragging while you are on a control—and the mouse pointer will delineate a selection box. (If you start by clicking a control and then attempt to delineate other controls by dragging, you’ll move only the control.) Any controls that are inside the selection box when you release the mouse button will be selected. You can also select all controls in a vertical or a horizontal band by making the rulers visible (click the Size/Space command in the Sizing & Ordering group on the Arrange tab and then click Ruler) and then dragging the mouse along the top or side ruler.

After you select the controls you want, click the Size/Space command in the Sizing & Ordering group on the Arrange tab and then click the To Fit command beneath Size. The Detail section should now look something like that shown in Figure 14-4. (You cannot see the entire Employee Number label because the right end of it is hidden under the EmployeeNumber text box.)

Switch to Form view, and scroll through several of the records to get an idea of which controls aren’t wide enough to display the data from the table and which ones could be narrower. You could painstakingly resize each control to exactly fit what you see in the sample data, but this is a bad idea for two reasons:

You can logically group the text box controls and the combo box control in this form into three separate lengths, as follows:

You can make the necessary adjustments by leaving the medium-length fields as they are and adjusting the fields in the other two groups. Switch back to Design view now so that you can begin resizing the controls. First, select the EmployeeNumber control and then hold down the Shift key while you select the DepartmentID and StateOrProvince controls. Next, click the sizing box in the middle of the right edge of one of the controls, and drag the right edge to the left until all three controls are about half their original sizes. Now, click the EmailName control, and resize this control to about half of its original width. Next, click the Office Location control and hold down the Shift key while you select the Photo, Address, City, and Country controls. Click the sizing box in the middle of the right edge of one of these controls, and drag the edge right until all three controls are about 50 percent bigger than their original sizes. Your layout should now look something like Figure 14-5.

Before you go on, you might want to save the form and name it frmEmployees. You can find the form at this stage saved as frmXmplEmployee2 in the sample database.

Setting up two columns to edit the data from the tblEmployees table is probably a good idea to better use the screen space that is wider than it is tall. By doing these adjustments yourself, you can choose which fields go in which column. For example, you might want to place the work-related fields (EmailName, Department, WorkPhone, OfficeLocation, Password, and IsAdmin) in one column, and place the personal fields (Address, City, StateOrProvince, PostalCode, Country, HomePhone, BirthDate, and Photo) in another.

To adjust your sample employees form in this way, follow these steps:

When you’re done, you should have a form design that looks something like the one shown in Figure 14-6. Now, you’re ready to fine-tune your form using alignment and control-size adjustments.

It’s a good idea to design your form so that all the controls are spaced evenly down the form and all controls in a column line up. One way that you might find convenient to do this is to take advantage of the grid. If you enable Snap To Grid in the Size/Space command in the Sizing & Ordering group on the Arrange tab, when you move any control, its upper-left corner “snaps” to the nearest grid point. You can use this feature to help you line up controls both horizontally and vertically.

You can adjust the density of the grid by changing the Grid X and Grid Y properties in the property sheet of the form. Be sure that the property sheet is open (Alt+Enter for a shortcut), and then select Form in the Selection Type list near the top of the Property Sheet window. For this example, set the Grid X and Grid Y properties to 16—0.0625 inch between grid points. (If you’re using metric, select a Grid X and Grid Y setting of 6.) This works well for the default 8-point Tahoma font because the “sized to fit” text boxes will be 0.17 inch high. You can place these text boxes every 0.25 inch (four grid points) down the form, which leaves adequate space between the controls. This reduced density also makes it easier to see the grid points so that you can move controls close to the point you want. You could set Grid X and Grid Y to 4, but that reduces flexibility for placing your controls.

The fastest way to snap all controls to the grid is to click Select All in the Selection group on the Format tab, click Align in the Sizing & Ordering group on the Arrange tab, and then click To Grid. The result might look something like that shown in Figure 14-7.

If you want to position each control individually, enable Snap To Grid by making sure it is selected under the Size/Space command in the Sizing & Ordering group on the Arrange tab. (The button appears highlighted and raised when it is active.) Click each text box, combo box, or check box control, and drag it vertically to positions every 0.25 inch (every fourth grid point) down the grid. When you release the mouse button, you’ll see the upper-left corner of the control “snap” to the nearest grid point. As you saw in Chapter 13, when you select and move a control that has an attached label, Access 2010 moves the control and its label as a unit. If you previously moved a label up or down independent of its attached control by using the positioning handle in the upper-left corner, you might need to select either the control or its label and use the positioning handle again to realign each label and associated control.

Snapping to the grid can help you spread the controls apart to make them easier to work with. You’ll see in the next few steps that it’s easy to line them all up properly.

You now have your controls spaced down the form, but they might not be equally spaced, and they probably aren’t aligned vertically and horizontally. These problems are easy to fix. First, if your form ended up looking like the sample in Figure 14-7 with one or more pairs of controls touching, you need to create some more space by moving down the bottom controls in each column. First, click the IsAdmin check box at the bottom of the first column to select it, and press the down arrow once for each pair of touching controls. Do the same, if necessary, to the BirthDate control at the bottom of the second column. (We needed to move the control down one row of dots in our sample.) Next, select all the text box controls and the check box control in the first column. You can do this by clicking the first text box control (not its associated label) and then holding down the Shift key as you click each of the remaining controls in the column. Alternatively, you can click the ruler above the controls. On the Arrange tab, in the Sizing & Ordering group, click the Size/Space command and then click Equal Vertical. Finally, choose all the text box controls and the combo box control in the second column, and click Equal Vertical again.

Now you’re ready to line up the labels. To get started, select all the labels in the left column except the label for the IsAdmin check box. (You can do this the same way you selected all the data-bound controls in a column.) When you have selected them, your form should look something like the one shown in Figure 14-8. Notice that Access also shows large positioning handles in the upper-left corners of all the related controls but no sizing handles.

The labels will look best if their right edges align. You have two choices at this point. If you turn off the Snap To Grid command, you can have Access align all the labels with the label whose right edge is farthest to the right, even if that edge is between dots on the grid. If you leave Snap To Grid on, you can have Access align the labels with the label farthest to the right and then snap the entire group to the nearest grid point.

When you’re ready to align the selected labels on your form, on the Arrange tab, in the Sizing & Ordering group, click the Align command and then click Right. While you’re at it, click the Align Text Right button in the Font group on the Format tab to align the captions to the right edges of all the label controls. Click outside the design area to select the form, which will cancel the selection of the labels. Your form should look similar to the one shown in Figure 14-9.

To further improve the alignment of the controls on the employees form (assuming your form now looks like Figure 14-9), do the following:

  1. The EmployeeNumber text box is a bit too far to the left. Click the control to select it, then click the positioning handle in the upper-left corner, and finally drag the control to the right. If you still have Snap To Grid turned on, it should line up with the FirstName control.

  2. Select the EmployeeNumber, FirstName, MiddleName, LastName, EmailName, DepartmentID, OfficeLocation, WorkPhone, Password, and IsAdmin controls. Click the Align command in the Sizing & Ordering group on the Arrange tab and then click Left.

  3. Select the labels in the right column and right-align them. Click the Align Text Right button in the Font group on the Format tab to align the captions to the right edge of the label controls.

  4. In our sample, the labels in the left and right columns end up a bit too close to the related data controls. Fixing this is a bit tricky. Select the longest label (Employee Number for the left column and State/Province for the right column), grab its positioning handle in the upper-left corner, and drag it left three rows of dots. Grab the sizing handle in the middle of the right edge, and expand the label size until it snaps to one row of dots away from the EmployeeNumber text box. Now, select all the labels again, align them left, and then align them right. Note that by first setting the right edge of the longest label and then aligning all the labels first to the left, the longest label is now assured to protrude farthest to the right. Thus, when the labels are all aligned right again, they line up with the new right offset of the longest label. Repeat the process using the State/Province label for the columns on the right.

  5. We like all our labels to appear to the left of the related control, so click the Administrator? label to select it, grab its positioning handle in the upper-left corner, and drag it to the left of the IsAdmin check box. If you still have Snap To Grid turned on and you do this carefully, the label should line up vertically with the other labels in the column and horizontally with the check box. While you’re at it, click the Align Text Right button in the Font group on the Format tab. If you like, click the Administrator? label, and add a colon to the end of the text.

  6. Close up the bottom of the Detail area a bit so that you have the same amount of space below the bottom control as you do above the top control.

After you complete these steps, your form should look something like the one shown in Figure 14-10.

If you switch to Form view, you can see the result of your work, as shown in Figure 14-11. Click the Save button on the Quick Access Toolbar to save this form. You can also find this form saved as frmXmplEmployee3 in the sample database.