|
Chapter 4: Simplifying Data Entry with Forms
In this chapter you will learn to:
A database that contains the day-to-day records of an active company is useful only if it can be kept up to date and if particular items of information can be found quickly. Although Microsoft Office Access 2003 is fairly easy to use, entering, editing, and retrieving information in Datasheet view is not a task you would want to assign to someone who's not familiar with Access. Not only would these tasks be tedious and inefficient, but working in Datasheet view leaves far too much room for error, especially if details of complex transactions have to be entered into several related tables. The solution to this problem, and the first step in the conversion of this database to a database application, is to create and use forms. A form is an organized and formatted view of some or all of the fields from one or more tables or queries. Forms work interactively with the tables in a database. You use controls in the form to enter new information, to edit or remove existing information, or to locate information. Like printed forms, Access forms can include label controls that tell users what type of information they are expected to enter, as well as text box controls in which they can enter the information. Unlike printed forms, Access forms can also include a variety of other controls, such as option buttons and command buttons that transform Access forms into something very much like a Microsoft Windows dialog box or one page of a wizard. TIP: Some forms are used to navigate among the features and functions of a database application and have little or no connection with its actual data. A switchboard is an example of this type of form. As with other Access objects, you can create forms by hand or with the help of a wizard. Navigational and housekeeping forms, such as switchboards, are best created by hand in Design view. Forms that are based on tables, on the other hand, should always be created with a wizard and then refined by hand—not because it is difficult to drag the necessary text box controls onto a form, but because there is simply no point in doing it by hand. In this chapter, you will create some forms to hide the complexity of the GardenCo database from the people who will be entering and working with its information. First you will discover how easy it is to let the Form Wizard create forms that you can then modify to suit your needs. You'll learn about the controls you can place in a form, and the properties that control its function and appearance. After you have created a form containing controls, you will learn how to tell Access what to do when a user performs some action in a control, such as clicking or entering text. You will also take a quick look at subforms (forms within a form). SEE ALSO: Do you need only a quick refresher on the topics in this chapter? See the Quick Reference entries on pages xxxiii–xxv. IMPORTANT: Before you can use the practice files in this chapter, you need to install them from the books companion CD to their default location. See "Using the Books CD-ROM" on page xiii for more information. Creating a Form by Using a Wizard
Before you begin creating a form, you need to know what table it will be based on and have an idea of how the form will be used. Having made these decisions, you can use the Form Wizard to help create the basic form. Remember though, that like almost any other object in Access, after the form is created you can always go into Design view to customize the form if it does not quite meet your needs. In this exercise, you'll create a form that will be used to add new customer records to the Customers table of The Garden Company's database. BE SURE TO start Access before beginning this exercise. USE the GardenCo database in the practice file folder for this topic. This practice file is located in the My Documents\Microsoft Press\Access 2003 SBS\Forms\FormByWiz folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Access 2003 Step by Step. OPEN the GardenCo database and acknowledge the safety warning, if necessary.
The second page of the Form Wizard appears, in which you choose the layout of the fields in the new form. When you select an option on the right side of the page, the preview area on the left side shows what the form layout will look like with that option applied. The third page of the wizard appears, in which you can select a style option to see how the style will look when applied to the form. The new Customers form opens, displaying the first customer record in the Customers table.
CLOSE the GardenCo database. Refining Form Properties
As with tables, you can work with forms in multiple views. The two most common views are Form view, which you use to view or enter data, and Design view, which you use to add controls to the form or change the form's properties or layout. When you use the Form Wizard to create a form in a column format, every field you select from the underlying table is represented by a text box control and its associated label control. A form like this one, which is used to enter or view the information stored in a specific table, is linked, or bound, to that table. Each text box—the box where data is entered or viewed—is bound to a specific field in the table. The table is the record source, and the field is the control source. Each control has a number of properties, such as font, font size, alignment, fill color, and border. The wizard assigns default values for these properties, but you can change them to improve the form's appearance. In this exercise, you will edit the properties of the Customers form so that it suits the needs of the people who will be using it on a daily basis. USE the GardenCo database and the tgc_bkgrnd graphic in the practice file folder for this topic. These practice files are located in the My Documents\Microsoft Press\Access 2003 SBS\Forms\Properties folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Access 2003 Step by Step. OPEN the GardenCo database and acknowledge the safety warning, if necessary.
When a form is created, some of its properties are inherited from the table on which it is based. In this example, the names assigned to the text boxes (FirstName, LastName, and so on) are the field names from the Customers table, and the labels to the left of each text box reflect the Caption property of each field. The size of each text box is determined by the Field Size property. TIP: After a form has been created, its properties are not bound to their source. In previous versions of Access, changing the tables field properties had no impact on the corresponding form property, and vice versa. Now in Access 2003, when you modify an inherited field property in Table Design view, you can choose to update the property in all or some controls that are bound to the field.
All the settings available on the toolbar (plus a few more) are also available in a Properties dialog box that is associated with each control. You can use this dialog box to display the properties of any object in the form, including the form itself: simply click the down arrow to the right of the box at the top of the dialog box, and click the object whose properties you want to display. You can display related types of properties by clicking the appropriate tab: Format, Data, Event, or Other. You can also display all properties by clicking the All tab. On the form behind the dialog box, you can see how these changes affect the CustomerID text in the text box (you might have to move the dialog box). TIP: When you are working in Design view with the Properties dialog box open, you can drag the dialog box by its title bar to the side of the screen so that you can see the changes youre making to the form. These different ways of selecting a control and changing its properties provide some flexibility and convenience, but you can see that it would be a bit tedious to apply any of them to a few dozen controls in a form. The next two steps provide a faster method. TIP: You can also select all the controls in a form by opening the Edit menu and clicking Select All, or by dragging a rectangle over some portion of all the controls. Small black handles appear around all the controls to indicate that they are selected. The title bar of the Properties dialog box now displays Multiple selection, and the Objects list is blank. Only the Format settings that have the same settings for all the selected controls are displayed. Because the changes you made in the previous steps are not shared by all the selected controls, the Font Name, Font Size, and Font Weight settings are now blank. The background of the labels will no longer be transparent.
The background of all the controls changes to pale yellow. TIP: If you dont see a color you want to use, click Define Custom Colors, work with the various settings until you have specified the desired color, and then click Add to Custom Colors. You can either click the … button and make a selection, or type a color value such as 32768 in the Border Color box.
TIP: You can edit the Caption property of a label or the Control Source property of a text box by selecting it, clicking its text, and then editing the text as you would in any other Windows program. However, take care when editing the Control Source property, which defines where the content of the text box comes from.
TIP: The order in which you make formatting changes, such as the ones you just made, can have an impact on the results. If you dont see the expected results, click the Undo button or press CTRL+Z to step back through your changes, and then try again. The forms background changes, and the path to the graphic used for the new background is displayed in the Picture property box.
CLOSE the GardenCo database. Refining Form Layout
The forms created by a wizard are functional, not fancy. However, it's fairly easy to customize the layout to suit your needs. You can add and delete labels, move both labels and text controls around the form, add logos and other graphics, and otherwise improve the layout of the form to make it attractive and easy to use. As you work with a form's layout, it is important to pay attention to the shape of the pointer, which changes to indicate the manner in which you can change the selected item. Because a text box and its label sometimes act as a unit, you have to be careful to notice the pointer's shape before making any change. This table explains what action each shape indicates:
In this exercise, you will rearrange the label and text box controls in the Customers form to make them more closely fit the way people will work with them. USE the GardenCo database in the practice file folder for this topic. This practice file is located in the My Documents\Microsoft Press\Access 2003 SBS\Forms\Layout folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Access 2003 Step by Step. OPEN the GardenCo database and acknowledge the safety warning, if necessary.
The form is divided into three sections: Form Header, Detail, and Form Footer. Only the Detail section currently has anything in it.
TIP: To fine-tune the position of a control, click it and then hold down the CTRL key while pressing the appropriate arrow key to move the control in small increments. To fine-tune the size of a control, use the same process but hold down the SHIFT key.
TIP: Form controls inherit whatever theme is set in the operating system. To change the theme, open the Control Panel, click Display, click the Themes tab, select a new theme, and then click OK. TIP: Access saves data automatically as you enter it, but layout changes to any object must be manually saved.
CLOSE the GardenCo database. Adding Controls to a Form
Every form has three basic sections: Form Header, Detail, and Form Footer. When you use a wizard to create a form, the wizard adds a set of controls for each field that you select from the underlying table to the Detail section and leaves the Form Header and Form Footer sections blank. Because these sections are empty, Access collapses them, but you can size all the sections by dragging their selectors. Although labels and text box controls are perhaps the most common controls found in forms, you can also enhance your forms with many other types of controls. For example, you can add groups of option buttons, check boxes, and list boxes to present people with choices instead of making them type entries in text boxes. The most popular controls are stored in the Toolbox. Clicking the More Controls button displays a list of all the other controls on your computer. The controls displayed when you click the More Controls button are not necessarily associated with Access or even with another Microsoft Office program. The list includes every control that any program has installed and registered on your computer. IMPORTANT: Some controls, such as the Calendar Control, can be very useful. Others might do nothing when you add them to a form, or might do something unexpected and not entirely pleasant. If you feel like experimenting, dont do so in an important database. In this exercise, you will add a graphic and a caption to the Form Header section of the Customers form from the GardenCo database. You will also replace the Country text box control in the Detail section with a combo box control. USE the GardenCo database and the tgc_logo2 graphic in the practice file folder for this topic. This practice file is located in the My Documents\Microsoft Press\Access 2003 SBS\Forms\Controls folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Access 2003 Step by Step. OPEN the GardenCo database and acknowledge the safety warning, if necessary.
You can also open the View menu and select the Toolbox check box. To keep the Toolbox open but out of the way, you can drag it off to the side, and dock it on one edge of the screen.
When you release the mouse button, Access displays the Insert Picture dialog box, in which you can select an image to insert in the control. The Garden Company logo appears inside the image control.
TIP: If the control isnt large enough, the image is cropped. You can enlarge the control to display the entire image. (You might also have to enlarge the Form Header section.)
Access inserts a label control containing the insertion point, ready for you to enter a caption. The Customers label takes on the formatting of the other labels.
With the Control Wizards button turned off, you can create a control with all the default settings without having to work through the wizards pages.
When you release the mouse button, Access displays a combo box control, which is unbound (not attached to a field in the Customers table). TROUBLESHOOTING: Access provides a number for each control as it is created, so dont be concerned if the number displayed in your control is different from what you see in the graphics in this book.
Both the combo box control and its label take on the new formatting. SELECT DISTINCT Customers.Country FROM Customers; (Note that there is no space between Customers and Country; there is only a period. There is also a semi-colon at the end of the text.) This line is a query that extracts one example of every country in the Country field of the Customers table and displays the results as a list when you click the boxs down arrow. (You might have to widen the Properties dialog box to display the whole query.)
TIP: If you need to add a new customer from a country that is not in the list, you can type the new countrys name in the combo box. After the record is added to the database, that country shows up when the combo box list is displayed.
CLOSE the GardenCo database. Using Visual Basic for Applications to Enter Data in a Form
As you might have suspected by now, almost everything in Access, including the Access program itself, is an object. One of the characteristics of objects is that they can recognize and respond to events, which are essentially actions. Different objects recognize different events. The basic events, recognized by almost all objects, are Click, Double Click, Mouse Down, Mouse Move, and Mouse Up. Most objects recognize quite a few other events. A text control, for example, recognizes about 17 different events; a form recognizes more than 50. TIP: You can see the list of events recognized by an object by looking at the Event tab on the objects Properties dialog box. While you use a form, objects are signaling events, or firing events, almost constantly. However, unless you attach a macro or Microsoft Visual Basic for Applications (VBA) procedure to an event, the object is really just firing blanks. By default, Access doesn't do anything obvious when it recognizes most events. So without interfering with the program's normal behavior, you can use an event to specify what action should happen. You can even use an event to trigger the running of a macro or a VBA procedure that performs a set of actions. Sound complicated? Well, it's true that events are not things most casual Access users tend to worry about. But because knowing how to handle events can greatly increase the efficiency of objects such as forms, you should take a glimpse at what they're all about while you have a form open. For example, while looking at customer records in the GardenCo database, you might have noticed that the CustomerID is composed of the first three letters of the customer's last name and the first two letters of his or her first name, all in capital letters. This technique will usually generate a unique ID for a new customer. If you try to enter an ID that is already in use, Access won't accept the new entry, and you'll have to add a number or change the ID in some other way to make it unique. Performing trivial tasks, such as combining parts of two words and then converting the results to capital letters, is something a computer excels at. So rather than typing the ID for each new customer record that is added to The Garden Company's database, you can let VBA do it instead. In this exercise, you will write a few lines of VBA code, and attach the code to the After Update event in the LastName text box in the Customers form. When you change the content of the text box and attempt to move somewhere else in the form, the Before Update event is fired. In response to that event, Access updates the record in the source table, and then the After Update event is fired. This is the event you are going to work with. This is by no means an in-depth treatment of VBA, but this exercise will give you a taste of VBA's power. USE the GardenCo database and the AftUpdate text file in the practice file folder for this topic. This practice file is located in the My Documents\Microsoft Press\Access 2003 SBS\Forms\Events folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Access 2003 Step by Step. OPEN the GardenCo database and acknowledge the safety warning, if necessary.
This tab lists the events to which the LastName text box control can respond to.
The Choose Builder dialog box appears, offering you the options of building an expression, a macro, or VBA code.
The Project Explorer pane lists any objects you have created to which you can attach code; in this case, only the Customers form (Form_Customers) is listed. As you create more forms and reports, they will appear here. The Code window displays a placeholder for the procedure that Access will use to handle the After Update event for the LastName text control. This procedure is named Private Sub LastName_AfterUpdate(), and at the moment it contains only the Sub and End Sub statements that mark the beginning and end of any procedure. 'Create variables to hold first and last names
' and customer ID
Dim fName As String
Dim lName As String
Dim cID As String
'Assign the text in the LastName text box to
' the lName variable.
lName = Forms!customers!LastName.Text
'You must set the focus to a text box before
' you can read its contents.
Forms!customers!FirstName.SetFocus
fName = Forms!customers!FirstName.Text
'Combine portions of the last and first names
' to create the customer ID.
cID = UCase(Left(lName, 3) & Left(fName, 2))
' Don't store the ID unless it is 5 characters long
' (which indicates both names filled in).
If Len(cID) = 5 Then
Forms!customers!CustomerID.SetFocus
' Don't change the ID if it has already been
' entered; perhaps it was changed manually.
If Forms!customers!CustomerID.Text = "" Then
Forms!customers!CustomerID = cID
End If
End If
'Set the focus where it would have gone naturally.
Forms!customers!Address.SetFocus
IMPORTANT: When a line of text is preceded by an apostrophe, the text is a comment that explains the purpose of the next line of code. In the VBA Editor, comments are displayed in green.
If you entered the VBA code correctly, COAJO appears in the CustomerID text box. Access does not create a Customer ID. The code does what it was written to do but not necessarily what you want it to do, which is to create an ID regardless of the order in which the names are entered. There are several ways to fix this problem. You could write a similar procedure to handle the After Update event in the FirstName text box, or you could write one procedure to handle both events and then jump to it when either event occurs. You wont do either in these exercises, but if you are interested, you can look at the code in the database file for the next exercise to see the second solution. CLOSE the GardenCo database. Creating a Form by Using an AutoForm
Although a form doesn't have to include all the fields from a table, when it is used as the primary method of creating new records, it usually does include all of them. The quickest way to create a form that includes all the fields from one table is to use an AutoForm. And as with the forms created by a wizard, you can easily customize these forms. In this exercise, you will create an AutoForm that displays information about each of the products carried by The Garden Company. USE the GardenCo database in the practice file folder for this topic. This practice file is located in the My Documents\Microsoft Press\Access 2003 SBS\Forms\AutoForm folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Access 2003 Step by Step. OPEN the GardenCo database and acknowledge the safety warning, if necessary.
The dialog box closes, and after a moment a new Categories form is displayed in Form view.
TIP: When AutoForm creates a form, Access applies the background style you selected the last time you used the Form Wizard (or the default style, if you havent used the wizard). If your form doesnt look like this one, switch to Design view, and on the Format menu, click AutoFormat. You can then select The Garden Company style from the list displayed. Disabling the CategoryID text box changes it, and the label text, to gray. CLOSE the GardenCo database. Last Updated: June 17, 2004
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||