Let's begin discussing tables by introducing lookup fields. This might seem an odd, even perverse, place to start. Lookup fields are simply one Access featurea minor one, at that, and certainly not crucial to table creation. Indeed, my own bias is against using them. But just as the diaries of obscure priests can illuminate profound religious controversies, an understanding of lookup fields is a superb way to introduce table creation in an Access database. Equally important, the topic is an excellent vehicle to review and reinforce the database principles you have learned thus far. The explanation might at first seem just a rehash of Chapters 2 and 3, but ultimately you will find it worthwhile. Nevertheless, if you feel at this point that you've seen one too many one-to-many relationships, and if you feel you have the material in the first four chapters down cold, feel free to jump to the next major section, titled "Creating Access Tables." The Northwind sample database in Access makes significant use of lookup fields. I start by examining two tables there and comparing them to similar ones in another database that doesn't use lookup fields. When Is It Okay to Repeat Values?It will be faster and more effective if I describe an example that shows how lookup fields work instead of asking you to do the exercise yourself (which I usually prefer to do). Feel free to follow along by opening and using the Northwind.mdb database. I strongly suggest that you make and use a copy of the Northwind file, which can likely be found in your Program Files/Microsoft Office/..../Samples folder, rather than the original file, which is available by selecting Help, Sample Databases, Northwind Sample Database. (I give you an exercise to do in Northwind in a few pages, so you might as well make a copy of the file right now.) The Northwind database contains the Products table (see Figure 5.1). It has data about the subject productsproduct names, the supplier, the category, and so on. The table has only one field that has anything to do with the supplier; it contains the supplier name. There is no contact information for the supplier; the contact name, e-mail address, and telephone number are all absent. Figure 5.1. The Products table in Datasheet view.I've stated that each table in a relational database should have only one subject, and that all the fields in the table should contain data only about that subject. It's easy to assert such abstract principles definitively, but it's far more difficult to put them into practice. How does the "one subject" rule apply in this example? Shouldn't supplier contact info be in a products table? What could be more basic, more critical, more "part of" product data than values that tell you how to get in touch with the people who supply them? But the issue is not whether a supplier's telephone number lies somewhere within the universe of product data. It certainly does. It's whether the phone number is much more closely associated with another subject, and thus much better included in another tablenamely, Suppliers. Let's take a closer look at the consequences of including supplier contact info in the Products table. Northwind contains a separate Suppliers table (see Figure 5.2), which contains contact information such as address, phone number, and home page. The first record is for Exotic Liquids; the contact is Charlotte Cooper. In the Products table, ProductIDs 1, 2, and 3chai, chang, and aniseed syrupare all provided by this same supplier, Exotic Liquids. Imagine if you had to store contact info about this supplier for each product. You'd have to enter the telephone numberas well as the contact name, contact title, street address, and so onagain and again for Exotic Liquids. And that would be true for all other suppliers as well. Figure 5.2. The Suppliers table in Datasheet view.
Why Is There Text in a Number Field?Now, finally, let's look at a facet of the problem you haven't seen in your previous work. In Design view of the Suppliers table, the first row is SupplierID (see Figure 5.3). It has the AutoNumber data type. This field is the table's primary key and uniquely identifies each supplier in the Suppliers table. Each supplier in the Suppliers table has a different SupplierID, and each supplier has one and only one SupplierID. Figure 5.3. The Suppliers table in Design view.In Design view of the Products table, the top pane includes the SupplierID field. (As I'll explain a little later, the field's Caption property is Supplier; so in the table's Datasheet view, the column heading is not SupplierID, but Supplier, as shown in Figure 5.1. Don't let this bother you: This is purely for cosmetics and does not affect any of the following discussion.) The SupplierID field in the Products table has the Number data type (see Figure 5.4), which means that only numbers can be entered in the field. The Description tells you that the value for this field should be the same entry as it exists in the Suppliers table. Figure 5.4. The SupplierID has a Number data type and includes matching data from the Suppliers table.In fact, a one-to-many relationship has been established between the Products and Suppliers tables through the SupplierID field. (You can verify this by viewing the Relationships window, which is available by selecting Tools, Relationships.) In the Products table, SupplierID is a foreign key. Whereas a supplier can appear only once in the Suppliers table (for example, there is only one record for Exotic Liquids), it can appear many times in the Products table (you've already seen three instances of Exotic Liquids). The data type of the Supplier field is a number, and the Description says it's the same number as the SupplierID in the Suppliers table. That means the first three records for the Supplier column in the Products table should be 1, the SupplierID for Exotic Liquids. Instead, however, the datasheet shows the actual name of the company (see Figure 5.5). Why? And how can a field have a Number data type if it displays text? Figure 5.5. A small section of the Products table in Figure 5.1. The SupplierID field, which has the column heading Supplier, displays the name of the supplier (Exotic Liquids), not its ID number (1).The Advantage of Lookup FieldsThe answer is that SupplierID in the Products table is a lookup field. Using the matching SupplierID, the field "looks up" the company name in the Suppliers table and replaces the ID with the actual name. The field still stores the SupplierID in the Suppliers table, but it now displays the supplier name. This would seem to present the best of all possible worlds. The field contains that unique ID that enables you to combine all the data from the Suppliers table with that of the Products table in forms, queries, and reports. But instead of a meaningless ID, it displays a value that has meaning that the user can understand. Let's take a look at what happens when you don't use a lookup field. Download the NiftyLionsChap5.mdb database from the companion web site (www.awprofessional.com/title/0321245458) to a convenient folder and open it. This imaginary database is for Nifty Lions, a small mail-order firm that sells various products with lion motifs to the general public. It has many (but not all) of the same tables that are in Northwind, along with similar table structures. It has far fewer records and shorter names, however, so I hope it will be easier to work with. NOTE I've used the Leszynski naming convention to name the main Access objects. Thus, a Customers table is titled tblCustomers, and a Merchandise table is called tblMerchandise, instead of simply Customers and Merchandise. The Leszynski system is described a little later in a sidebar. It's not a big deal: I've used it mainly because you're likely to see it in other contexts. NiftyLions has a Suppliers table. As in Northwind, you have a SupplierID in the first column and the supplier company name in the second. NiftyLions also has a Merchandise table. As in Northwind's Products table, you have Product Name and Supplier fields (see Figure 5.6). Unlike Northwind, however, the Supplier field shows the actual SupplierID, not the supplier name. Thus, the foreign key in the Merchandise table both stores and displays the supplier's AutoNumber primary key, which makes all the values for that supplier in the suppliers table available to the Merchandise table. Figure 5.6. The Supplier field in the Merchandise table is a number.Figure 5.7 shows the Merchandise table in Design view, with the cursor in the SupplierID field. In the Field Properties section below, the Caption property is Supplier. SupplierID and Supplier are the exact same field; in the datasheet, however, a caption has been used to make it easier to read. Figure 5.7. In Design view of the NiftyLions Merchandise table, the SupplierID is a number. But no lookup field has been created, so in Datasheet view, you see the actual SupplierID, not the supplier name.In the Description, a note has been added that the SupplierID field is the same as that in the Suppliers table. In the Merchandise table, SupplierID is a foreign key. Many products can have the same supplier, while in the Suppliers table the supplier's record can appear only once. But no lookup field has been created. Thus, the actual ID of the supplier is displayed in the Merchandise table. That's going to be pretty confusing unless you happen to remember the actual name of SupplierID 1and ID 2, and ID 3, and every other ID. It might not be a problem with seven suppliers, but suppose you had 190 shippers (or 1,000 products, or 10,000 namesthe same principle applies for other fields). Thus, you can see the advantages the lookup field offers. The Downside of Lookup FieldsYou can readily see the problem with lookup fields by creating a query using the Products table of the Northwind database. Let's create a query that will include only products supplied by Exotic Liquids. (If you need a refresher on simple queries, take a look at Chapter 8, "Queries," before proceeding.) If you haven't already done so, find the Northwind.mdb database on your hard drive in the Program Files/Microsoft Office/..../Samples folder; make a copy of the file and open it.
In almost every way that actually mattersstructure, capability to integrate data, the actual value stored in the fieldthe Supplier field in the Northwind products table and the Supplier field in NiftyLions's Merchandise table have the same qualities. Notably, they are both foreign keys on the "many" side of a one-to-many relationship. In only one aspect, the value displayed, do the two fields differ. To the untrained eye, however, the two fields seem quite distinct. Are there ways to construct this query so you can use supplier names instead of ID numbers as criteria? Certainly. But here's the problem with lookup fields: Based on the datasheet in the underlying table, you had every reason to believe that you could use supplier names as criteria in the SupplierID field. When you try that, however, you get an error message that contains no instructions. You're left scratching your head about what to do next. The lookup field masks the underlying structure of your database.
Creating Lookup FieldsBecause I'm not enthusiastic about lookup fields, I'm not going to go through an example on how to create them. Suffice it to say that, in Table Design view, you begin by choosing the Lookup Wizard from the Data Type drop-down list for the field.
What Lookup Fields Can Teach YouAlthough lookup fields themselves might be suspect, the importance of the principle on which they are based cannot be overstated. The lookup fieldand the display of the supplier name instead of the SupplierIDwas possible only because a one-to-many relationship existed between the two tables. Again and again in Access, you will find yourself bringing together data from different tables by establishing relationships. You will be able to do that because the tables are related to one another. As you have seen, those associations are made possible because the tables have matching data in one field. Whether you're designing multitable queries, adding combo boxes to forms, or including fields from different tables in the Report Wizard, your work rests on this same bedrock rule. That might not be apparent now, but it will be increasingly obvious as you proceed through this book and learn more about Access. Concluding Lookup FieldsYears ago, I worked with a group of writers in Japan where we often "polished" (read: did a complete rewrite of) Japanese-to-English translations. Translating is more art than science, and translators have much latitude in their work. Many translators (mostly American, but Japanese as well) spent a lot of effort making their English sound as good as possible. But for those of us who had to rewrite the translations anyway, we wanted them to be as pure and "Japanesey" as possibleeven if they were ungrammatical. It wasn't that we writers were afraid of being made redundant. It was that we wanted to get a true feel of what the Japanese said. Perhaps that's not the best analogy, but that gives you a sense of how I feel about Access tables. I don't want little devices in them, such as lookup fields, that supposedly make my life easier. I want to see the values that have actually, truly been stored. If my tables are sound, I can create the queries, forms, and reports I needand be confident that they are supported in the underlying tables.
|