Lesson 4.16. Creating a Lookup Field
Figure 4-22. A lookup list displays values from a table or query.
Figure 4-23. Create a lookup list by selecting Lookup Wizard as the field's Data Type.
Figure 4-24. Select a source for the lookup field's values.
Figure 4-25. Select the fields that contain the values.
Lookup fields are definitely one of the coolest and most powerful features in Access. A lookup field lets you pick a field's entry from a list of values. There are two ways that a lookup field can get its list of values:
From a list of values or options that you enter yourself. For example, you could add the values "FedEx," "UPS," and "AirBorne" to a Shipping field.
From a list of values in a table or query. For example, instead of entering a CustomerID number, you could select the CustomerID from a list of names.
You can see an example of a lookup field in Figure 4-22. Instead of you having to type a hard-to-remember CustomerID number, the lookup field displays more meaningful information, such as the customer's name, yet still stores the CustomerID number in the field. Lookup fields will make more sense once you have actually worked with them, so let's jump right into this lesson's exercise.
Close the tblCustomers table without saving your changes, then open the tblCustomerTours table in Design view.
Let's use a lookup field to make the CustomerID field easier to view and add data to. Here's how to create a lookup field:
Click the Data Type box next to the CustomerID field, click the list arrow, and select Lookup Wizard, as shown in Figure 4-23.
The appears, as shown in Figure 4-24, and asks if you want your lookup field to get its values from another table or query or if you want to type a list of options yourself. Since you want your lookup field to get its values from the tblCustomers table, you will select the first option.
The next step in the Lookup Wizard is to select the table or query that contains the values for your lookup field. You want to look up customer names, so you would select the tblCustomers table.
Select the tblCustomers table and click Next.
Now you have to select the fields that contain the values you want to display in your lookup field. The Lookup Wizard displays the field names in tblCustomers table that you can add to your lookup field. To add a field to your lookup field, double-click the field or select the field and click the button.
This step can be a little confusing at first. You need to add the field that contains the value you want to enterthe CustomerID fieldbut you also want to add several fields that will display more meaningful information in the value list, such as the LastName and FirstName fields.
Double-click the CustomerID, LastName, and FirstName fields to add them to the lookup field, as shown in Figure 4-25. Click Next when you're finished.
The next step in the Lookup Wizard dialog box is selecting a sort order for your list. You can sort records by up to four fields, in either ascending or descending order. We don't want to assign a sort order right now, so let's move on to the next step.
This next step allows you to adjust the width of the columns in your lookup list. To adjust the width of a column, drag its right edge to the width you want, or double-click the right edge of the column heading to get the best fit.
You can also indicate whether or not to include the primary key in the column by checking or unchecking the "Hide key column" check box. Any primary key fields will be hidden by default to make the lookup field less confusing. As you can see, your primary key field, CustomerID, is hidden.
Note: If the table or query you are working with does not have a primary key, the "Hide key column" check box will not appear. The Lookup Wizard will instead include an additional step where you will be prompted to select the column that will act as the bound column.
Click Next. Complete the Lookup Wizard by clicking Finish.
You've finished creating the lookup field! Let's test it out....
Click Yes to save your changes and then click the View button to display the table in Datasheet view.
The CustomerID field still contains CustomerID numbers, but now it searches for and displays the customer's LastName fieldmuch easier to understand.
Click the CustomerID field for any record. Click the list arrow that appears in the field.
Out pops a list of all the customers in the tblCustomers tableneat, huh? All you have to do is click a customer's name to add their CustomerID number to the CustomerID field.
Press Esc to close the list without selecting any options.
TO CREATE A LOOKUP FIELD:
DISPLAY THE TABLE IN DESIGN VIEW.
CLICK THE FIELD'S DATA TYPE BOX, CLICK THE LIST ARROW, AND SELECT LOOKUP WIZARD.
CLICK THE I WANT THE LOOKUP COLUMN TO LOOK UP THE VALUES IN A TABLE OR QUERY OPTION AND CLICK NEXT.
SELECT THE TABLE OR QUERY YOU WANT TO USE FOR THE LOOKUP LIST AND CLICK NEXT.
SELECT THE FIELDS YOU WANT TO ADD TO THE LOOKUP FIELD AND CLICK NEXT.
SELECT A SORT ORDER FOR YOUR LIST (OPTIONAL) AND CLICK NEXT.
ADJUST THE WIDTH OF THE COLUMNS THAT WILL APPEAR IN THE LOOKUP LIST AND INDICATE WHETHER OR NOT TO INCLUDE THE PRIMARY KEY IN THE COLUMN. CLICK NEXT.
IF PROMPTED, SELECT A COLUMN THAT WILL ACT AS THE BOUND COLUMN AND CLICK NEXT.
ENTER A LABEL FOR THE LOOKUP COLUMN AND CLICK FINISH.