Chapter 1: Getting Started with Access

Access provides a powerful yet comfortable environment in which you can manage your information. Using Access, you can tame even the most difficult data-management tasks. This chapter is the first step in your journey. Here you will learn basic terms, as well as how to start and exit Access. You will also learn how to get help and even catch a glimpse of what you can do with Access. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • • •

What a database is How tables, forms, and queries relate to a database What records and fields are How to start Access The parts of the Access environment How Wizards can help you How to get help when using Access Possible uses for both databases and Access How to exit Access

Access Terminology
Before you dive into the Access environment, there are a few terms with which you should be familiar. As such, we'll discuss these terms now and make sure you understand them. Note: As you work through this book, you will undoubtedly come across other new terms. The text defines many of these terms at their first use. You can also use the online Help System to find the definitions of many different Access terms. The following sections discuss the major terms which you must understand before you work with Access.

What is a Database?
A database is simply a collection of related information. For example, if you gathered together all your photographs, you would have a database of photographs. If you collected all your photographs that included your dog, you would have either a more targeted database or a subset of your larger database. If your database is small (for example, your home insurance policies), you can probably manage the information manually. In such instances, you might use a traditional management method such as a card file or a simple list on a piece of paper. However, as the database becomes larger, your management task becomes more difficult. For example, it would be virtually impossible to manually manage the customer database of a large corporation. This is where your computer and a database management system

(DBMS) come in handy. DBMS software (such as Access) lets you manage large amounts of information quickly and easily.
In Access, a database consists not just of information, but also the tables into which the information is organized. Access databases also contain related queries, forms, reports, and programming instructions. Because these terms deserve further definition, they are covered in the following sections.

What is a Table?
In Access, tables contain the actual information in your database. There can be more than one table in a database. The information in each table can relate to information in other tables in your database. For example, you might have one table that contains a record of all the door locks in your building. In the same database, another table might have a list of all the keys for those locks. Still another table might contain the names of all the people who have the keys. All three tables contain related information, so they belong to the same database. Figure 1-1 depicts the relationship between the tables and this database.

Figure 1-1 How Access relates tables and databases. Your first step in creating a database is to determine the information you need. Next, you will use Access to design a table to hold the information. Figure 1-2 illustrates the Design view of Access. You use Design view to specify the framework used for each of your table entries.

Figure 1-2 Design view for a table in Access. After you design your table or tables, you will use Datasheet view to enter and examine (view) your data. Figure 1-3 illustrates how your data may appear in Datasheet view mode.

Figure 1-3 Datasheet view for a table in Access.

What is a Query?
When you work with large databases, there will be times when you will want to work with specific pieces of your data. For example, if you have a company database, you may want to view the names of all your customers who reside in New York. On such occasions, you would form a query. A query defines which database information you want to view. Think of a query as a question you ask the database, such as "Who are the customers that reside in New York?" For example, if your database contains the names of all your customers who have purchased a particular product, your query might request a list of only those customers that made over a certain income. Another query might request only those customers with children. Essentially, a query limits or filters the information in a database. When you use a query as a filter, Access displays only information that satisfies the query.

Why would you use queries? Specifically because you want to work with only a part of your database. Queries make it easy for you to work with only those records that fit a particular criteria. Access lets you get as broad or as specific or as complex as you want in the queries you develop. You will begin to learn about queries in Chapter 6, "Using Queries to Examine Your Data."

What is a Form?
A database exists to store information. After you determine the information your database will contain, you will need to enter the data. Later, you may want to examine, add to, or change that data. Although you can use the Datasheet view to accomplish each of these tasks, you may want instead to create a custom display screen that lets you enter, view, and edit information. Within Access, custom display screens are called forms. Using forms, you can both display the information in a table and add buttons, text boxes, labels, and other objects that make data entry much easier. Earlier, in Figure 1-3, you saw what a table looked like as a Datasheet. Figure 1-4 shows an example of a form used to input information into the same table. Notice how using the form provides a better interface for the user.

Figure 1-4 An example of a form used to enter information in a table.

What is a Record?
A record is a single block of information, such as employee or customer data. A table is made up of many records. For example, if you have a table that contains information about your baseball card collection, a record is the specific information about a single baseball card. Sometimes, records are referred to as rows because, within a table, Access represents individual records as rows.

What is a Field?
Just as tables are made up of records, records are made up of fields. A field is the smallest piece of information in a database. For example, if you have a table containing your phone list, each record represents a different person or business in that list. In turn, those records are made up of individual fields (names, addresses, or phone numbers). Figure 1-5 shows the relationship of fields, records, tables, and databases.

Figure 1-5 How fields, records, tables, and databases are related. You may also hear fields referred to as columns. Within tables, Access represents fields as columns when you look at data in Datasheet view (refer back to Figure 1-3).
(This is section 2 of 5 in this chapter)

Using the Access Environment
Before you can work with Access, you need to start the program. You start Access in much the same way as you would any other Windows program. You begin by clicking your mouse on the Start menu, and then choosing Programs. This displays all the programs installed on your system. All you need to do is select the Microsoft Access option from the Programs menu. In a moment, you will see a message welcoming you to Access. At the bottom of the screen, you may see a status bar that displays what is happening while Access is loading. Once Access has started, you are greeted with a dialog box asking if you want to create a new database or open an existing one, as shown in Figure 1-6.

Figure 1-6 You can create a new database or open an existing one. In Chapter 2 you learn how to actually create a database from scratch. Most of the time, however, you will work with the same databases over and over again, so the bottom of the dialog box lists the last nine databases you have opened, along with a More Files choice. To see how Access works, let's open one of the sample databases provided with Access. To do this, click your mouse on the More Files option in the bottom of the dialog box, then click on OK. Access displays a standard Open dialog box, shown in Figure 1-7.

Figure 1-7 The Open dialog box. The Open dialog box may look familiar to you. It is the same dialog box you use for opening files in virtually every Windows program. All you need to do is make sure you are in the right drive and directory, and then select the file you want to load. In this instance, the sample databases provided with Access are in the Samples subdirectory in the directory where you installed Office. The exact location of the files differs from system to system, depending on how Access was first installed. If you need help figuring out where the sample databases are located, you can use the Find feature in Windows to search for the file Northwind.mdb. Once you have located and highlighted the file in the Open dialog box, go ahead and click your mouse on the Open button. In a moment, the Access program window appears as shown in Figure 1-8.

Figure 1-8 The Microsoft Access program window. Within the Access program window, there are several items to which you should pay attention. The first item is the menu bar, located at the top of the window. As you work with Access, the available menus will change depending on what you are doing at the time. The second item is the toolbar, which appears just below the menu. Toolbar icons let you accomplish common tasks quickly and easily. As you work, Access makes different tools available at different times.

The Database Window
One of the most important parts of the Access environment is the Database window, as pointed out in Figure 1-8. You will see the Database window whenever you create or load a database (as you have just done). Earlier in the chapter, you learned that an Access database can contain tables, forms, queries, reports, and other objects. The Database window lets you select the part of your database with which you want to work. On the left side of the window is a series of buttons which indicate the types of objects you can track in your database. When you click your mouse on one of these buttons, Access shows you a list of the objects in that category. For example, if you look at the Database window in Figure 1-8, you see a list of

tables available in the Northwind database. If you click your mouse on the Forms button, you will see a list of forms available, as shown in Figure 1-9.

Figure 1-9 The Form button displays the forms available in the database. Notice that there are quite a few forms available with this database. If there are more objects (such as forms) than can fit in the Database window at one time, Access displays scroll bars in the Window so you can display all the objects in turn.

Understanding Wizards
Access is a very powerful program. With Access, you can manage your data in virtually any way you can imagine. With many database programs, you need to learn esoteric commands or complex procedures in order to use a powerful feature. This is not true with many of the features of Access. To overcome this potential problem, Access uses a series of Wizards which let you accomplish tasks step by step. You invoke many of these Wizards by choosing tools from the various toolbars in Access. Others can be selected from menus, and still others from within the Database window. For example, when you want to create a new table in your database, you simply double-click your mouse on the Create Table By Using Wizard, which is in the Tables list of the Database window (see Figure 1-8). Access then starts the Wizard, the first step of which is shown in Figure 1-10.

Figure 1-10 An example of a Wizard. You work through a Wizard by answering the questions posed in the dialog boxes, or by making choices from those presented. You move from step to step by clicking your mouse on the control buttons at the bottom of each dialog box. Access provides Wizards to help create databases, tables, forms, queries, reports, and graphs. You can use Wizards to help create objects in forms and reports, design mailing labels, and export information. In fact, Wizards are so powerful and make common tasks so easy that they are used throughout this book. Many of the projects you work on will rely on Wizards (at least to begin with).

Getting Help
Access provides quite a bit of help which you can take advantage of at any time you desire. There are three primary ways that Access provides help:

• • •

The Office Assistant An online Help system ToolTips

The primary way to get help in Access is to simply press F1. Access then displays the Office Assistant, which is an animated guide designed to help you discover answers to common Access problems. You typically use the Office Assistant when you have a particular task you want to accomplish. The assistant leads you through the task, helping you accomplish what you want to do. If, for some reason, the Office Assistant does not appear when you press F1, you can also call up the assistant by choosing Show the Office Assistant from the Help menu. If you want to access the Help system in Access, you can do so by using the Help menu; it should be available at all times. You can also access the Help system by clicking your mouse

on the Microsoft Access Help button on the toolbar. This tool is at the right side of the toolbar; it looks like a question mark within a balloon. Another helpful feature of Access is context-sensitive help. You start this by choosing the What's This option from the Help menu. When you choose this option and then click your mouse on another item within the Access environment, the Help system displays any helpful information it has available on that topic. For example, if you choose What's This and then click your mouse on another tool on the toolbar, you will get helpful information about the tool. Context-sensitive help is a very powerful feature you can use to get immediate help. Access also uses a feature called ToolTips. These tips are displayed whenever you move the mouse pointer above a tool on a toolbar. For example, Access displays the ToolTip shown in Figure 1-11 if you move the mouse pointer over the tool that looks like a pair of scissors. The yellow box appearing under the mouse pointer indicates the purpose of the tool.

Figure 1-11 ToolTips help you understand the purpose of different tools.

Looking at Possibilities
As you start to use Access, you might wonder what sort of things you can do with the program. You already know that Access is a full-featured database program, designed to give you the capability to easily manage large amounts of data. But how can you put the program to work for you?

What Can You Do with a Database?
Earlier in this chapter, you learned that a database is simply a collection of related information. With this in mind, if you want, you can use a database to categorize virtually anything. In fact, you are only limited by your imagination. Not everything is well-suited to being maintained with database management software such as Access. Two particular areas come immediately to mind:

If the amount of data to be maintained is small, using a database is probably overkill. For example, you generally would not use a database to keep track of the pots and pans in your kitchen. Although you could do it, no real advantage is gained by using a database for such a simple task rather than employing a more traditional approach, such as a listing of all your kitchenware on a piece of paper. If information is only kept for a short period of time, the value of a database is questionable. For example, you probably wouldn't use a database to track phone messages unless you wanted to keep them for a very long period of time, perhaps for historical analysis.

What Does Access Allow You to Do?
Access lets you take your database ideas and translate them into reality. With Access, you can quickly and easily create databases that you can put to work right away. For example, you may want to develop a database to track your CD collection. Access makes this easy. You can define a simple database, or a relational database with your CDs in one table, the recording artists in another, the studios in another, and the individual CD tracks in still another. You can even have the database contain samples of the music! When you design your database, you can use Access to create input forms as shown in Figure 1-12.

Figure 1-12 CD Collection input form. Input forms make maintaining the information in your database both quick and easy. While creating such a database may at first seem daunting, by applying the techniques described in this book, you can create databases like this in no time! If you have special, not-so-common needs, Access provides development tools you can use to create your own full-blown applications. You can develop forms that use custom controls (buttons, lists, and the like), use special data validation rules, and even create Visual Basic for Applications programming code to process your data. You can attach this code to buttons and other controls on your forms. Such an application--created with Access--is shown in Figure 113.

Figure 1-13 A form with button controls. The result is a custom application, all developed within Access. Projects such as these are not trivial in nature; indeed, they can take months to develop. But Access gives you the tools you need to manage the project and make the most of your development time. Whatever your database needs, simple or complex, quick or evolutionary, Access provides the tools and environment you need to focus on more than just the data. Access helps you develop new ways to examine your data and work with it.

Exiting Access
You exit Access by using any of the same methods you use to exit other Windows programs. These methods include the following:

• • • • •

You can click your mouse on the Close icon in the upper-right corner of the Access program window. You can double-click your mouse on the Control menu icon (the small square with the key in it, located in the upper-left corner of the Access program window). You can choose the File menu Exit option. You can press ALT+F4. You can right-click your mouse on the Access task on the Windows taskbar and then choose Close from the resulting Context menu.

Regardless of how you exit Access, the effect is the same. If you have any unsaved work, Access asks if you want to save it. Access then closes the program window and returns you to Windows.

Summary
This chapter has introduced you to the Access environment. Here you have learned what you need to know before using Access. You have learned basic terms, as well as how to start and end the program. You now have the knowledge necessary to create your first database.

Before you move on to Chapter 2, make sure you understand the following key concepts:

• • • • • • • • •

A database is a collection of related information. In Access, a database is made up of tables, which are made up (in turn) of records and fields. Queries help you limit the information pulled from a database. Forms display information from a database in a user-friendly manner. You start Access by choosing Programs from the Start menu, and then choosing Microsoft Access. The Database window is where you specify the part of your database with which you want to work. As with other Windows programs, Access provides menus and toolbars which you use to control what you want to do with the program. You can use built-in Wizards to make even complex tasks simple. Access provides help in the form of the Office Assistant, an online Help system, and ToolTips. You exit Access in the same manner as you exit any other Windows program.

In Chapter 2, you will learn everything you need to know to create your first database.

Chapter 2: Access in an Hour
(This is section 1 of 7 in this chapter)

Access is a powerful, flexible, easy-to-use, database-management program. In this chapter, you will start to tap into Access capabilities. Although the title of this chapter, "Access in an Hour," is an overworked phrase, you really will learn quickly how to use Access. Granted, an hour won't be enough time to master the advanced features. But it will be enough time for you to learn the basics behind the program and to create your own database that you can put to immediate use. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • • • •

How to create a database What to name your database and where to store it How to create a table using the Table Wizard What types of tables you can create with the Table Wizard How to pick a name for your table What a primary key is and how to choose one How to enter information into your new table How to close the Datasheet How to change and delete information in the table When your information is really saved

Creating Your Own Database
To create your own database, you can use any of three methods:

• • •

Click your mouse on the New tool (it is the leftmost tool on the toolbar; it looks like a blank sheet of paper). Choose New Database from the File menu. Press CTRL+N.

Regardless of which method you choose, you will see the New dialog box shown in Figure 2-1.

Figure 2-1 The New dialog box. The New dialog box allows you to specify how you want to create your new database. There are many choices on the General tab (Figure 2-1) and the other two tabs in the dialog box. The choices provide different ways to accomplish the same ends. The selection you make in the New dialog box serves as the starting point, or template, for a new Access database. For the purposes of this discussion, simply make sure the default Database object is selected, as shown in Figure 2-1. Then click your mouse on the OK button. Access displays the File New Database dialog box, as shown in Figure 2-2.

Figure 2-2 The File New Database dialog box. The File New Database dialog box should look familiar. It is very similar to the Open Database dialog box you learned about in Chapter 1, "Getting Started with Access." It is also similar to dialog boxes that you use to create files in other Windows programs. To use the File New Database dialog box, just specify the name you want to use for your database and where you want to store the database on disk.

Choosing a Database Name
You can name your database anything you want, but the filename must follow the same conventions you would use for any disk filename. Do not include a filename extension (the part of the filename to the right of the period); Access will provide the file extension based on its rules. Notice that the File New Database dialog box lists all the database files in the current directory. If you want to replace an existing database, all you need to do is provide the name of that database. To do this, type in the name or use the mouse to select it from the filename list. In general, you should choose a database name that is as descriptive as possible. For example, if you were creating a database about your gem collection, you might name it "Gems" or "My Gem Collection." In this chapter, you will create a database that will contain the names, addresses, and phone numbers of all your friends. Thus, "My Friends" is an appropriate name for this database. Type the words My Friends in, but don't press ENTER quite yet.

Where Should You Store Your Database?

In the New Database dialog box, you also can specify where you want to store your database. Access lets you store database files virtually anywhere you desire. You can specify any drive, path, or network location to which you have access. You may want to use a special directory for your database files. For instance, you may want to put all your work-related databases in one directory and all your personal databases in another directory. If this is the case, you should save the My Friends database in your personal database directory. After you specify your database name and location, click your mouse on the Create button. Access creates the new database, using the name you provided, and displays the Database window shown in Figure 2-3. Notice that the name of your database is shown at the top of the Database window.

Figure 2-3 The Database window for your new database. As you will recall from Chapter 1, Access stores your data in tables. Since this Database window is for a brand new database, there are no tables listed. In the following section, you will learn how to create a table.

Chapter 2: Access in an Hour
(This is section 2 of 7 in this chapter)

Creating a Table
After you have created your database, you need to create at least one table. Access uses tables to define the structure of the data in your database. Each table contains a collection of related information. Access makes it easy to construct tables. Access also provides a Table

Wizard to help you create common types of tables. You will use the Table Wizard to create your first database table. Note: Make sure you don't confuse a table with a database. In some software, a database is simply a list of information, much like a table in Access. However, a database in Access is much more than a table. For more information, refer to Chapter 1.

Starting the Table Wizard
Before you start the Table Wizard, make sure that the Tables button is selected in the Database window (your screen will appear as shown earlier in Figure 2-3). Then, double-click your mouse on the Create Table By Using Wizard option. Momentarily, you should see the Table Wizard dialog box shown in Figure 2-4. In this dialog box, Access asks you to select the type of table you want to create.

Figure 2-4 Picking the type of table to create. Before you select a table type, take a minute to look at the Table Wizard dialog box. This dialog box is similar to other Wizard screens: at the top is some help information, in the center is where you make your choices, and at the bottom are the Wizard control buttons. You use these buttons to move from one step in the task to the next. The purpose of these buttons is shown in Table 2-1.

Butto n Cancel Back Next Finish

Meaning Stops the Wizard; no action is taken. Returns to the previous step (if available). Proceeds to the next step (if there is one). Ends the Wizard; completes the action with the available information.
Table 2-1 The Wizard control buttons.

Notice that at this point only one of the control buttons is available; the others are dim. Access makes only those control buttons available which have meaning at the current time. Thus, the Back button is not available because there is no previous step; you are at the first step right now. The Next and Finish buttons are not available because you need to make some choices before the Wizard can progress.

Picking a Table Category
At this particular step of the Table Wizard, Access asks you to specify the type of table you want to create. Notice that there are two choices--Business and Personal--in the center of the left side of the dialog box. The button you select affects the types of tables listed in the Sample Tables list. Just for practice, select the Business button. Notice that there are many different sample tables listed. You can view all of them using the scroll bar at the right side of the Sample Tables list. Table 2-2 describes the different sample business tables which the Table Wizard can create.

Sample Table Mailing List Contacts Customers Employees Products Orders Order Details Suppliers Categories Payments Invoices Invoice Details Projects Events Reservations Time Billed Expenses Deliveries Fixed Assets Service Records Transactions

Meaning Creates a simple or complex mailing list. Tracks your sales contacts. Manages your business customers. Maintains information about your company's employees; can work with the Expenses and Time Billed sample tables. Develops your own inventory database. Tracks orders for your products. Tracks order details (individual line items); works in relation to the Orders sample table. Tracks your vendors. Builds a category list which you can use in conjunction with other tables. Records payments made by your customers. Maintains information about invoices generated. Tracks invoice details (individual line items); works in relation to the Invoices sample table. Tracks the projects on which you or your employees are working; works in relation to the Time Billed sample file. Develops lists of special events; works in conjunction with the Reservations sample table. Manages reservations for a particular event; works in conjunction with the Events sample table. Tracks time expended on projects; works with the Projects, Employees, and Expenses sample tables. Maintains a history of expenses related to specific projects; works with the Projects and Employees sample tables. Tracks deliveries of your product; can be related to the Customers, Orders, and Employees sample tables. Develops a list of the fixed assets owned by your company. Logs when a service is performed on an asset; can work with the Fixed Assets and Employees sample tables. Maintains a record of business transactions performed; can work

Tasks Employees And Tasks Students Students And Classes

with the Payments sample table. Tracks a list of tasks to be accomplished. Builds relationships between the Employees and Tasks sample tables. Maintains information about students (or employees) and their classes. Builds relationships between the Students table and a sample Classes table.

Table 2-2 Business tables which can be created with the Table Wizard. Note: Remember that the sample tables used in the Table Wizard are just that--samples. However, you can modify them slightly while you are using the Table Wizard. Also, you can later change the design of any table created with the Table Wizard. Since you want to create a database of your personal friends, you will need to select the Personal category. To select this group of sample tables, click your mouse on the Personal option button. As shown in Figure 2-5, Access changes the list of sample tables.

Figure 2-5 The Table Wizard after displaying the Personal sample databases. Scroll through the sample tables. You will see that the Table Wizard can create many different personal tables. For a list of these tables, refer to Table 2-3.

Sample Table Addresses Guests Categories Household Inventory Recipes

Meaning Creates a mailing list of your friends and acquaintances. Tracks guests invited to a social event. Builds a category list which you can use in conjunction with other tables. Maintains a record of your belongings for insurance purposes. Manages your favorite quiche, fondue, and other recipes.

Plants Exercise Log Diet Log Wine List Rolls of Film Photographs Authors Books Groups Service Records Recording Artists Recordings Video Collection Accounts Investments

Tracks the contents of your greenhouse or backyard jungle. Records exercises you have done and calories burned. Manages your diet on a meal-by-meal basis. Tracks your favorite wines and vineyards. Logs the rolls of film you shoot; you can use it with the Photographs sample table. Maintains a log of your photographs; you can use it with the Rolls of Film sample table. Lists your favorite authors; you can use it with the Book Collection sample table. Tracks books in your personal library; you can use it with the Authors sample table. Lists group photographs and any notes you have about them. Logs when a service is performed on an asset; with some modification, this table can work with the Household Inventory sample table. Lists your favorite artists; you can use it with the Recordings sample table. Tracks your recorded music; you can use it with the Artists sample table. Organizes your video tapes and laser discs. Manages your investment accounts; works with the Investments sample table. Tracks your individual investments; works with the Accounts sample table.

Table 2-3 Personal tables which can be created with the Table Wizard. Take a moment to select different sample tables. As you use the mouse to highlight different sample table names, notice that Access changes the information in the Sample Fields list. To create a table to keep track of the names, addresses, and phone numbers of your friends, highlight Addresses in the Sample Tables list. (There is a good chance that this option is already highlighted; it is the first option in the list.) You are now ready to select the fields that will appear in your table. Your screen should appear as shown earlier, in Figure 2-5.

Choosing Types of Information to Include
Within your table, you will specify the fields, or pieces of information, you want to track. The middle column, labeled Sample Fields, is where you specify the fields you want to use in your table. Remember from Chapter 1 that fields define the type of information Access maintains for each record in your table. Thus, at this point, you are defining the type of information you want to keep on each of your friends and acquaintances. Table 2-4 shows the fields from which you can make your selection.

Field AddressID FirstName LastName SpouseName

Meaning A unique numeric ID for your friend's address Your friend's first name Your friend's last name The name of your friend's spouse

ChildrenNames Address City StateOrProvinc e PostalCode Country EmailAddress HomePhone WorkPhone WorkExtension MobilePhone FaxNumber Birthdate SendCard DateLastTalked To DateUpdated Nickname Notes Hobbies HealthIssues Photograph

The name of your friend's children Your friend's street address Your friend's city Your friend's state or province Your friend's ZIP code Your friend's country Your friend's electronic (Internet, Prodigy, and so on) mail address Your friend's home phone number Your friend's work phone number Your friend's extension at work Your friend's mobile (cellular) phone number Your friend's fax line phone number Your friend's date of birth An indicator of whether you want to send a birthday card to this friend Date you last had contact with your friend Date of last change to the database record Any nicknames or preferred names for your friend Any comments or notes you may want to include Any of your friend's hobbies or interests Any of your friend's health problems A photo of your friend

Table 2-4 Sample fields available for the Addresses sample table. The Table Wizard provides 25 sample fields for the Addresses sample table. However, you don't need to use all of them. Instead, you should pick the fields you think you want to maintain. Remember, you will need to keep the information up-to-date for it to be of value. The more fields you select, the more fields you must maintain. To select a field, highlight the field name and then click your mouse on the button with the greater than symbol (>). Access, in turn, will move your selected field to the third column in the dialog box, which is labeled Fields In My New Table. Since you will probably want to include at least the first names, last names, addresses, and phone numbers of your friends, highlight each of these fields in the Sample Fields list. Then, after each is highlighted, click your mouse on the > button. When you are done, the dialog box should appear as shown in Figure 2-6.

Figure 2-6 After specifying a few fields for your table. If you make a mistake, don't despair. You can use the dialog box buttons between the second and third columns to manipulate each of the fields you will include. Table 2-5 lists these buttons and their purposes.

Butto n > >> < <<

Purpose Includes a field from the Sample Fields column in your table. Includes all fields from the Sample Fields column in your table. Removes a field from your table. Removes all fields from your table.
Table 2-5 Field selection buttons.

Note: You can move fields around in your table by removing them and then adding them again. Access adds a selected field to your table immediately after whatever field is highlighted in the third column. Include whatever fields you deem necessary for your table. Make any modifications you desire until you have completely defined the field order you desire. Before moving on, make sure that you add the AddressID field to your table. This field assigns a primary key to your table. In the following two sections, you will learn the importance of adding the AddressID field. When you are done, click your mouse on the Next button to tell Access you are through with this step and ready to move on. Access will display the next step dialog box shown in Figure 2-7.

Figure 2-7 The next step in the Table Wizard.

Naming Your Table
You are nearly finished defining your table. However, Access needs to know a few more pieces of data before it can proceed. At the top of this dialog box, Access asks you to name your table. Don't confuse this name with the name you gave your database when you first created it. Here you are naming your table, and the database you already created can contain many such tables. For this example, you will accept the default name (Addresses). In this dialog box, you also specify whether you want Access to use a primary key for your table. A primary key is a field that Access uses to sort your table. Generally, a primary key should be unique, meaning that there will only be one record with any given primary key; Access will not let you add a record if it has the same primary key as another record. For example, if you have three friends with the last name Smith, you cannot use the last name as a primary key. Likewise, if all your friends live in Seattle, you cannot use the city as a primary key. However, since your friends probably have unique phone numbers, you could use the phone field as the primary key. You can set the primary key one of two ways. You can let Access set a key for you (this is the default), or you can specify the key yourself. Make your choice and then click your mouse on the Next button.

Understanding Keys
A primary key defines how Access orders information in your table. Even though you don't need to define a primary key, Access will function more efficiently if you do. Using a primary key, Access can locate and display records very quickly. This improved performance will be more important as you add more records to your database. Access lets you define both unique keys and non-unique keys. Using a unique key is best because it improves searching time. However, there may be situations in which a unique

key is not possible. For example, you may want to use a city name as the key. Since there are many cities with the same name, you would want to specify this as a nonunique key. In addition, you can use multiple fields to define your key. However, you cannot use the Table Wizard to do so. Instead, you must create the table manually. You will learn how to do this in Chapter 4, "Creating Another Database." When you choose a field to use as a primary key, you should select a field that is as close to unique as possible, regardless of whether you allow duplicates or not. For instance, you would not want to use the FirstName or LastName field as the primary key; there can be way too much duplication in these fields. On the other hand, you probably could use the HomePhone field since there is much less chance for duplication in this field.

Defining a Key
If you let Access define your primary key automatically (at the end of the previous section), you can skip this section entirely and proceed to the next one. If you set the key yourself, you will see the dialog box shown in Figure 2-8.

Figure 2-8 Setting up the key for your table. Using this dialog box, you can select which field Access will use as the key. Access suggests a field to use (the first one in your table layout), but you can select a different one by clicking your mouse on the drop-down arrow at the right side of the field. For this example, make sure the AddressID field is selected. Note: If you forgot to include the AddressID field in your table layout, you can use the Back button to return to the dialog box where you define fields. (You will need to click your mouse on the button twice.) Add the field and then use the Next button to return to this dialog box. Next, the dialog box asks you to specify how you want Access to treat the primary key. You have three choices:

• • •

Consecutive numbers Microsoft Access assigns automatically to new records. If you choose this option, you will never need to fill in the primary key; Access will do it for you as you add records. Numbers I enter when I add new records. If you select this option, you can enter the primary key manually. However, the key must still be unique, and it must contain only numbers. Numbers and/or letters I enter when I add new records. This is the same as the previous option except that you can enter both letters and numbers.

The primary key option you select depends on how you plan to use the primary key. If you only want a counter (a key whose value corresponds to the record number: 1, 2, 3, and so on), select the first option. However, if your primary key will be something else, choose one of the other options. When you are done, click your mouse on the Next button.

Finishing Your Table
To finish your table, you must now answer two more questions. Both questions regard what you want to do when the Table Wizard is done. At this point, you should see the dialog box shown in Figure 2-9.

Figure 2-9 Finishing up the Table Wizard. In this dialog box, you specify what you want to do next. Your choices are as follows:

• • •

Modify the table design. Select this option if you want to continue refining the design of your table. Enter data directly into the table. Select this option if you want to use the Datasheet view to enter information in your table. Enter data into the table using a form the Wizard creates for me. Select this option if you want the Wizard to create a data input form for your database table.

In Chapter 9, "Creating Custom Forms," you will learn how to work with forms. Right now, choose the second option (which is the default). If you feel you need step-by-step hand-holding while using Access, you can choose the Display Help check box at the bottom of the dialog box. As soon as you click your mouse on the Finish button (notice the Next button is no longer available; there are no further steps), the Table Wizard creates the table according to your specifications. You will know the Wizard is done when you see the Datasheet view of your table on the screen. Note: Congratulations on designing your first database and table. In other database systems, what you have just accomplished could take hours. Your table is now ready for you to enter information, and you are fast on your way to becoming proficient with Access.

Chapter 2: Access in an Hour
(This is section 3 of 7 in this chapter)

Entering Information in Your Table
One of the primary ways to enter information in a table is through the Datasheet. In Chapter 1, "Getting Started with Access," you learned that you enter data into a table using an Access datasheet. Now that you have created your own table, you are ready to start using the Datasheet for your own information. Your Datasheet is shown in Figure 2-10. The actual layout of your Datasheet may be a bit different, depending on which fields you used when you defined your table. But regardless of the fields used, all operations within the Datasheet are essentially the same.

Figure 2-10 The Datasheet view of your table. Notice that the cursor is blinking in the first field of the first record in your table. Access is waiting for you to begin entering information. To do this, just start typing. When you want to move from field to field, press either the ENTER or TAB key. Note: Skip any field (by pressing TAB) that already contains something (such as the first field in Figure 2-10, which contains the AutoNumber designation). Since this table was set up so that the AddressID field is the primary key, Access automatically fills in this field. As you start to enter information in your table, notice that Access assumes you are starting to enter a record. As shown in Figure 2-11, Access signifies this by adding an extra record below, and placing a pencil icon at the left side of, the record on which you are working.

Figure 2-11 Adding a record in Access. You should continue to add the information to your record. When you reach the last field, press TAB or ENTER. Access, in turn, will take you to the first field of the next record. You can back up through previous fields by pressing SHIFT+TAB. When you finish with one record and proceed to another, Access automatically saves your changes (notice that the pencil icon is removed from the left side of a record that has been saved.)

Moving Around Your Table
As the number of records in your table increases, you can use different keyboard keys to quickly move from one record to another or within fields of a specified record. Table 2-6 lists the keys you can use to navigate through your table.

Keypress TAB SHIFT+TAB ENTER RIGHT ARROW LEFT ARROW DOWN ARROW UP ARROW PAGE DOWN PAGE UP HOME END F5

Action MOVE TO THE NEXT FIELD MOVE TO THE PREVIOUS FIELD MOVE TO THE NEXT FIELD MOVE TO THE NEXT FIELD MOVE TO THE PREVIOUS FIELD MOVE TO THE NEXT RECORD MOVE TO THE PREVIOUS RECORD MOVE DOWN ONE SCREEN OF RECORDS MOVE UP ONE SCREEN OF RECORDS MOVE TO THE FIRST FIELD IN THE CURRENT RECORD MOVE TO THE LAST FIELD IN THE CURRENT RECORD JUMP TO A SPECIFIC RECORD NUMBER

CTRL+DOWN ARROW CTRL+UP ARROW CTRL+PAGE DOWN CTRL+PAGE UP CTRL+HOME CTRL+END CTRL+ALT+END

MOVE TO THE CURRENT FIELD OF THE LAST RECORD IN THE TABLE MOVE TO THE CURRENT FIELD OF THE FIRST RECORD IN THE TABLE SCROLL RIGHT ONE SCREEN SCROLL LEFT ONE SCREEN MOVE TO THE FIRST FIELD OF THE FIRST RECORD IN THE TABLE MOVE TO THE LAST FIELD OF THE LAST RECORD IN THE TABLE MOVE TO THE LAST FIELD OF A NEW RECORD (AT THE END OF THE TABLE)
Table 2-6 Navigating through your table.

Data Entry Shortcuts
In addition to the keys listed in Table 2-6, Access provides a number of shortcut keys you can use when you enter your data. Some shortcut keys you know about; you use them to initiate menu functions. When you select an Access menu, Access displays shortcut keys to the right of menu options. Table 2-7 lists data entry shortcut keys you can use to speed up routine data entry tasks.

Keypress CTRL+; CTRL+ALT+SPACEB AR CTRL+' CTRL+" CTRL+ENTER CTRL+PLUS SIGN CTRL+SHIFT+ENTER

Action INSERTS THE CURRENT DATE INSERTS THE DEFAULT VALUE FOR A FIELD (IF ANY) INSERTS THE SAME VALUE AS IN THE SAME FIELD OF THE PREVIOUS RECORD INSERTS THE SAME VALUE AS IN THE SAME FIELD OF THE PREVIOUS RECORD INSERTS A NEW LINE WITHIN THE CURRENT FIELD ACCEPTS THE CURRENT RECORD AND ADDS A NEW ONE DELETES THE CURRENT RECORD SAVES CHANGES TO THE CURRENT RECORD, BUT REMAINS IN CURRENT FIELD
Table 2-7 Shortcut keys for entering data.

Leaving the Datasheet
When you finish entering information, you will probably want to leave or close the Datasheet. This tells Access you are done working with the table, and you are ready to perform some other task. There are two primary ways you can leave the Datasheet: select the Design view tool or click your mouse on the Close icon (the X) in the upper-right corner of the Datasheet. The Design view tool is the leftmost tool on the toolbar. Click your mouse on this tool to display another side of your table. Your screen will appear similar to what is shown in Figure 2-12.

Figure 2-12 Switching to Design view. When you switch to Design view, the Design view tool changes to the Datasheet tool. Clicking your mouse on the Datasheet tool displays your table's Datasheet again. When you are working in Design view (as shown in Figure 2-12), Access lets you change your table's layout. Design view displays the same fields you set up when you used the Table Wizard earlier in the chapter. In Chapter 3, "Taking a Closer Look," you will learn more about how you use Design view. To close a Datasheet or Design view window, click your mouse on the Close icon in the upperright corner of the window. Access closes the window, and the Database window is left visibile. Your new table (Addresses) is listed in the available tables in the Database window. When you later want to work with the information in your table, you can either double-click your mouse on the table name in the Database window or select the table name and click your mouse on the Open button.

Chapter 2: Access in an Hour
(This is section 4 of 7 in this chapter)

Changing Information in Your Table
In the previous section, you learned how to use the Datasheet view to add information to your table. The Datasheet view also lets you quickly and easily edit existing information. You can do this by following these steps:
1. Select the field you want to change. Press the F2 key. Use the arrow keys to move through the field contents, making changes as desired. Press one of the navigation keys to have Access save your changes and move you to another field.

2.
3.

4.

You can also use the mouse to speed up some of your editing. For instance, if you want to make a change in a field, click your mouse on the field. You can then perform steps 3 and 4 above.

Finding Information
If you have many records in your table, you may find it difficult to rely solely on the keyboard navigation keys to find a specific record. To help you quickly find a specific record, Access lets you search for specific data, such as a name or phone number. To search your table for specific data, select the Find option from the Edit menu. Access displays the Find and Replace dialog box shown in Figure 2-13.

Figure 2-13 The Find tab of the Find and Replace dialog box. In the Find What box, you enter the data you want to search for. You can enter any text into this box, such as a name, a phone number, or a city. You can also use wildcard characters to broaden your search. For example, you might want to search for the first phone number that uses the 212 area code, or the first city whose name starts with the letter A. Access provides a large number of wildcard characters you can use in your searches. Table 2-8 lists the available wildcard characters.

Characte r * ? # [] ! -

Meaning Matches any number of characters. Matches any single character. Matches any single digit. Matches any single character within the brackets. Matches any single character except those specified. (Used with the brackets.) Matches any single character within a range. (Used with the brackets.)
Table 2-8 Wildcard characters you can use while searching.

You may already be familiar with the concept of wildcards. For most people, the asterisk and question mark wildcard characters are the most familiar. The question mark can be used in place of any single character. The asterisk is used to represent any number of characters, and it can be used anywhere within a search specification. Thus, to search for anything starting with the letters br and ending in ed, you would use a search specification such as this: br*ed In this case, your search might match text such as breed, branded, or even bruised. If you use the brackets, exclamation point, and minus sign in your specification, you can be even more creative. The brackets let you develop a list of acceptable characters. For example, the following search specification finds the words bold and bond: bo[ln]d You would not find Boyd because y is not an acceptable character. You can use the exclamation point to reverse the process. For example, if you wanted to find anything except the letters l and n in the third position, you would use the following: bo[!ln]d Now, Access will find the word Boyd, but not the words bold or bond. You can use the minus sign (a dash) to specify a range of letters to be included. For instance, the following search specification will find any text with the letters c through n in the fourth position: hea[c-n]* If you use the Look In pull-down list, you can specify where your search should be conducted. You typically have two options available. You can either search in only the current field, or you can search in the entire table. Obviously, limiting your search to the current field results in a much quicker search. You can also use the Find dialog box to specify the parts of your table you want to search. If you click your mouse on the arrow to the right of the Match box, you can choose to search any of the following areas:

• •

Any Part of Field Whole Field

Start of Field

You can further specify how your search should be done by clicking your mouse on the More button. This results in an expanded Find and Replace dialog box, as shown in Figure 2-14.

Figure 2-14 The expanded Find tab of the Find and Replace dialog box. The newly visible area of the dialog box allows you to specify the direction in which a search should be conducted. Using the Search pull-down list, you can specify Up (toward the start of your table), Down (toward the bottom of your table), and All. You can also limit your search by selecting one of the two check boxes. They control whether Access should pay attention to upper- and lowercase in your search, and whether formatting should play a role in matches found. After you have specified your search criteria, click your mouse on the Find Next button to display the first match to your search. If the first match does not contain the data you desire, continue the search by again clicking your mouse on the Find Next button. Note: It is sometimes difficult to edit a field while the Find dialog box is visible. When you are ready to edit, close the dialog box and make your change. When you want to find the next occurrence of your search specification, press SHIFT+F4. This is much faster than reissuing the Find command and using the Find Next button.

Replacing Information
As you work with your data, there will be times when you need to find a field so you can change its contents. For example, if your friend's phone number changes, you will want to find and update the phone number. The Replace option from the Edit menu, which is closely related to the Find command, lets you not only find information, but also replace it with different information. To initiate the Replace command, choose the Replace option from the Edit menu or press CTRL+H. You will see the Find and Replace dialog box, as shown in Figure 2-15.

Figure 2-15 The Replace tab of the Find and Replace dialog box. Notice that the Replace dialog box is very similar to the Find dialog box, shown earlier in Figure 2-13. In the Find What box, you type the data you want to find; you can use the same type of search specifications (including wildcard characters) discussed in the previous section. In the Replace With box, you type the new data you want to use to replace the information in the Find What box. You cannot use wildcard characters in the Replace With box. Using the Look In drop-down list, you tell Access where you want to search (the current field or the entire table). The Match drop-down list performs the same function as when doing a simple search. Clicking your mouse on the More button displays the same additional controls shown in Figure 2-14. After you have filled in your specifications, click your mouse on Find Next to find the next occurrence of the Find What string. When Access locates a match, it displays the match on the screen. You can then replace the match by clicking your mouse on the Replace button. When you select Replace, Access will make the replacement and search for the next occurrence of the Find What string. If, instead of Replace, you click your mouse on the Find Next button again, Access leaves the previous match unchanged and searches for the next occurrence of the Find What string. If you are absolutely sure you want to change everything in the table that meets your criteria, you can click your mouse on the Replace All button. When you select Replace All, Access automatically replaces all occurrences of your Find What text in one step.

Chapter 2: Access in an Hour
(This is section 5 of 7 in this chapter)

Deleting Records
Earlier in this chapter, you learned how to add records to your table. Over time, you may need to delete one or more records. For example, if you have an argument with your buddy, you may want to delete him from your "My Friends" database. To delete a record, follow these steps:

1.

2.

Use your mouse to select a record you want to delete. Do this by moving the mouse pointer into the gray area at the left of the record. As the mouse pointer enters this area, it will change into a right-pointing arrow. Click your mouse on the left mouse button to highlight the record. Press the DEL key or select the Delete option from the Edit menu. Access displays a dialog box (shown in Figure 2-16) to prompt you to confirm the deletion:

3.

Figure 2-16 You need to confirm any record deletions.
4. Click your mouse on Yes to delete the records or No to not delete them.

Note: Make sure you really want to delete the selected records before you click your mouse on Yes. You cannot undo your changes once the deletion is complete. Also, Access lets you select multiple records for deletion. To select additional records, hold down the SHIFT key and repeat steps 1 and 2. This selects all the records between the first record and the second. To select non-sequential records, hold down the CTRL key and repeat steps 1 and 2.

Chapter 2: Access in an Hour
(This is section 6 of 7 in this chapter)

Saving Your Information
Earlier in this chapter, you learned how to enter and edit information in a table. When you are adding or changing a record, a pencil appears to the left side of the record. This pencil indicates that the record is being edited and that your changes have not been written to disk yet. When you use one of the navigation keys to move to another record, Access automatically saves your changes to disk. There is no need to explicitly save your data; in fact, there is no command you can use to save it. Access automatically saves data in this way to protect your information. If you should lose power to your computer while the pencil icon appears on the screen, you would only lose the information in the current record. Access has already saved all your previous changes to disk. Note: The exact point at which your information is physically written to disk will vary from system to system. Typically, information is first written to a disk buffer in the memory of your computer. When the buffer is full or when your program ends, the information is written to the disk. Although this arrangement speeds up overall computer performance, it does present a risk. Information within the disk buffer (yet to be written to the disk) will be lost if you should lose power to your computer. The only way around

this is to make sure you have a backup power source and to keep stray fingers away from the power switch.

Chapter 2: Access in an Hour
(This is section 7 of 7 in this chapter)

Summary
In this chapter, you caught your first glimpse of the power Access brings to your desktop. You now know how you can use the development tools built into Access (such as the Table Wizard) to quickly and easily set up tables. You also know how to create your own database and enter information in tables. Before moving on to Chapter 3, make sure you understand the following key concepts:

• • • • • • • • •

You can create a database by using the File menu New Database command or by using the New tool. You can use the Table Wizard to quickly and easily create a table. The Table Wizard provides many sample tables. You can use these as they are or use them as a starting point for a custom database. Use a primary key in your table to help Access efficiently order the table and quickly find records. The Datasheet lets you directly enter information in a table and edit existing information. You can use the Find command to search for information in your table. This command lets you build search criteria using wildcard characters, if desired. You can use the Replace command to replace information in your table. This command is similar to the Find command, but also allows the replacements. To delete a record from the Datasheet, select the record and press the DELETE key or choose the Delete option from the Edit menu. Access saves your changes on a record-by-record basis. When you move from one record to another, Access automatically saves any changes to the older record to disk.

In Chapter 3, you will learn a few more tricks for working with your new database and table.

Chapter 3: Taking a Closer Look
(This is section 1 of 3 in this chapter)

In the previous chapter, you learned how to create both a database and a table, as well as how to enter and edit information in the table. In this chapter, you will build on the foundation laid in Chapter 2. By the time you finish this chapter, you will understand the following key concepts:

• •

How to change an existing table's structure The data types that Access supports

• • •

Why you should add comments to your table design What field properties are and how you can change them How to perform editing functions on entire tables

Modifying Your Table Design
In Chapter 2, you learned about the Design view, which lets you examine your table's structure without looking at the information it contains. For now, open the My Friends database you created in Chapter 2. To look at your table's structure (or design), select the table name from the Database window and then click your mouse on the Design button. Access, in turn, will display the Design view window shown in Figure 3-1.

Figure 3-1 The Design view window for the My Friends table. Note: The number and types of fields shown in your Design window may vary from those shown in Figure 3-1. Your fields will depend on how you answered the Table Wizard questions in Chapter 2. Using Design view, you can change your table's layout. You can add or edit fields, change the type of information stored in a field, add comments to your layout, and modify the properties of a field. The following sections examine each of these operations.

Adding and Editing Fields
In most ways, the Design view window operates like the Datasheet window. Access lets you add fields, move fields, or change field names without affecting the information contained in your current fields. To add a field, select the first blank row in the layout (this is the last row) or insert a row. To insert a row, select the field before which you want to insert the new field. Then choose the Insert Row option from the Edit menu or click your mouse on the Insert Row tool. When you insert a new field, Access moves all the subsequent fields down and inserts an empty field in the layout. For instance, assume that you want to add a field for the middle initial of your friends. Naturally, you would place this field between the first name and last name fields. To add the field, perform the following steps:
1. 2. Select the third row of the layout and make it the current row. (This is the row that defines the LastName field.) Click your mouse on the Insert Row tool. This adds a blank row between FirstName and LastName, as shown in Figure 3-2.

Figure 3-2 Inserting a blank row in your table layout. Now you can enter the new field name. In this case, type Middle Initial (you can include the space). That's all there is to adding a field. In the next section, you will learn how you can specify the type of information (text, numbers, and so on) that the new field will contain.

As you work with table fields, there may be times when you want to move a field. To move a field, select the field's row (make sure the entire row is highlighted) and then move the mouse point to the current record indicator at the left side of the record. (The current record indicator is the small triangle to the left of the record.) When you hold down the left mouse button, you can drag the field to any new position you desire. When you release the mouse button, Access moves the field to the new location. Moving a field does not harm or modify its data, nor does it change the data within any surrounding fields. Your field names should describe the data that your fields contain. Over time, the information you store in a table may change. To change the name of an existing field, simply select the name and edit it. For instance, if you want to change the name of the PostalCode field to Zip Code, select the existing field name and type in Zip Code. Likewise, you may want to change the name of the StateOrProvince field to simply State. The resulting changes to your table structure (after adding the Middle Initial field and changing the StateOrProvince and PostalCode field names) are shown in Figure 3-3.

Figure 3-3 The modified table structure. Over time, you may no longer need to track specific data. As such, Access also lets you delete fields. You delete a field in much the same way you would delete records in a table:
1. Using your mouse, select the entire row containing the field you want to delete. Press the DEL key or choose the Delete option from the Edit menu. If you are deleting a field that contains data, Access will ask you to confirm the deletion, as shown in Figure 3-4.

2.

Figure 3-4 Access asks you to confirm the deletion.

 To delete the field, along with all the information previously contained in it, click your mouse on the Yes button.
Note: If you delete a field by mistake, immediately use the Edit menu Undo option. If you make any other changes to your table structure, the deleted field (and its contents) cannot be recovered.

Changing the Information Type
In order to store a piece of information efficiently, Access needs to know its data type. The data type specifies the type of information the field will contain. Access lets you specify nine different data types, as detailed in Table 3-1.

Data Type AutoNumb er Currency Date/Time Hyperlink Memo Number OLE Object Text Yes/No

Meaning A sequential number Access fills in automatically as you add records. A number accurate to 15 decimal places to the left of the decimal point and 4 places to the right. A number representing any date or time between the year 100 and 9999. An active link to another document, URL, or location within a document. A long text string, up to 64,000 characters. A numeric value. Objects such as sound clips or graphical images. (OLE is a technical term for Object Linking and Embedding, which you learn more about in Chapter 17, "Using OLE.") Any characters, up to a length of 255 characters. A yes or no value.
Table 3-1 Data types supported by Access.

If you have been working with computers long, you may already be familiar with some of the data types listed in Table 3-1. These basic data types cover virtually every category of information you could electronically store. In the Data Type column of the Design view window, Access displays the current data type of each of your fields. If you want to change data types, select the data type entry for the field you want to change. For instance, assume you added a field called Christmas Card to your layout. The purpose of the field is to indicate whether this person is on your Christmas card list. This field is a great candidate for a Yes/No data type, but Access automatically sets the data type to text. As such, you must explicitly change it to a different data type.

To do this, select the Data Type column for the Christmas Card row. You will notice a small arrow appears to the right side of the data type. If you click your mouse on the arrow, a dropdown list of available data types appears. All you need to do is select one of the data types. In this case, select Yes/No. When you leave Design view, Access converts any values in the field. Note: Changing data types can be a time consuming process, depending on how many records there are in your table. When you change a data type, Access tries to convert any existing data in the field into the new data type. If it cannot make a conversion (for instance, to or from an OLE Object), you will lose the data previously stored in the field.

Adding Layout Comments
When you design a table, you decide which fields the table will store, as well as each field's data type. To help you explain your decision to others who may eventually need to update the table, Access lets you place comments about each field in the table. By reading your comments, you or another user quickly knows why specific choices were made. In Design view, you can place comments about your fields in the third column of each row. The contents of the description field do not affect any Access operations; the description field is simply a place where you document your thoughts or provide comments of a field's purpose. Sometime in the future, you may forget exactly why you included a particular field in your table. If you include a comment now, you are less likely to forget later. To add a comment, simply select the cell in the third column of the row that should contain the comment. You can then type anything, up to 283 characters in length. In Chapter 9, "Creating Custom Forms," you will learn how this description field can come in handy.

Understanding Properties
You probably noticed the area at the bottom of the Design window; this is the Field Properties area. Properties are simply characteristics associated with a field or an object. The Design window lets you change the properties associated with fields. The types of properties associated with a field depend on the field's data type. For instance, a text field has a Length property (also known as a Field Size property) which controls the number of characters the field can hold. An OLE Object (such as a sound or a picture), on the other hand, does not have such a property simply because a length would not make sense. To change a field's property, select the row that contains the field and then select or type the new property. Although many of the properties are self explanatory, some are not. The following sections explain the latter properties. Note: Changing some properties can result in a loss of data. For instance, if you shorten the length of a text field, then any fields that contain longer values will be truncated. In instances where you may lose data, Access will first warn you before it carries out the change.

Caption
A Caption is a label Access uses to refer to a field. In Datasheet view, the Caption appears above the field's column. Access also uses the Caption property in other places, such as in labels for controls. (You will learn more about controls in Chapter 9.)

The Caption property can be up to 255 characters. If you don't provide a Caption, Access uses the field name instead.

Format
The Format property describes the format Access uses for displaying a value in the field. The Format property only affects how Access displays data. The Format property does not change the actual value stored in your table. You can set different formats for different data types. For example, Access provides several different formats that control how a date is displayed. Likewise, for currency fields, you can display monetary values based on several different formats. To see the available formats for a particular field, select the Format property and then click your mouse on the arrow that appears to the right of the property box. For instance, if you select the Birthdate field (which is a Date/Time data type), and then click your mouse on the Format property, an arrow will appear to the right of the property box. If you click your mouse on the arrow, you will see a drop-down list of the available formats for the field. The format name is shown to the left of the drop-down list, while an example of the format's use is shown to the right. If a pre-defined format does not meet your needs, you can create your own custom formats. You can create a custom format by putting together a series of characters that specify how the values in the field should appear. The formatting characters you use will depend on the data type of the field: Numeric or Currency, Date/Time, Yes/No, or Text or Memo. The formatting codes for Numeric or Currency data types are shown in Table 3-2.

Character 0 # . , E-, E+, e-, e+ % \ -, +, $, (, ) *

Meaning Provides a place holder for a digit. Access displays either a digit or a zero. Provides a place holder for a digit. Access displays either a digit or a space. Decimal point. Thousand separator. Scientific notation. Number is a percentage and is scaled by 100. Display the next character as a literal. Display as a literal character. Use the following character as the fill character (for filling empty spaces in the format).

Table 3-2 Custom formatting codes for Numeric or Currency data types. For example, if you wanted to develop a custom format for currency so that the numbers 1234.5678 and -1234.5678 appeared as the following: *******$1,234.57 (*******$1,234.57) you would use the following in the Format property: **$#,##0.00;(**$#,##0.00)

Notice that there are actually two formats specified, separated by a semicolon. Access uses the format to the left of the semicolon if the value in the field is positive and the format to the right if the value is negative. Table 3-3 details the custom formatting codes you can use for fields with a Date/Time data type.

Characte r : / d dd ddd dddd ddddd dddddd w ww m mm mmm mmmm q y yy yyyy h hh n nn s ss tttt AM/PM am/pm A/P a/p

Meaning Time separator. Date separator. Display the day as a number without a leading zero. Display the day as a number with a leading zero. Display the day as three text characters (Sun, Mon, Tue, and so on). Spell out the full name of the day (Sunday, Monday, Tuesday, and so on). Use the short date setting from Windows Control Panel to display the full date (default is m/d/y). Use the long date setting from Windows Control Panel to display the full date (default is mmmm dd, yyyy). Display the day of the week as a number. Display the week of the year as a number. Display the month as a number without a leading zero. Display the month as a number with a leading zero. Display the month as three text characters (Jan, Feb, Mar, and so on). Spell out the full name of the month (January, February, March, and so on). Display the numeric quarter of the year. Display the year as a number. Display the year as two digits. Display the year as four digits. Display the hour as a number without a leading zero. Display the hour as a number with a leading zero. Display the minute as a number without a leading zero. Display the minute as a number with a leading zero. Display the second as a number without a leading zero. Display the second as a number with a leading zero. Display the full time using the time format specified in the Windows Control Panel. Use a 12-hour clock and display the time with the suffix AM or PM, as appropriate. Use a 12-hour clock and display the time with the suffix am or pm, as appropriate. Use a 12-hour clock and display the time with the suffix A or P, as appropriate. Use a 12-hour clock and display the time with the suffix a or p, as appropriate.
Table 3-3 Custom formatting codes for the Date/Time data type.

In most instances, the formats Access provides for the Date/Time data type will meet your needs. However, you may need to use a special format to fulfill a unique need, such as displaying the quarter of the year. For instance, suppose you wanted the dates 6/11/1999 and 1/15/2000 to appear as follows: Quarter 2, 1999 Quarter 1, 2000 In this case, you would use the following in the Format property: "Quarter "q", "yyyy Table 3-4 shows the custom formatting codes you can use when working with fields that have the Text or Memo data type.

Characte r @ & < >

Meaning Character required (either text from string or a space). Text character not required Force all characters to lowercase Force all characters to uppercase

Table 3-4 Custom formatting codes for the Text or Memo data types. When you compare them to other formatting characters, the custom text formatting characters are fairly easy to use. As an example, assume you want to format a value so it appears all in uppercase and so that three question marks appear if the field is empty. In this case, you would use the following Format property: >;??? The code to the left of the semicolon specifies the formatting Access will apply to a text value. The code to the right directs Access to display question marks if there is no text value. The result of this Format property might appear as follows: ??? WIDGET ??? COG ??? You can also create a custom Format property for the Yes/No data type. Access internally stores this data type as one of two values: -1 for Yes and 0 for No. A custom Format property for this data type has three parts, each separated by a semicolon. Access does not use the first part, which is left blank (apparently for future versions of Access). Access uses the second part if the value is -1 and the third part if the value is 0. For example, take a look at the following Format property: ;"Left";"Right" This Format property results in the text Left and Right being used instead of Yes and No. The text you use in your custom format is entirely up to you. You should note that when you are

entering data for this field in the Datasheet, you still work with it as a check box; if the check box is selected, then Access translates that to Left. If it is not selected, it translates to Right. Note: Remember that the Format property only affects how Access displays information; it does not modify the underlying values in the table.

Input Mask
Some data types let you specify an Input Mask property. An input mask guides users as they enter data. Perhaps one of the most common examples is when you enter a phone number. If you use an input mask, you might see something like this:

Notice that the parentheses are already in place for the area code and a dash appears in the proper place for the phone number itself. All the user has to do is type numbers. You specify this sort of Input Mask property as follows: !\(999") "000\-0000 You could probably tell from this example that you construct input masks in much the same way as format specifications (as covered in the previous section). In other words, you specify a series of characters that tells Access the way the mask should appear. Table 3-5 indicates the various characters you can use in the Input Mask property.

Characte r 0 9 # L ? A a & C . , / or : < > ! \

Meaning Digit placeholder (entry required; plus and minus signs are not allowed). Digit placeholder (entry not required; empty positions converted to spaces; plus and minus signs are not allowed). Digit placeholder (entry not required; empty positions converted to spaces, plus and minus signs are allowed). Letter placeholder (entry required). Letter placeholder (entry optional). Letter or digit placeholder (entry required). Letter or digit placeholder (entry optional). Placeholder for any character or a space (entry required). Placeholder for any character or a space (entry optional). Decimal point. Thousand separator. Date separator. Time separator. Force all following characters to lowercase. Force all following characters to uppercase. Fill input mask from right to left instead of left to right. Display the next character as a literal.
Table 3-5 Characters you can use in the Input Mask property.

Note: If you only need to put together a simple input mask, or you want to use a common one (such as a phone number, social security number, or zip code), you can use the Input Mask Wizard. You access this Wizard by clicking your mouse on the Builder button that appears at the right side of the Input Mask property box when the box is selected.

Validation Rule and Validation Text
As you have learned, input masks control the format with which users enter data. In addition to letting you specify how users enter data, Access also lets you specify the acceptable values for a field. You can do this by constructing validation rules. Validation rules, if you apply them properly, can be very powerful tools. They let you check a user's input as it is received into the field. For example, you can use a validation rule to make sure that a value is greater than 0 or greater than 100. You could use a different rule to ensure a value falls within a certain range. You construct validation rules like an equation. You use operators such as those shown in Table 3-6 to compare the field value and some other value. Note: Putting together validation rules may seem much like programming. There are many similarities. If you have no programming experience, and you feel that constructing validation rules is a bit over your head, don't worry--entire chapters could be written about putting expressions together. You can use Access successfully without ever putting together a validation rule. After you become more familiar with Access, and perhaps after you have worked through the latter chapters of this book (which cover programming topics), you may want to revisit this section and give validation rules another try.

Switch + / * Mod \ ^ = < > <= >= <> Betwee n And Eqv Imp Not Or Xor

Meaning Addition Subtraction Division Multiplication Modulus division (remainder) Integer division (whole numbers) Exponentiation Equal Less than Greater than Less than or equal Greater than or equal Not equal Between two values Logical And Logical Equivalence Logical Implication Logical negation Logical Or Logical Exclusive Or

Table 3-6 Operators you can use in making validation rules.

As an example, assume you want to check if a value being entered by a user is between 10 and 20. You can do this by typing the following equation in your Validation Rule property: Between 10 And 20 The Validation Text property is the message displayed by Access if a value does not meet your validation rule. In other words, if your validation rule checks whether a value is outside a certain range, and the value is outside that range, Access displays the validation text in a dialog box. As an example, let's assume that the following are your settings for the Validation Rule and Validation Text properties:

In this case, if you tried to enter a value lower than 20 in the field, then Access would display an error message dialog box, as shown in Figure 3-5.

Figure 3-5 A validation rule error message dialog box. Note: Some people like to use the Access Expression Builder to create validation rules. While it can be helpful in some instances (such as if you are creating a very complex expression), for most purposes it is more trouble than just entering an validation rule directly. If you decide you want to try out the Expression Builder to put together a validation rule, simply click your mouse on the Builder button at the right side of the Validation Rule property box. This button is visible when you select the Validation Rule property box.

Chapter 4: Creating Another Database
(This is section 1 of 4 in this chapter)

In Chapter 2, "Access in an Hour," you learned how to perform all the basic Access functions. You created both a database and a table, and you learned your way around the Datasheet. In this chapter, you will revisit many of the same topics covered in Chapter 2, but from a different angle. Here you will learn how to create a table from scratch, instead of using the Table Wizard. You will find this exercise both instructive and enlightening. By the time you finish this chapter, you will understand the following key concepts:

How to create a database

• • •

How to design your own table How to layout your table using the Design view window How to test your table layout

A Brand New Database
To begin, you will create a new database. As you will recall from Chapter 2, you can create a database by following these steps:

 Click your mouse on the New tool on the toolbar. Access displays the New dialog box, as shown in Figure 4-1.

Figure 4-1 The New dialog box.

 Make sure the Database object is selected, then click your mouse on OK. Access displays the File New Database dialog box, shown in Figure 4-2.

Figure 4-2 The File New Database dialog box.
1. If necessary, change the drive and directory for the new database. In the File Name box, enter the name Customer. This is the name you will use for the database in this chapter. Click your mouse on Create. After Access creates the requested database file, you will see the Database window shown in Figure 4-3.

2.
3.

Figure 4-3 The Database window that appears after Access creates your Customer database.

Chapter 4: Creating Another Database
(This is section 2 of 4 in this chapter)

Creating a Table From Scratch
At this point, you are ready to create a table for your new database. In Chapter 2, you used the Table Wizard to make creating a table easy. This is not to imply that creating a table manually is difficult; on the contrary, it is a sure-fire way to understand your tables intimately. In the rest of this chapter, you will learn how to develop your own table without using the Table Wizard. In doing this, you will use much of the information you learned in Chapter 3, "Taking a Closer Look." This table will contain a list of your customers. (If you don't have a business, just pretend you do.) Even though you could use the Table Wizard to construct this table, the result wouldn't be nearly as well-suited to your needs as a manually created table. In order to manually create a table, you need to go through primary stages: design and layout. The following sections discuss these stages in detail.

The Design Stage
You seldom use the computer during the design stage. Instead, you sit down with a pencil and paper and decide what information you want in your database. Specifically, you determine

what data you need to track and the characteristics (data types, formats, and so on) of that data. Although there are many books on the market which discuss the theory and practice of database design, you don't need to read them to design a simple database. All you need to do is ask yourself (and answer) the following questions:

• • • • • •

What should I call my table? What pieces of data will I need to maintain? What data type is best suited for each piece of data? How much storage space should be set aside for each piece of data? How should the pieces of data be organized? Do I need a unique key (a primary key) for my data?

There are many other questions you can ask yourself. In fact, you could pull together quite a bit of detailed information before you even sit in front of the computer. But if you can at least answer the above questions, you will be able to put together your first table.

A Table Design Form
As mentioned in the previous section, the best way to design your table is with a pencil and paper. With that in mind, you might want to create a table design form you can use as you work on your design. Figure 4-4 shows an example of such a form.

Figure 4-4 A sample table design form. You should first determine a name for your table. Since this table will contain a list of business customers, use Business Customers as the table name. Write that name at the top of the design form.

Planning the Table Fields
On a scrap sheet of paper, write the names of the fields you'd like to use in your table. Don't worry about the order they are in; consider this a "brainstorming session." For your field names, you might include company name, address (perhaps two address lines for long addresses or suite numbers), phone number, fax number, contact name, and so on. Table 4-1 shows a list of field names which you can use for the Business Customers table. If you have not already done so, you should enter each of these field names in your table design form. Normally, you would enter the field names you wrote down during your brainstorming session. However, so your database matches the examples shown in this book, use only the field names listed in Table 4-1.

Field Name Company Name Address 1 Address 2 City State Zip Code Main Phone Fax Number Account Number Salesperson Credit Limit Account Opened Last Order Active Contact Contact Phone Comments

Meaning Name of the company First address line Second address line City State Zip Code Phone number for the company switchboard Fax phone number Customer account number Who handles the account The amount the account can buy on credit Date the account was set up Date of last order Specifies if this is an active or inactive account Company contact name Phone number for the contact Anything necessary to record

Table 4-1 Field names for the Business Customers table.

Picking Data Types
A field's data type specifies the type of value the field will hold, such as date, text, or currency value. Your next step in defining your table is to determine each field's data type. Table 4-2 lists the Access data types.

Data Type AutoNumb er Currency Date/Time Hyperlink Memo Number OLE Object Text Yes/No

Meaning A sequential number Access fills in automatically as you add records. A number accurate to 15 decimal places to the left of the decimal point and 4 places to the right. A number representing any date or time between the year 100 and 9999. An active link to another document, URL, or location within a document. A long text string, up to 64,000 characters. A numeric value. Objects such as sound clips or graphical images. Any characters, up to a length of 255 characters. A yes or no value.
Table 4-2 Data types supported by Access.

For most of the fields in your table, you will use a Text data type. But for some, you will find other data types more appropriate. For instance, you should use a Yes/No data type for the

Active field, the Date/Time data type for the Date Opened and Last Order fields, and a Memo data type for the Comments field. Table 4-3 lists the data type you should use for each field in your table. You should write these data types in the Type of Data column of your table design form.

Field Name Company Name Address 1 Address 2 City State Zip Code Main Phone Fax Number Account Number Salesperson Credit Limit Account Opened Last Order Active Contact Contact Phone Comments

Type of Data Text Text Text Text Text Text Text Text AutoNumber Text Currency Date/Time Date/Time Yes/No Text Text Memo

Table 4-3 Data types for the fields in the Business Customers table. Notice that Table 4-3 assigns the AutoNumber data type to the Account Number field. Because Access automatically calculates this data type with each new record, you can leave it up to Access to assign your customer account numbers.

Determining Field Lengths
As shown in Table 4-2, a text field can hold up to 255 characters, and a memo field can hold up to 64,000 characters. Your next step in your table design is to determine the lengths of your fields. For some fields, you won't need to do this. For example, neither the Credit Limit field nor the Active field will have a length because their data types don't require lengths. However, you will have to specify lengths for all the other fields. To appropriately determine maximum field lengths, you need to thoroughly understand the data your table will contain. For instance, you need to know if you will maintain a 5-digit or a 9digit ZIP code for your customers. Likewise, you should know how much space you need in the Company Name and Address fields. If you thoroughly understand your data, you can choose the lengths that best meet your needs. Although Access will always let you change field lengths, even after you start entering data in the table, you should think about your data now in order to avoid frustration and duplicate work later. Table 4-4 lists the maximum field lengths you should use for the Business Customers table.

Field Name Company Name Address 1 Address 2 City State Zip Code Main Phone Fax Number Account Number Salesperson Credit Limit Account Opened Last Order Active Contact Contact Phone Comments

Max Length 35 40 40 25 2 9 10 10 (not applicable) 35 (not applicable) (not applicable) (not applicable) (not applicable) 35 10 (not applicable)

Table 4-4 Field lengths for the Business Customers table.

Ordering Your Fields
Finally, you need to determine the order in which you want the fields to appear in your table and specify that order on your table design form. Remember, earlier in the design process you wrote down fields in the order that they popped into your head. Because ordering fields (like selecting field lengths) is a subjective process based solely on your needs and the needs of your organization., you normally might place the fields in the order they occurred to you during your brainstorming session. In the Order column of your table design form, you would place a 1 next to the first field, a 2 next to the second field, and so on. However, for our purposes here, use the order specified in Table 4-5. Enter this information into the Order column of your table design form.

Field Name Company Name Address 1 Address 2 City State

Orde r 2 3 4 5 6

Zip Code Main Phone Fax Number Account Number Salesperson Credit Limit Account Opened Last Order Active Contact Contact Phone Comments

7 8 9 1 16 13 12 14 15 10 11 17

Table 4-5 Field order for the Business Customers table. Before you enter the information on your table design form into Access, you should decide whether you want to use a primary key for your table. In Chapter 2 you learned that Access uses a primary key to order records and locate them quickly. The primary key is typically a unique value (meaning there can be no duplicates in the table), but it does not have to be. In the Business Customers table, there are several fields that you can use as a key. For instance, you could use the Company Name field as a key. However, since you could have two companies with the same name, you may want to look for another key that is more unique. If you cannot find another, then you could use the Company Name field and just allow for duplicates. You can also use the Account Number field as a key. By definition, this number should be unique. Remember, Access automatically creates the account number for you using a AutoNumber field. And because the Account Number is unique, it makes an ideal primary key. To remind yourself that the Account Number field will be your primary key, circle that field's number (1) in the Order column of your table design form.

The Layout Stage
After you finish designing your table, you are ready to use Access. With the table design form in hand, you can quickly and easily layout the database. All you need to do is type, in the proper order, the information from the table design form to the Design view window. To create the Business Customers table, follow these steps:
1. 2. 3. If necessary, start Access and load the Customer database. This is the database created earlier in this chapter. From the Database window, select the Tables button. There should be no tables listed since none have been created yet. Double-click your mouse on the Create Table in Design View option. Access displays the Design view window shown in Figure 4-5.

Figure 4-5 The Design view window. In Chapter 3 you learned how to use the Design view window to change the layout of an existing table. Here you will use it to create a new table. In this case, the Design view window is completely empty. Notice also that the cursor is blinking in the first row, in the Field Name column. Access is ready for you to layout your table. To start, enter the first field name in your table's order: the Account Number field. Type Account Number and then press ENTER. After Access accepts the field name, the cursor moves to the Data Type column for that field. Your Design view window should now look like what is shown in Figure 4-6. Notice that Access assumes this will be a text field and that default properties for the Text data type appear at the bottom of the Design view window.

Figure 4-6 The Design view window after entering the first field name. In your table design form, you specified that the Account Number field is an AutoNumber. To change the field's data type, click your mouse on the arrow at the right side of the Data Type cell. Access, in turn, displays a drop-down list of data types. Click your mouse on the AutoNumber data type. The cell should now display AutoNumber as the data type for the Account Number field. Your Design view window should look like the window shown in Figure 4-7. Also, notice that, along with the data type, Access has changed the field's properties.

Figure 4-7 The Design view window after changing the data type. When you press ENTER, your cursor moves to the Description column. You can enter comments about the field, if you wish. For the Account Number field, you may include a comment that reminds you or another user that this field is a primary key. When you press ENTER again, Access takes you to the next row, in the Field Name column. Enter the name of your second field (Company Name) and then specify its data type. Do this in the same way that you entered the last field's information. Before you leave the row containing the Company Name field, take a look at the properties shown at the bottom of the Design view window (see Figure 4-8). You already know that these are defaults for the Text data type. Notice that Access sets the Field Size property to 50.

Figure 4-8 The Design view window after specifying the data type for the second field. Since you specified on the table design form that the length should be only 35, you will need to adjust the Field Size property. Use your mouse to select the Field Size property and change it to 35. Then, use your mouse to return to the Field Name column, in the third row, and enter your next field. Go ahead and enter all 17 fields in the Business Customers table. As you enter each field in the table, make sure you set the Data Type property and change the Field Size property, when necessary. When you are done, your Design view window should appear as shown in Figure 49.

Figure 4-9. The Design view window after entering the last field. After you finish entering all your fields, you must specify your primary key. To do this, scroll back through the table design until you can see the Account Number field on the screen. Select the field and then click your mouse on the Set Primary Key tool. In response, Access will display a key in the gray area to the left of the Account Number row (see Figure 4-10).

Figure 4-10 The finished Design view window. You have now laid out your entire table, and you are ready to save your work to disk. To save your table, simply close the Design view window. To do so, click your mouse on the Close button in the upper-right corner of the window. In response, Access asks if you want to save your work (see Figure 4-11).

Figure 4-11 Access asks if you want to save your work. To save your work, click your mouse on the Yes button. Access then asks you to specify a name for the table, as shown in Figure 4-12.

Figure 4-12 Saving your table contents to a named table. Type the name you wrote at the top of your design form: Business Customers. Press ENTER or click your mouse on the OK button to save your table under that name. Note: If you save the table under the wrong name, don't let it bother you. You can use the information presented in Chapter 3 to rename your table.

Chapter 4: Creating Another Database
(This is section 3 of 4 in this chapter)

Testing Your Layout
Now that you have defined your table layout, it is time to test it out and refine it. You can do this easily using the Datasheet for the table. Display the Datasheet for the Business Customers table (refer to Chapter 2 if you need help with this). You should see a blank Datasheet, as shown in Figure 4-13.

Figure 4-13 The Datasheet for the Business Customers table. Take a moment to look at the Datasheet's appearance. If the field names don't appear like you want them to, you can switch to the Design view window and make changes to the Caption properties of the fields whose labels you find unsatisfactory. (You may remember from Chapter 3 that Access displays the Caption property at the head of each Datasheet column.) To switch between the Datasheet and Design view windows, use the leftmost tool on the toolbar. (This tool switches between Datasheet and Design views.) That is how you refine your table; you enter information into the Datasheet, observing what you can make better. Then, you switch to the Design view window to make the change. When the change is made, switch back to the Datasheet to keep entering information. This iterative process helps you to quickly make your table best suit your needs. With your table design complete, enter a record. For your first record, use the information shown in Table 4-6.

Field Name Account Number Company Name Address 1 Address 2 City State Zip Code Main Phone

Value (Access will fill this in) Birch Enterprises 3456 Maple Lane PO Box 549 Kansas City MO 64133 8162375632

Fax Number Contact Contact Phone Account Opened Credit Limit Last Order Active Salesperson Comments

8162375633 Ed Birch 3/2/99 1500 7/8/99 Yes (select the check box) John Davis

Table 4-6 Information for the first Datasheet record. As you enter information, you may come up with a way to make data entry easier. For example, entering phone numbers could be easier if you set the Input Mask property for the phone number fields. Switch back to the Design view window and select the first phone number field (Main Phone). You can set the input mask for this field by following these steps:
1. 2. Click your mouse on the Input Mask box at the bottom of the Design view window. You should see a Builder icon (a button with three dots on it) appear at the right side of the box. Click your mouse on the Builder icon. You should see the Input Mask Wizard dialog box appear. Select the Phone Number input mask, as shown in Figure 4-14.

Figure 4-14 The Input Mask Wizard dialog box.

 Since you want to use the input mask for phone numbers, click your mouse on the Next button. The Wizard then displays the dialog box shown in Figure 4-15.

Figure 4-15 You can change the placeholder character with this dialog box.
1. 2. If you desire, change the placeholder that the input mask will use (the default is the underline). Click your mouse on the Next button. The Wizard will then display the dialog box shown in Figure 4-16.

Figure 4-16 You can change how Access stores information with this dialog box.
1. Specify how you want Access to store information saved through the input mask. You can save the phone number with or without the formatting characters (the default). Since the field length

2.

for your phone numbers is only 10 characters long, you should save the value without the formatting characters. Click your mouse on the Next button. Access displays the final dialog box for the Input Mask Wizard, as shown in Figure 4-17.

Figure 4-17 The final Input Mask Wizard dialog box.

 Click your mouse on the Finish button. Access then updates the Input Mask property in the Design view window so it appears as follows:
!(999) 000-0000;;_ You can repeat steps 1 through 8 for each of the other phone number fields, as well. When you move out of the Input Mask property field, notice that Access reformats the mask so it appears as follows: !\(999") "000\-0000;;_ Access adds the extra characters so that the mask conforms with its own internal specifications. When you are done adding your input masks, click your mouse on the Datasheet view tool. In response, Access asks you if you want to save your changes to the table layout (see Figure 418).

Figure 4-18 Access asks you if you want to save your changes. Click your mouse on Yes to save your changes and return to the Datasheet. You can now enter another record to see if your improvements worked.

Chapter 4: Creating Another Database
(This is section 4 of 4 in this chapter)

Summary
Although Access provides many different tools (such as Wizards), you don't need to use these tools to create a great database. Instead, you can manually create and refine tables that will work just as well. And as you work with Access in this way, you will become more aware of its capabilities and how to use them. In this chapter, you have manually developed and refined a database. Before you move on to Chapter 5, make sure you understand the following key concepts:

• • • • • •

Designing a table requires some planning. You need to examine what data you are going to manage, as well as the data's characteristics. Using a table design form makes designing your table easier. When you manually create a table, you use the Design view window to specify your layout. As you input fields in the Design view window, you may need to change the data types and properties which Access presets. The process of refining your table layout is iterative in nature. It involves working alternately in both the Datasheet and Design view windows, repeatedly switching from one window to the other until your design is correct. As you work in the Datasheet, you should look for ways to improve the layout of your table. Switch to the Design view window to make your changes, and then back to the Datasheet to test those changes.

In Chapter 5, you will learn more about what Access can do. Specifically, you will learn how you can sort and filter the data in your table.

Chapter 5: Sorting and Filtering Information
(This is section 1 of 3 in this chapter)

To let you quickly manipulate information in a table and display it in a more organized fashion, Access provides two special tools: sorting and filtering. Using these tools, you can work with

your information in whatever way makes sense to you. This chapter will explain how sorting and filtering work, as well as how you use these tools to work with your data. By the time you finish the chapter, you will understand the following key concepts:

• • • • • • • •

What sorting is How to perform a simple sort What a simple sort is How to construct a complex sort How to remove the effects of a sort What a filter is How to filter your data How to save a filter

Sorting Your Data
Sorting is the process of putting your data into a logical order. We each sort things every day. For example, you may arrange your personal bills by date so that you can handle the most urgent ones first. You may arrange your video tapes in alphabetical order or your music collection by artist name. In your kitchen, you may arrange your silverware into bins: forks in one bin, spoons in another, knives in a third. In each of these cases, you are sorting, although you may not think of it that way, at first. With Access, you can perform simple and complex sorts The next two sections will address each of these sort operations in detail.

Simple Sorting
Within Access, you will sort (or order) records in a table based on one or more fields. For example, you might sort records by last and first names so that all your friends with the last name Smith are sorted by their first names. A simple sort orders records based on the contents of one or more adjacent fields. When you perform a simple sort, you must specify the order in which you want Access to sort your records:

• •

Ascending order. This means that Access will arrange the records in order from lowest to highest. Thus, Access would arrange alphabetic characters from A to Z and arrange numeric information in increasing numeric order. Descending order. This is the opposite of ascending order, and is sometimes called reverse order. Access will order information from highest to lowest: alphabetic information from Z to A, and numeric information from greatest to least.

For example, assume you have a table of your favorite video tapes. If you want to order the records based on the titles of your video tapes, you ask Access to perform a simple sort. To perform a simple sort based on a single field, select a field (cell) in the column by which you want to sort. Next, use either of the following actions, depending on how you want to sort:

To sort in ascending order, click your mouse on the Sort Ascending (A-Z) tool (see Figure 5-1). You can also select the Sort option from the Records menu and then choose Sort Ascending.

To sort in descending order, click your mouse on the Sort Descending (Z-A) tool (see Figure 5-1). You can also select the Sort option from the Records menu and then choose Descending Sort.

Figure 5-1 The sorting tools on the toolbar. When you perform the sort, Access reorganizes your table so that the records are in the order you specified. Note: Depending on the number of records in your table, it may take some time for Access to sort your data. The length of time necessary also depends on the speed of your computer and hard drive. You can also use the simple sorting feature to sort by more than a single field. However, these columns must be adjacent to each other, and each field must be sorted in the same manner (ascending or descending). In other words, you can't sort one column in ascending order and the other in descending order. If you want to sort one column in one order and another in a different order, you will need to use a complex sort, discussed later in this chapter. Also, if the columns are not adjacent to each other, you must use a complex sort. As an example of using the simple sorting feature with adjacent columns, assume you have assigned numbers to every tape in your video collection. Also, assume each record represents a single show. (Keep in mind that Access may use the same tape number in more than one record. This would happen when a video tape contains more than one show.) What if you want to sort the table by tape number and then show name? If the columns that contain these two fields were adjacent to each other, you would follow these steps:

 Move the mouse pointer so it is over the column name (at the top of the Datasheet) for one of the columns by which you want to sort. When you do this, the pointer changes to an arrow pointing down.
1. Click your mouse on the left mouse button. This selects (highlights) the column. Hold down the SHIFT key and repeat steps 1 and 2 with the other column. Access will highlight all the columns between the first and the second one you selected. Use one of the sorting methods (ascending or descending) described in the previous example.

2.
3.

When you perform a simple sort using multiple columns, Access works from left to right through your table. Thus, Access first sorts the leftmost column, moves to the right and sorts the next column, and so on.

Moving Columns
Before you can perform a simple sort on multiple fields (columns), you may need to move the columns so they are adjacent. When you are working in the Datasheet, you can use the mouse to reorganize (move) columns in your table. To move a column, follow these steps:

1. Select the column you want to move and leave the mouse pointer on that column. 2. Hold down the left mouse button. A small, shaded box will appear around the tail of the mouse pointer. 3. Drag the column to a new position in the table. As you move the mouse left or right, Access highlights the lines between columns to indicate where the column will be moved. 4. When the mouse pointer is over the position you desire, release the mouse button. Access, in turn, moves the column to the new position.

Complex Sorting
In the last section, you learned about simple sorting. Complex sorting goes a step further. You can use a complex sort to sort multiple, non-adjacent columns in different orders (ascending and descending). For example, if you have a table of names and addresses, you may want to order the records first by ZIP code and then by last name (within ZIP code). If the two columns are not adjacent (with the ZIP code column to the left of the last name column), you need to use a complex sort. To accomplish a complex sort, you use the Filter window. (This may sound like a strange place, but it is--nonetheless--the proper place. Filters are described in detail later in this chapter.) To display this window, select Filter from the Records menu, and then Advanced Filter/Sort from the resulting submenu. Access displays the Filter window, shown in Figure 5-2.

Figure 5-2 The Filter window. To perform a complex sort, you need to choose which fields you want to sort and then specify how to sort them. For example, assume you are working with the Business Customers table (developed in Chapter 4, "Creating Another Database"), and you want to sort it first by ZIP Code and then by company name. To do this, you need to specify these fields and their sort order in the grid at the bottom of the Filter window. In the first column of the Filter window, click your mouse on the arrow at the right of the field box for that column and select the Zip Code field.

Next, you need to tell Access you want the Zip Code field sorted in ascending order. To do this, click your mouse in the Sort row beneath where you placed the Zip Code field and then click your mouse on the arrow at the right of the cell. Access, in turn, provides a drop-down list from which you can pick Ascending as the sort order. After you select the order for this sort, the Filter window will appear as shown in Figure 5-3.

Figure 5-3 The Filter window after specifying the first field to sort by. In the second column, you can specify the second field by which you want to sort (Company Name) and how you want to sort it. If you want to sort the table in other ways, you can continue to select other fields and specify how to sort them. When you are done, you need to apply the sort to the table. To apply the sort, select the Apply Filter/Sort option from the Filter menu, or click your mouse on the Apply Filter tool. Access then displays the records in your table in the specified order. Within the Filter window, there are other ways you can add fields to the sort, other than the method you just used (clicking your mouse on the pull-down arrow for the field list). For example, if you double-click your mouse on a field name in the list at the top of the Filter window, the field name will appear in the first empty column in the Field row. As shown in Figure 5-4, for example, if you double-click your mouse on the field name Main Phone, Access will place the field name into the first unused filter column.

Figure 5-4 You can double-click your mouse to select fields. You can also use your mouse to drag field names from the field list to the grid area. All in all, Access provides three or four different ways to pick the fields you want to sort by. If you later want to remove a field from the sort, simply use your mouse to select the column containing the field (in the grid area), and then press the DEL key or choose Edit menu Delete Column option. Note: To undo the effects of a sort, choose the Remove Filter/Sort option from the Records menu.

Chapter 5: Sorting and Filtering Information
(This is section 2 of 3 in this chapter)

Filtering Your Data
In the previous section, you learned how to use the Filter window to create complex sorts. You can also use the Filter window to create filters. A filter lets you limit the records you view. Filtering does not remove records; it only lets you specify which records you want Access to display. When you define a filter, you specify criteria that data must meet before Access will display it. For example, one filter might list records for employees that live in New York. Access displays only those records that meet your filter criteria. In effect, you create a subset of your data. You can then work with that subset as if it were a table: you can add records,

delete records, and make any changes you desire. Access updates the original table to reflect each change you make. You use filters to make your data more manageable. As an example, assume you were working with a list of your customers, and you replaced one of your sales representatives. Now you must update the customers table to reflect the new sales rep. You could use the Replace function (described in Chapter 2, "Access in an Hour"), but you may instead want to create a filter so you can review customers before you actually reassign them. In this case, your filter would limit the records Access displays to only the records for the old sales rep. In most cases, it is much easier to work with this smaller set of records than with the entire customer table.

Creating a Filter
To create a filter, you again need to work with the Filter window. Click your mouse on the Filter option on the Records menu, and then choose Advanced Filter/Sort from the resulting submenu. Access displays the Filter window, shown in Figure 5-5.

Figure 5-5 The Filter window. To create a filter, all you need to do is pick a field and specify the criteria you want applied to the field. If you desire, you can also specify how you want the field sorted. (Thus, you can both filter and sort at the same time.) As an example, assume you have a table that contains your inventory records, similar to the table shown in Figure 5-5. In this table, one of the fields is named Supplier. You use this field to keep track of who supplies a particular item. Assume you need to look at which items are supplied by Allied Industries. Using a filter, you can restrict the records Access

displays to only those records for which the supplier is Allied Industries. To create such a filter, you would follow these steps:
1. 2. Choose the Filter option on the Records menu, and then choose Advanced Filter/Sort from the resulting submenu. In the first column of the grid at the bottom of the window, choose Supplier as the Field. In the Criteria cell for the column, enter an equal sign, followed by the words Allied Industries. When you press ENTER, the Filter window should appear as shown in Figure 5-6. (Note that Access adds the quotation marks around the supplier name automatically.)

3.

Figure 5-6 Creating a simple filter.

 Click your mouse on the Apply Filter tool on the toolbar.
Access applies your selection rules (as defined by the filter) and displays only those records for Allied Industries. The resulting subset of your inventory table is much easier to work with than the entire table. In this case, your filter instructs Access to examine each record in the table and to display only those records for which the Supplier field contains (is equal to and hence the equal sign) the name Allied Industries. Note: The filters you create can be as simple or as complex as you desire. You specify criteria for your filter using the same regular expressions you used to create validation rules for your fields (Validation rules were discussed fully in Chapter 3, "Taking a Closer Look".) If you need help remembering the expressions you can use, refer back to the section on validation rules.

Removing a Filter
It is not uncommon to use a filter to quickly view your data in a particular way, and then want to remove the filter to view all your records. Access makes this very easy, and you can accomplish this task using either of two different methods.

The first (and perhaps easiest) method involves the toolbar. When a filter is active, the toolbar contains a tool that looks like a funnel. Actually, there are three tools that contain funnels, but one tool that contains just a funnel, without any adornment. This tool looks like it is selected. If you position the mouse pointer over the tool, you will see that its name is Remove Filter. Click on the Remove Filter tool, and all your records again appear; the filter is completely removed. The other method of removing a filter is to use the menus. On the Records menu is an option entitled Remove Filter/Sort. If you choose this option, all your records are again displayed.

Saving a Filter
Access only retains your filter until you leave your table. Thus, if you close your table and later decide you want to use the filter again, you will need to create the filter again from scratch. This can be both frustrating and time consuming. To get around this problem, you can save a filter as a query. You will learn all about queries in Chapter 6, "Using Queries to Examine Your Data." Filters are closely related to queries, although they are not nearly as versatile or flexible. Since filters and queries are related, Access lets you store a filter as a named query. To do this, make sure you have the Filter window displayed. Then, select the Save As Query option from the File menu, or click your mouse on the Save As Query tool on the toolbar. In response, Access displays the Save As Query dialog box, as shown in Figure 5-7.

Figure 5-7 The Save As Query dialog box. Enter the name by which you want to save this filter. The name can be any name you desire. As an example, if you want to save the filter that displayed the inventory items supplied by Allied Industries, you can use the name Supplied by Allied Industries. Type the name and press ENTER or click your mouse on the OK button. To later reuse the filter, display the Filter window and select the Load From Query option from the File menu. Access displays the Applicable Filter dialog box, as shown in Figure 5-8.

Figure 5-8 The Applicable Filter dialog box. Highlight the name of the query you want to load as a filter and choose OK. Access loads the filter. You can then use the filter or make any other changes to the filter you desire.

Chapter 5: Sorting and Filtering Information
(This is section 3 of 3 in this chapter)

Summary
Access lets you quickly and easily sort and filter your data. These two closely related operations can help you manage your data more efficiently. This chapter has taught you how you can both sort and filter information. Before you move on to Chapter 6, make sure you understand the following key concepts:

• • • • • • • •

Sorting places data in a specific order. Information can be sorted either in ascending order (from smallest to largest) or descending order (from largest to smallest). You can use the simple sort feature to quickly sort a table based on the contents of one or more adjacent fields. You can use a complex sort to sort multiple fields that are not adjacent. You can also use a complex sort to sort multiple fields in different orders (ascending and descending). To create a complex sort, use the Filter window. A filter is a way to temporarily limit what records Access displays. A filter creates a subset of your table. You create a filter by specifying criteria for Access to use when selecting records to display. You specify the criteria using the Filter window. A filter is lost when you close a table. You can save and later reuse a filter if you save it as a query.

In Chapter 6, you will learn about a topic that is closely related to filters--queries.

Chapter 6: Using Queries to Examine Your Data
(This is section 1 of 4 in this chapter)

In the previous chapter, you learned how you can use sorting and filtering to better manage the information in your tables. In this chapter, you will learn about another, more powerful tool provided by Access--queries. Like filters, Access queries let you select specific data from your table. However, queries are much more powerful than filters. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • •

What queries are What types of queries Access lets you create How Access stores queries How to use the Query Wizards How to design your own queries How to use the QBE grid How to set properties in a query

Understanding Queries
In normal English, a query is a question. Effectively, that is also what a query is in Access. In short, Access lets you pose a question which is designed to return a number of records from a table. For instance, "show me the orders entered in the last 30 days that have a value over $5,000." This is a query, and it is one you can easily create in Access. Although you may not be able to state your query in the conversational style just used, Access gives you quite a bit of latitude in determining which records you want to view.

Types of Queries
Access supports five different types of queries. Each query type performs a fundamentally different operation in relation to the data in a table. These five types of queries are:

• • • •

Select query. This is the simplest type of query you can create. A Select query returns a number of records based on criteria you specify. In many instances, this type of query is very similar to a filter (covered in the previous chapter). Action query. This query performs an action on the data retrieved. For instance, the query could create a new table and move records to it, or it could simply delete records in the current table. Crosstab query. This query presents information in a spreadsheet-like manner--similar to the Datasheet. (Crosstab is a compound word formed from cross-tabulation.) You can use this query to calculate subtotals and totals based on the contents of various fields. Parameter query. This query works in conjunction with other types of queries. When you execute this type of query, Access asks you to answer a set of questions. The query uses the answers to those questions to finish its work. You can think of this type of query as a "query with a user interface." SQL query. To formulate some types of queries, you must use SQL (structured query language). SQL provides a way to communicate with other types of databases or specialized database servers on networks.

You may get the idea from looking at this list that you can use Access queries to do much more than just ask questions about data (and you would be right). Queries can do more--much more. The type of query you use will depend on what you want to accomplish with the query. As you work through this chapter, you will learn how to create many different types of queries.

As you know, Access lets you query information in tables. In addition, Access also lets you query information in queries. While this may sound redundant, querying a query is actually a powerful and useful operation. A query often returns a subset of information in a table; in effect, this subset is a table in its own right, even if it is a table based on another table. You can use this subset as the basis for further queries. Thus, you can query data from a table and then query the query. You can even query the result of the second query! Access is quite powerful and flexible when it comes to what you can actually query.

How Access Stores Queries
In Access, you save your queries with your database; they are another component of your database, another object, just like tables, reports, or macros. In Chapter 5, "Sorting and Filtering Information," you learned that you can save a filter as a query, and that, in doing so, you save it using a name. You can see what queries have been defined for a database by clicking your mouse on the Queries button in the Database window. When you are working with a new database or one that has no queries defined, Access displays nothing in this window except the options allowing you to create new queries, as shown in Figure 6-1.

Figure 6-1 A Database window with the Queries button selected. Notice that when you click your mouse on the Queries button, Access makes the same toolbar buttons available as when you click your mouse on the Tables button. In Access, you perform many of the same actions to control queries as you do to control tables. For example, you can copy queries, rename them, and delete them. You can also create new queries and design (change) existing queries. Access provides you with two ways to create queries--you can either use one of the query Wizards or you can create a query from scratch. In the rest of this chapter, you will learn how to create queries using both of these methods.

Note: For the examples in this chapter, you use the database you created in Chapter 4, "Creating Another Database." If you did not create that database and table directly, you may want to load it off of the CD-ROM; it is called Customer 1.mdb. You can save this file out under a different name, preferably Customer.mdb.

Chapter 6: Using Queries to Examine Your Data
(This is section 2 of 4 in this chapter)

Using the Query Wizards
Access actually includes four different query Wizards, and there is only one place you can see them all. Choose Query from the Insert menu. Access displays the New Query dialog box, as shown in Figure 6-2.

Figure 6-2 The New Query dialog box. Notice that there are five options listed in the dialog box. The first option (Design View) is not really a query; it allows you to create a query from scratch. The purpose of the other four options is as follows:

• • • •

Simple Query Wizard. This option allows you to create a very simple select query. This Wizard is the same one that is run if you double-click your mouse on the Create Query By Using Wizard option in the Database window. (See Figure 6-1.) Crosstab Query Wizard. This option creates a simple Crosstab query, as defined earlier in the chapter. Find Duplicates Query. Creates a Select query that displays duplicate records in a table. Find Unmatched Query. Creates a query that displays records that have no relationship with records in different tables. (This query is useful when working with relational databases, a concept introduced in Chapter 13, "Understanding Data Relationships.")

Your query selection will determine which Wizard is used by Access. In the following sections, you will learn how to use three out of the four Wizards represented here. (It is premature to

discuss the Find Unmatched query in this chapter, but you will understand how such a query is used after learning the information in Chapter 13.)

Creating a Simple Query
To create a simple query, select this option from the New Query dialog box and then click your mouse on the OK button. As an alternative, you can also double-click your mouse on the Create Query By Using Wizard option, available when the Queries button is selected in the Database window. Access then displays the Simple Query Wizard dialog box shown in Figure 63.

Figure 6-3 The Simple Query Wizard dialog box. At the left side of the Simple Query Wizard dialog box, you can use the drop-down list to specify what Access should use as the source for this new query. Remember that you can use either tables or other queries as your source. Since the Customers database has only one table (the Business Customers table), Access has already selected it for you. If you were working with a database that contained several other tables and queries, you could use the drop-down list to change what the Wizard uses as a data source. In the lower-left portion of the dialog box is a list of all the fields defined in the selected table or query. If you change the data source in the pull-down list (described in the previous paragraph), then the available fields change, as well. You can use the list, and the controls just to the right of the list, to indicate which fields you want included in the query you are creating. For the purposes of this example, select the Company Name, Address 1, Address 2, City, State, and Zip Code fields. Make sure that after selecting each one, you click your mouse on the > button. Access then displays the name of each field in the Selected Fields list at the bottom right of the dialog box. When you are done selecting these fields, the dialog box appears as shown in Figure 6-4.

Figure 6-4 The Simple Query Wizard dialog box after selecting some fields. With the fields selected, you are ready to proceed to the next step in the Wizard. Click your mouse on the Next button, and Access displays the dialog box shown in Figure 6-5. This is the final dialog box in this Wizard.

Figure 6-5 The final dialog box for the Simple Query Wizard.

Here you can enter a name that you want used to save your query. The suggested name is simply the table name on which the query is based (Business Customers) to which the word "Query" is added. The simple query you created using the previous dialog box can be used as the basis for mailing labels, since it only contains the address information for your customer list. Enter the name Address List, and then click your mouse on Finish. Access displays the results of your query, as shown in Figure 6-6.

Figure 6-6 The results of the Simple Query Wizard.

Creating a Crosstab Query
To create a Crosstab query, first open the New Query dialog box by choosing Query from the Insert menu. Then select the Crosstab Query Wizard option from the New Query dialog box and then click your mouse on the OK button. Access displays the Crosstab Query Wizard dialog box shown in Figure 6-7.

Figure 6-7 The Crosstab Query Wizard dialog box. At the top of the Crosstab Query Wizard dialog box, Access asks you to select the table from which you want to query data. Since this database has only one table (the Business Customers table), Access has already selected it for you. If you were working with a database that contained several other tables and queries, you could use the View area in the center of the dialog box to change what the Wizard displays as data sources. At the bottom of the dialog box, the Wizard displays how your finished Crosstab query will appear. Access updates this representation every time you make a change while you are using the Wizard. To proceed to the next dialog box (shown in Figure 6-8), click your mouse on the Next button.

Figure 6-8 Getting ready to select rows. A Crosstab query lets you define what should appear in both the rows and columns of the resulting display. Using the Wizard, you should indicate which fields you want to appear in each row. For this example, assume you want to tabulate the number of active and inactive accounts you have, by sales rep and by state. In the Available Fields list, you should select the Salesperson field and then click your mouse on the > button. Notice that the Wizard updates the sample crosstab at the bottom of the dialog box. Next, you should select the State field and click your mouse on the > button. Again, notice that the Wizard updates the sample crosstab at the bottom of the dialog box, as shown in Figure 69.

Figure 6-9 The crosstab query after selecting row heading. You have just told Access to display the salesperson and the state on the left side of the crosstab. Click your mouse on the Next button. Access displays the dialog box shown in Figure 6-10.

Figure 6-10 Getting ready to select columns. In this dialog box, Access asks you to select which fields it will tabulate into the columns. Since you want this query to indicate how many accounts are active and inactive for each sales rep, you should select the Active field. After you select the Active field, notice that Access updates the sample at the bottom of the dialog box. Click your mouse on the Next button when you are ready to proceed. Access displays the dialog box shown in Figure 6-11.

Figure 6-11 Picking an operation for the query. In this dialog box, Access asks you to specify what you want the query to calculate. At the right side of the dialog box, the Functions box lists all the different operations available to you. You can perform any of these operations on any field you like, although some combinations of fields and operations may not make sense. For example, if you choose a text field, performing an average on it doesn't make sense; you should only perform that operation on a numeric or currency field. To finish out this query, select the Company Name field and then choose the Count operation. Notice that the information in the sample area of the dialog box changes with your selection. The query will then calculate how many active and inactive accounts (there is a company name for each account) there are in each state for each sales rep. Finally, notice that there is a check box you can use to indicate whether the query should provide a summary for each row. For a simple crosstab that only has two real columns (active or inactive), a summary is probably unnecessary. Click your mouse on the check box to turn the summary off. After the selection is made, you can click your mouse on the Next button. Access will display the dialog box shown in Figure 6-12.

Figure 6-12 Access asks you to name your query. At this point, you are done creating the query, and you need to save it. In this dialog box, Access asks you what name it should use for the query. As with names for other parts of your database, you should use something that is descriptive, such as Active Account Tally. Type in this name and click your mouse on the Finish button. Access saves your query, updates the query list in the Database window, and then opens the query to show you the results of your work. Figure 6-13 shows an example of how this query should turn out.

Figure 6-13 Results of the new query. When you look at the crosstab, one of the first things you will notice is that the columns have headings of -1 and 0. Remember that the Active field is a Yes/No field, which means Access stores this field's values as one of two numbers: -1 and 0. As such, these numbers are also what Access displays in the headings. Later in this chapter, you will learn how to change the headings by changing the properties associated with the query. If you look past this oddity in the headings, you can see that the crosstab provides the information you requested. It shows the number of accounts on active (Yes, or -1) and inactive (No, or 0) status for each state and each rep. The Crosstab Query Wizard made it easy for you to create this view of your data.

Creating a Find Duplicates Query
You can also use the New Query dialog box to create a Find Duplicates query, which lets you quickly search a table for duplicate fields. Select the Find Duplicates Query Wizard option in the dialog box and click your mouse on the OK button. Access, in turn, displays the Find Duplicates Query Wizard dialog box shown in Figure 6-14.

Figure 6-14 The Find Duplicates Query Wizard dialog box. In this dialog box, Access lists the data sources you can use for this query. Typically, you would use a table as your data source, but with this Wizard you can also use another query. Because the Business Customers table is the only item in the Customers database, Access has already selected this table for you. You can click your mouse on the Next button to move to the dialog box shown in Figure 6-15.

Figure 6-15 Selecting a field to check. In this dialog box, Access asks you which fields you want to check for duplicate values. Although there is no limit to the number of fields you can have your query check, you should select only those fields necessary to ensure there are no duplicates. The more fields you ask your query to check, the longer it will take to run. In the case of the Business Customers table, you will probably only need to check the Company Name and Contact fields to determine if there are duplicates. First, select Company Name and click your mouse on the > button. Next, select the Contact field and click your mouse on the > button. Access then displays the two fields, in order, in the list at the right side of the dialog box. After you specify these fields, click your mouse on the Next button. Access displays the dialog box shown in Figure 6-16.

Figure 6-16 Displaying additional information. In this dialog box, Access asks if you want your query to display any additional information. When you review duplicate records, you can use this information to help you determine which ones you want to save and which you want to delete. For the Business Customers table, you may want Access to display the Contact Phone, Last Order, and Salesperson fields. Select a few of these fields (remember to click your mouse on the > button after you highlight each one). Access adds each field's name to the list at the right side of the dialog box. When you finish picking your fields, click your mouse on the Next button. Access displays the dialog box shown in Figure 6-17.

Figure 6-17 Finishing up the query. Now that you have finished creating your query, Access asks you to give it a name. Since the Wizard's suggested name for this query is quite descriptive, click your mouse on the Finish button to tell Access you accept it. Access saves your query under this name and updates the query list in the Database window. Access opens the query to show you the results of your work. Figure 6-18 shows an example of how this query should turn out.

Figure 6-18 The results of the Find Duplicates query. You can now review the duplicates and decide what you want to do with each of them. If you delete any record in the display, Access will delete the corresponding record in the underlying

table (Business Customers). For this reason, Find Duplicates queries can be very helpful in managing the information in your table.

Chapter 6: Using Queries to Examine Your Data
(This is section 3 of 4 in this chapter)

Designing Your Own Queries
Up to now, you have used the Query Wizards to create your queries. This is great, but the Query Wizards only create a few specialized queries. If one of those queries does not meet your needs, you can create your own query from scratch. Many people think that developing queries from scratch is one of the most difficult parts of working with Access. In many ways they may be right. But with a little guidance and a lot of practice, you can develop just about any query you can imagine. As you create queries from scratch, you will learn many principles which you can also apply when you edit existing queries. Either way--creating or editing--you will use the query Design window. The process you follow to create a query depends, in large part, on the type of query you want to create. Regardless of the query type, you should always start in the same way: select the Queries button in the Database window and then double-click your mouse on the Create Query in Design View option. Your screen will then appear as shown in Figure 6-19.

Figure 6-19 Creating a query. Notice that the query Design window is in the background. In the foreground, Access displays the Show Table dialog box, which asks you to select the data sources you want to use. As in any other query, you can select either tables or queries as data sources (notice the tabs in the dialog box). In this case, select the Business Customers table and click your mouse on the Add button. Access opens a small field window for the table, keeping the Show Table dialog box open. To close the Show Table dialog box, click your mouse on the Close button. After you close the Show Table dialog box, Access displays the query Design window in the foreground. In the middle of the Design window is a space for the data sources you have opened. In this case, the space contains the Business Customers table. Below that is the QBE (query by example) grid. The query Design window should appear as shown in Figure 6-20.

Figure 6-20 The query Design window. In the QBE grid, you specify the criteria for your query. The options available in the grid will depend on the type of query you are creating.

The Query Design Toolbar
The query Design toolbar appears just below the menu whenever you are using the query Design window. Many of the tools on the toolbar should already be familiar, as they are used in other Access windows. Other tools, however, are unique to the query Design toolbar. Table 6-1 shows these unique tools and what they accomplish.

Tool

Name Query Type Run Show Table Totals Top Values Propertie s Build

Purpose Allows you to specify the type of query you are creating. Executes the query, if necessary. Displays the Show Table dialog box, as shown earlier in Figure 6-19. Displays the totals row of the QBE grid. Not available when creating an Update or Delete Query. Finds the top values in a query based on a percentage or number of rows. Displays the Properties window, either for query fields or the query itself. Displays the Builder window, which some people prefer when creating criteria for a field.
Table 6-1 Query design tools.

You may often use the Query Wizards to create your run-of-the-mill queries, which means you will use the query Design window for those that are more specialized. Thus, you will normally use the Query Type tool at the beginning of your design session to indicate what type of query you are creating. If you click your mouse on the down-arrow at the right side of the tool, you can see that Access allows you to create the following types of tools:

Select Query. The default query type. Lets you develop a query that returns a number of records that meet your criteria.

• • • • •

Crosstab Query. Modifies the QBE grid so its options represent those needed for a Crosstab query. Make-Table Query. An Action query that creates a new table. Access uses this table to store the results of the main part of the query. When you choose this tool, Access asks you the table name and where Access should store the table. The QBE grid is the same as for a Select query. Update Query. An Action query that updates numerous records in a table. Access modifies the QBE grid to represent the query's needs. Append Query. An Action query that appends records to a table. When you choose this tool, Access asks you the table name and where it should store the table. Access modifies the QBE grid to reflect the criteria for this type of query. Delete Query. An Action query that deletes records. Access modifies the QBE grid to a simpler format than the other queries.

These query types should look familiar; they are basically the same as those discussed at the beginning of this chapter.

Using the QBE Grid
Regardless of the type of query you create, you will always use the QBE grid to design the query. You can manipulate items in the grid in the same ways you can in other Access design grids. For example, in Chapter 5, "Sorting and Filtering Information," you learned how you can use the Filter window grid. The QBE grid works the same way; you can add columns, delete columns, and move columns around. You can also select cells in the grid and edit them. The meaning of the rows in the QBE grid will vary depending on the type of query you are designing. The top row, however, always contains a field name. Each column in this row represents a field that Access will show in the query result. Thus, if you want to display five fields in your query result, you would use five columns in the QBE grid. Access displays fields in the same order you place them in the grid. However, how you set the Show row for each field controls whether or not Access displays the field. (see Figure 6-21).

Figure 6-21 The QBE grid. If you select Show row for a particular column, Access displays that field in the results. If you don't select the Show row, Access doesn't display the field. Why would you not want to display a field? Simple; if you are using a column only to select or sort records, you don't need to display it. For example, if you select records based on the value of a Yes/No field (and select only those records with the field set to Yes), you know that every value in that results field will be the same (-1). As such, there is really no need to display the field. You can use the Show row to hide or display fields as you desire. Note: The Show row is not applicable to or available with the Crosstab, Update, Append, and Delete Query tools. The Sort and Criteria rows work the same as described for filters in Chapter 5, "Sorting and Filtering Information." If you are still not clear on their proper use, refer back to that chapter.

Perhaps the best way to discover how to use the QBE grid is just to experiment with it for a while. Assume that you want to create a query that will use the Business Customers table and display all the active accounts with credit limits over $1,000 for one of your sales reps (John Davis). Further, you want the accounts listed in descending (highest to lowest) order based on their credit limit (for those accounts with the same credit limit, you want them shown alphabetically by company name). To design such a query, you would follow these steps:
1. 2. 3. Start with an empty QBE grid. Use the Query Type tool to make sure a Select Query is being designed. (It should be, by default.) In the field list, double-click your mouse on the Active field name. Access displays this field name in the first column of the QBE grid. In the Criteria row for the first column, enter the equation = Yes. This tells Access you want only those records with the Active field set to Yes. Clear the check box in the Show row for the first column. This tells Access not to display the value of this field. In the field list, double-click your mouse on the Salesperson field name. Access displays this field name in the second column of the QBE grid.

4.
5. 6.

7.

In the Criteria row for the second column, enter the equation = "John Davis". This tells Access you want only those records that have noted John Davis as the sales representative. 8. Clear the check box in the Show row for the second column. This tells Access not to display the value of this field. 9. Double-click your mouse on the Credit Limit field name. Access displays this field name in the third column of the QBE grid. 10. In the Sort row for the third column, choose Descending.

11. In the Criteria row for the third column, enter the equation > 1000. This indicates you want only
those records with credit limits over $1,000. 12. Double-click your mouse on the Company Name field name. Access displays this field name in the fourth column of the QBE grid. 13. In the Sort row for the fourth column, choose Ascending. 14. Double-click your mouse on the Contact field name. Access displays this field name in the fifth column of the QBE grid. 15. Double-click your mouse on the Contact Phone field name. Access displays this field name in the sixth column of the QBE grid. 16. Double-click your mouse on the Last Order field name. Access displays this field name in the seventh column of the QBE grid.

You are now done with the query. Your query Design window should look like what is shown in Figure 6-22.

Figure 6-22 The completed query. This query displays five fields from the Business Customers table, but only for those records that meet your criteria. You can save the query right away, if you desire. It may be better, however, to see the results of your query. To do so, click your mouse on the Run tool on the toolbar. Access runs the query, without saving it, and displays the results. This way you can check your work to see if the result is what you expected. Figure 6-23 shows the results of your new query.

Figure 6-23 Query results. When you finish examining the query results, you can use the View tool (left side of the toolbar) to switch back to the query Design window. You can then save your query by closing the Design window. At this point, Access asks if you want to save your query. Click your mouse on the Yes button, and Access prompts you for a name to use (see Figure 6-24).

Figure 6-24 You can save your query using whatever name desired. Enter the name you want Access to use for the query. In this case, you may want to enter the name Active Davis Accounts Over $1000. When you click your mouse on the OK button, Access saves the query and updates the Database window to reflect the addition of the new name.

Setting Properties
In Chapter 3, "Taking a Closer Look," you learned about field properties and how they can affect your Datasheet. The properties described in Chapter 3 apply to fields in a table, but you can also set properties for fields in a query. Query properties affect how Access displays the information the query derives, as well as how you can edit that information.

To see the properties for a particular field, select the field in the QBE grid. Then, click your mouse on the Properties tool. Access displays a Properties window; the exact appearance of the window will depend on the data type of the field. A sample Properties window is shown in Figure 6-25. (These properties are for the Credit Limit field.)

Figure 6-25 The Properties window for the Credit Limit field. Notice that there are fewer properties than you would see in the table Design window. The properties shown, however, function exactly the same way as they do when you work with tables. (You may want to refer back to Chapter 3 for a refresher.)

Chapter 6: Using Queries to Examine Your Data
(This is section 4 of 4 in this chapter)

Summary
Queries are powerful tools that you can use to select, change, and manipulate information in your database. From a data source (a table or another query), a query selects records according to criteria you specify. What Access does with those records depends on the type of query you design. In this chapter, you have learned the fundamentals of queries, and a little bit more. When you combine the information in this chapter with what you have learned in previous chapters, you will find that queries are not as mysterious or difficult as you may have first suspected. Before you move on to Chapter 7, make sure you understand the following key concepts:

• •

A query is a way for you to pose questions to Access about the information in your tables. Access lets you create five types of queries: Select, Action, Crosstab, Parameter, and SQL.

• • • • •

Queries are another part of your database, just like tables, reports, and macros. You can use the Queries button in the Database window to view which queries are in your database. You can use Query Wizards to create four different types of queries. They are instructive (in what the queries accomplish) and a good starting place for your own custom queries. You can use the query Design window to design your own queries from scratch. This window, along with the query Design toolbar, lets you specify and create any type of query you can imagine. The QBE (query by example) grid appears at the bottom of the query Design window. Using the grid, you can specify which fields Access will use to determine if records should be displayed, as well as additional records which Access will display in the query results. When you design a query, you can specify properties for the fields in the query result. These field properties are similar to those you set when you design a table.

In Chapter 7, you will learn how you can print either your table or your query results.

Chapter 7: Printing Your Table
(This is section 1 of 5 in this chapter)

Many computer users don't give much thought to what happens after they get their information in a database. Very often, getting information into a database is only half the story--you also need to worry about printing out information so you can share your data with others. In this chapter, you will learn how you print the information contained in your database. You will learn concepts that apply whether you are printing a simple table or a complex report. However, this chapter is only the start. Two other chapters in this book deal with Access report writing capabilities. Between these three chapters, you will have all the tools you need to create any type of output you desire. By the time you finish this chapter, you will understand the following key concepts:

• • • • •

How to print your table's contents How to select a printer How to specify the way in which your information should print How to print an object's layout How to use print preview to save paper

Printing a Table
As just discussed, after you enter data into your table, you will want to print a copy of your table's contents. Access makes it very easy to print the data in your table. One way you can print your table is to follow these steps:
1. 2. 3. Choose the Tables button in the Database window. Select (highlight) the name of the table you want to print. Select the Print option from the File menu or click your mouse on the Print tool on the toolbar.

Another way to print your table is to follow these steps:

1. 2. 3. 4.

Choose the Tables button in the Database window. Select (highlight) the name of the table you want to print. Click your mouse on the Open button. Access displays the Datasheet for the table; you can make any changes to the table you desire. Select the Print option from the File menu or click your mouse on the Print tool on the toolbar.

Regardless of which method you use, Access understands that you want to print. If you click your mouse on the Print tool on the toolbar, Access tries to print your table right away. If you select the Print option from the File menu, Access displays the Print dialog box, as shown in Figure 7-1.

Figure 7-1 The Print dialog box. The various parts of the Print dialog box control what Access prints. If you want to control which printer Access uses to print your table, use the Name pull-down list at the top of the dialog box. If you want to change specific attributes of the printer, click your mouse on the Properties button. Access, in turn, will display the Properties dialog box for the printer, described later in this chapter. The Print Range area lets you choose the parts of your table Access prints. You can print your entire table, part of your table (the rows or columns you have selected), or a range of pages. Later in the chapter, you will learn how to preview your printout on your screen before you print it. Using this print preview, you might decide to print a specific range of pages. The Pages option lets you specify a range of pages to print. The Copies area lets you tell Access how many copies of your information to print. The Collate check box only has meaning if you are printing more than one copy of the table. For example, assume your table takes five pages to print completely. If you need two copies and you have selected the Collate check box, Access will print all five pages of the table (one copy) and then print the five pages again (the second copy). If you don't select the Collate Copies check box, Access prints two copies of each page, in turn. In this instance, you will need to manually collate the copies later.

Finally, in the center of the right side of the Print dialog box is a check box called Print to File. This check box lets you divert your printout from the printer to a disk file. Printing to disk is actually quite powerful. For instance, assume you need to send a report to someone across the country. You can print it out and fax it to them or send it through the mail system. However, if you print the report to a file, you can instead send them the disk containing the file, or you could e-mail the print file to them. If the report is large, sending an e-mail or a disk could save lots of money in shipping charges. As a result, they get the information faster and at a lower cost. When they receive the file, they can send it to their printer and have a copy the same as if you had mailed a printed copy to them. When you finish telling Access what to print, click your mouse on the OK button. Access will send your information to the printer according to your specifications. If you selected the Print to File check box, Access will ask you for a file name to use, as shown in Figure 7-2.

Figure 7-2 The Print To File dialog box. This is a standard file control dialog box. You can use the controls it provides to select a drive and folder for your output file. All you need to do is type the filename you desire and click your mouse on the OK button. Access will send printer output to the disk file you specified. If your computer has access to multiple printers or if you need to better control your printout (perhaps by changing your page orientation or margins), you can use the Print Setup dialog box discussed next).

Chapter 7: Printing Your Table
(This is section 2 of 5 in this chapter)

Controlling the Printed Page
Access, like many programs, takes full advantage of the Windows environment. Access uses the printing facilities provided by the Windows environment, which means it can use any printer Windows can use. While you cannot add or delete printers within Access (this is done from Windows itself), you can control to which printer Access sends your information and

which features of that printer Access uses. To control Access printer operations, you use the Page Setup dialog box. You can get the Page Setup dialog box from many different places in Access. Any time you are getting ready to print, there is normally a tool or menu option that will let you view the Page Setup dialog box. In most cases, however, you will get to the dialog box by selecting the Page Setup option from the File menu. The Page Setup dialog box is shown in Figure 7-3.

Figure 7-3 The Page Setup dialog box. The following sections discuss the various parts of this dialog box in detail.

Setting Printout Margins
The first of the two tabs available in the Page Setup dialog box is Margins (see Figure 7-3). Depending on the data you are printing, you may need to change the default page margins. Margins are the white space left at each side of your paper. The Margins tab of the Page Setup dialog box lets you control how much of a margin you want left on your printed page. To set a margin, simply change the setting shown for each of the four margins. The default margins are 1" on each side of the paper. You can change these margins to any logical value you desire; simply select the box whose margin you want to change, and type in a new one. Note: Some printers require a certain margin. For instance, some laser printers cannot physically print within a quarter-inch of the paper's edge. In these instances, Access will not allow you to change the margins to less than the physical limitations of the printer.

Setting Page Specifications
Using the Page tab of the Page Setup dialog box (see Figure 7-4), there are numerous ways you can change how Access actually puts information on the physical page.

Figure 7-4 The Page tab of the Page Setup dialog box. Note: The options available in the Page Setup dialog box will depend, in large part, on your printer type. If some setup options are not available to you, don't worry. Because of the printer you are using, you probably have options available that other users don't have. The first setting you can specify in the dialog box is the orientation of your paper. Actually, this is a bit of a misnomer. The paper itself is not oriented; it is the image created by Access that is oriented in relation to the paper. To select landscape or portrait orientation, use the settings in the Orientation area, at the top of the dialog box. Printed materials typically use a portrait orientation. This simply means that when you are reading the paper, the narrow side is at the top and bottom of the page. If you turn the page 90° to the left or right, the page is in landscape mode--the narrow side is at the left and right sides of the page. Landscape mode is handy when you are printing tables that have many columns in them. If your printer supports multiple paper sizes, such as standard and legal sheets, you may need to select the paper size you desire. To select a paper size, change the information in the Paper area of the dialog box. Depending on your printer, you may be able to change both the size and source of the paper. (There are controls for each of these in Figure 7-4.) You should select

the paper size and source that makes the most sense for the type of output you want to create.

Changing Your Printer
A printer driver is special software that Windows uses to communicate with your printer. Most printers require their own special software (in other words, their own printer driver). It is not unusual to have several Windows printer drivers installed in your system. Many people have at least two printers, such as an ink jet and a laser printer. Others will have even more drivers installed so they can use their friends' printers. For instance, if someone else in your company has a high-resolution laser printer, you might have a printer driver installed for that printer, even if you don't have direct access to the printer. Instead, you can send the output to a file and then physically take the file to the other printer to print the output. Access lets you select any printer whose driver has been installed in Windows. In short, selecting a printer driver tells Access which printer you want to use. You select a printer driver in the Printer area of the Page tab of the Page Setup dialog box. (See the bottom of Figure 74.) You will normally select the Default Printer setting. With the Default Printer setting, Access uses the printer you have setup as the default printer in Windows. You can, however, select a different printer by choosing the Use Specific Printer option button. The Printer button then becomes active and you can use it to select the printer you want to use. When you click your mouse on the Printer button, Access displays a new Page Setup dialog box that allows you to specify the printer you want to use. (Why the same name is used for two dialog boxes is a mystery.) See Figure 7-5 for an example of this dialog box.

Figure 7-5 The Page Setup dialog box. The pull-down list at the top of the dialog box shows the name of each printer connected to your PC, as well as the names of the printer drivers you have installed. Regardless of which printer you choose, Access will remember your choice and use that printer until you select another. You may have noticed that this second Print Setup dialog box (Figure 7-5) is similar to the top of the Print dialog box (Figure 7-1). Access allows you to specify which printer you want to use

in both locations. Changing the printer in one dialog box is the same as changing it in both of them. Note: Changing your printer will affect the options available in the various Page Setup areas of Access. Generally, Access only displays setup options applicable to the current printer. Don't worry if your setup dialog boxes change when you change printers. When you are done specifying the printer you want to use, click your mouse on the OK button. You can then continue to set other print options, as desired.

Controlling Printer Properties
There are hundreds (if not thousands) of different printers on the market. This is one reason that Windows requires printer drivers in order to communicate with a particular printer. To complicate matters, each of those printers has different capabilities. For instance, different printers will handle different types of paper. Some printers can print in color. Other printers offer different options for printing graphics. The list is endless. Access allows you to change the specific properties used by a printer. You can do this by clicking your mouse on the Properties button in either the Print dialog box (Figure 7-1) or the Page Setup dialog box (Figure 7-5). Access displays the Properties dialog box for a printer. The actual appearance of this dialog box varies completely, depending on the printer you are using. Figure 7-6 shows an example of the Properties dialog box for the Epson Stylus Photo 700 printer.

Figure 7-6 The Properties dialog box for the Epson Stylus Photo 700 printer. This particular printer is a color ink-jet printer. Thus, many of the controls in the Properties dialog box are used to specify how color should be printed on the page. Other controls allow you to specify the trade-off between quality and speed. Still other tabs allow you to indicate other ways in which the printer should be used. To provide a contrast, take a look at Figure 7-7. It shows the Properties dialog box for an HP LaserJet printer. Notice that the controls in this dialog box are completely different from those in Figure 7-6. This is, again, because the capabilities of the two printers are completely different.

Figure 7-7 The Properties dialog box for an HP LaserJet printer. The controls in the Properties dialog box for the HP printer allow you to put multiple images on a single sheet of paper and control other printer features. Most of the time you won't need to fiddle with the settings in your printer's Properties dialog box. Instead, you will typically use the default settings determined by Windows. You should understand that the dialog box is available, however, as using it is the primary way you have to take advantage of advanced printer features. The best way to find out what the controls do in your printer's Properties dialog box is to experiment a bit. You won't hurt the printer, and you may discover new and exciting ways to utilize the features inherent in the printer.

Chapter 7: Printing Your Table
(This is section 3 of 5 in this chapter)

Printing Your Layout
Access lets you print the layout of your tables or any other database object you might have defined (such as queries, forms, or macros). You do this using a feature which Access calls the Documenter. To print a layout report, choose the Analyze option from the Tools menu. Access displays a submenu from which you should choose Documenter. Access then displays the Documenter dialog box, as shown in Figure 7-8.

Figure 7-8 The Documenter dialog box. If you had a particular object selected before you invoked the Documenter, the type of object you selected controls which tab of the Documenter dialog box Access displays. You can specify a different object type, however, by simply clicking your mouse on the appropriate tab. Access then displays the defined objects that fit the category you selected. Next, select the check box next to the object or objects for which you want a report printed. For instance, if you wanted a layout report for the Business Customers table (Figure 7-8), then you should make sure the check box to the left of the Business Customers object is selected. Next, click your mouse on the Options button. Access displays the Print Table Definition dialog box, shown in Figure 7-9.

Figure 7-9 The Print Table Definition dialog box. The Print Table Definition dialog box lets you specify the parts of the table definition you want Access to print. In the first section, you specify the table attributes you want Access to include in the printout. The other two sections let you indicate the level of detail for both fields and indexes. (Indexes are an advanced form of keys for tables. They are not used very often, except for specialized databases. You create indexes when you are designing your table.) Make your adjustments, if necessary, and then click your mouse on the OK button. Note: If you choose to print the layout of different database objects (besides tables), Access replaces the Print Table Definition dialog box with an appropriate dialog box for the selected object type. Now click your mouse on OK in the Documenter dialog box. Access examines your table (or other database objects you selected) and produces a definition printout. After a short time, Access displays the printout on your screen, as shown in Figure 7-10.

Figure 7-10 The Print Definition for a table. Before printing the definition to the printer, Access gives you a chance to see it on the screen. This viewing process is a print preview; it lets you make sure everything is OK before you actually print a copy of the printout. Notice that Access greatly reduces the number of toolbar tools at the top of the screen. There are only a few tools which you will typically use in this display:

• • • •

Print. This tool displays the Print dialog box, with the ultimate goal of sending the report to the printer. Zoom. There are actually two zoom tools. One looks like a magnifying glass; it alternates between close-up and full-page views of the printout. The other is the percentage tool; it allows you to indicate a specific percentage for zooming. Pages Per Screen. These are actually three tools, called One Page, Two Pages, and Multiple Pages. These three tools allow you to specify how many pages are displayed on your screen at the same time. Close Window. This tool cancels printing the report and returns you to the Database window.

In addition to the Zoom tool, you can also use your mouse to zoom in and out. Notice that when you move it over the page, the mouse pointer appears as a magnifying glass. If you click your mouse, Access alternately displays a close-up or full-page view. When you first saw the report (as shown in Figure 7-10), it was in close-up view. If you want to view it as a full page, simply click your mouse on the page with the mouse. Your screen will then appear as shown in Figure 7-11.

Figure 7-11 Full-page view of the definition printout. If you use the mouse pointer to click your mouse on the page again, you will see a close-up view of the page, at the point where you clicked on the mouse pointer. When you are ready to print the definition, click your mouse on the Print tool. Access, in turn, prints the report to your default printer. If, instead of printing, you want to close the printout display, simply click your mouse on the Close tool.

Chapter 7: Printing Your Table

(This is section 4 of 5 in this chapter)

Using Print Preview
In the previous section, you learned a bit about Access print preview capabilities. Print preview lets you review your printout before you waste paper and time at the printer. Thus, it helps to make you more productive. You can choose the Print Preview option from the File menu at any time you desire. For instance, if you choose it while you are in a Datasheet, you can see what your table will look like when it is printed. An example of this is shown in Figure 7-12.

Figure 7-12 Print preview of a table. Like the tools in the previous section, the Print Preview tools let you examine your output before it goes to the printer.

Chapter 7: Printing Your Table
(This is section 5 of 5 in this chapter)

Summary
Access relies heavily on the Windows environment when it comes to printing. This chapter has introduced you to the basics of printing within Access. The knowledge you have gained in this chapter will serve you well as you work through later chapters on printing reports. Before you move on to Chapter 8, make sure you understand the following key concepts:

• • • • • •

Access lets you use any printer you can use with Windows. Many of the printing options available will depend on your printer type. To change the printer you are using, choose the Page Setup option on the File menu or click your mouse on the Page Setup tool or the Setup button (if either of these are available). The options in the Page Setup dialog box let you specify how you want your information printed. You can use the Page Setup dialog box to specify the type of paper to use, where it should come from, how it should be oriented, and what margins to use. Access lets you easily print the contents of a table. To do so, select or display the table. Then, choose the Print option from the File menu or click your mouse on the Print tool. You can easily print a report that describes a database object, such as a table. To do so, choose Analyze from the Tools menu and Documenter from the resulting submenu. Print Preview is a feature that lets you see your output on the screen before you send it to the printer.

In Chapter 8, you will learn how you can create simple structured reports with Access.

Chapter 8: Creating Simple Reports
(This is section 1 of 5 in this chapter)

In the previous chapter, you learned the basics of printing within Access. In this chapter you will build upon that knowledge to create an actual report. In the simplest sense, a report is a formatted output of your data. Using reports, you can select the fields you want Access to print, as well as their formats. By the time you finish this chapter, you will understand the following key concepts:

• • • • • •

What a report is Ways to create reports How to start the Report Wizards The types of reports you can create with the Report Wizards How to create a simple tabular report How to create mailing labels

What is a Report?

An Access report differs from a simple printout of a table's contents. When you create a report, you also can add headers, footers, subtotals, and other special features that enhance the appearance of your data on the printed page. Using a report, you have complete control over how your information appears. Many database programs include special report writers that are designed to make the process of creating reports faster and easier. Access is no exception. Access includes a built-in report writer that you can use to design your reports and customize them to your heart's content. For those times when you have very simple report-writing requirements, you will appreciate the inclusion of the Access Report Wizards. In this chapter, you will learn how to use the Report Wizards. How to use the report writer (the report Design window) is covered fully in Chapter 14, "Creating Complex Reports." In Access, reports are simply another part of your database. If you look at the Database window, you will see there is a Reports button. Select this tab to display the reports stored with your database. As with any other database object, you can copy, rename, and delete reports very easily. Exactly how you perform these operations was covered in Chapter 3, "Taking a Closer Look."

Chapter 8: Creating Simple Reports
(This is section 2 of 5 in this chapter)

Creating a Report
Note: This section uses the My Friends database you created in Chapters 2 and 3. If you do not have that database available, you may want to use the Friends 1.mdb database located on the CD-ROM. Copy the file to a directory on your hard drive and rename it My Friends.mdb. To start the Report Wizard, first make sure that the Reports tab is selected in the Database window. Then, double-click your mouse on the Create Report By Using Wizard option. Access displays the Report Wizard dialog box, as shown in Figure 8-1.

Figure 8-1 The Report Wizard dialog box. At the left side of the Report Wizard dialog box, you can use the drop-down list to specify what Access should use as the source for this new report. Remember that you can use either tables or other queries as your data source. Since the My Friends database has only one table (the My Friends table), Access has already selected it for you. If you were working with a database that contained several other tables and queries, you could use the drop-down list to change what the Wizard uses as a data source.

Choosing Information to Include
In the lower-left portion of the dialog box is a list of all the fields defined in the selected table or query. If you change the data source in the pull-down list, then the available fields change, as well. You can use the list, and the controls just to the right of the list, to indicate which fields you want included in the report you are creating. Since this is a simple report, you might use the fields in this order:

• • • • • • • •

Last Name First Name Address City State Zip Code Home Phone Work Phone

To tell Access you want to use a field in your report, click your mouse on a field name (in the order shown here) and then click on the > button. Access will move the field from the left column to the right. Repeat this process until you have selected and transferred all the fields listed above. When you are done, click your mouse on the Next button. Access, in turn, displays the dialog box shown in Figure 8-2.

Figure 8-2 Picking which fields to group by.

Grouping Information
Your next step is to determine if you want your information grouped in any particular order. Grouping is routinely done in financial reports, although it is by no means limited to financial reports. For instance, if you were creating a report that showed sales for a particular company, you might want the information in the report grouped by month, by salesperson, or by territory. In creating a simple report based on the My Friends table, grouping is probably not necessary. To show how it works, however, let's say you had a large number of friends in your database and you wanted to group them by state. To do this, simply choose the State field at the left side of the dialog box and click your mouse on the > button. The Report Wizard dialog box changes to show how your grouping will now occur, as shown in Figure 8-3.

Figure 8-3 Grouping helps you organize the output in a report. Notice that the State field is moved to the top of the sample report layout, and the Grouping Options button becomes active. For some types of reports, you may want to change the grouping options. Doing so allows you to specify how Access should analyze the field by which you are grouping. In this case, changing the grouping options is not necessary. To proceed to the next step, click your mouse on the Next button. Access displays the dialog box shown in Figure 8-4.

Figure 8-4 Picking which fields to sort by.

Sorting Information for Your Report
Now you need to decide how you want the information in your report sorted. Access allows you to specify up to four fields by which sorting can be done. In most instances you will use only one or two fields. It makes sense that if you are creating a list of your friends, you would want them in alphabetical order by name. Thus, you should choose the LastName and FirstName fields. Use the pull-down list for the first sort field to select the LastName field. If desired, you can click your mouse on the order button to the right of the field to indicate whether Access should sort the field in ascending order (the default) or descending order. Now use the pull-down list for the second sort field to select the FirstName field. When you are done picking the fields you want to sort by, you can click your mouse on the Next button to proceed to the next step. Access displays the dialog box shown in Figure 8-5.

Figure 8-5 Determining a layout for your report.

Specifying Your Report Layout
Using the dialog box shown in Figure 8-5, you can indicate different options for laying out your report. The option buttons in the dialog box control different layouts. The best way to find out how you want your data to appear is to try the different buttons. Access changes the sample report to indicate how your information will appear. Note that the controls in this dialog box have nothing to do with content, only with how the report presents the content you already selected. Besides picking a layout style, the dialog box also lets you choose an orientation for the report (portrait or landscape). Since there are a good number of fields to include in this report, you will probably want to use landscape orientation. You also use this dialog box to indicate whether Access should limit the size of each field in the output. If the Adjust check box (bottom of the dialog box) is selected, Access may truncate some of your data. If you clear the check box, the data is not truncated, but it may not all fit on one line. For the purposes of this report, accept the default of allowing Access to adjust the column width. (Remember that you can always change the report layout later.) When you are done, click your mouse on the Next button to proceed to the next dialog box, shown in Figure 8-6.

Figure 8-6 Picking a report style. Here you have the opportunity to further define how you want your content to appear. Access provides six different predefined styles that determine report elements such as fonts, colors, lines, and the like. You can select each style, in turn, and see in the dialog box a representation of how your report will appear. Pick the one that you like the most, then click your mouse on Next. Access displays the final dialog box, shown in Figure 8-7.

Figure 8-7 Finishing up the report.

Wrapping Up Your Report
You are almost done defining your report. In this final dialog box, Access asks you for a title to use for the report. This title is used in two places: at the top of each page in your printed report, and when saving the actual report in your database. Notice that the Wizard provides the same name as the table being used. In this case it is not a bad title, so you can leave it as is. You are now ready to click your mouse on the Finish button. When you do, Access uses print preview to show you what your report will look like after it prints. This way you can see if you need to make any changes to the report design. Figure 8-8 shows how the report should look.

Figure 8-8 The finished report. All in all, the Report Wizard creates a handsome report. There are a few changes you could make (such as narrower margins, changing the width of some fields), but overall the report is not bad. In Chapter 14 you will learn how to use the report Design window so you can create your own reports from scratch and make modifications to your existing reports (such as this one).

When you are done reviewing the report, close the print preview window by either clicking your mouse on the Close tool on the toolbar or on the Close icon (the X) in the upper-right corner of the window. Access displays the Design window for the report you just completed. (Again, the Design window is covered in Chapter 14.) You can close the design window by clicking your mouse on the Close icon in the upper-right corner of the Design window. You have now completed your first report in Access.

Chapter 8: Creating Simple Reports
(This is section 3 of 5 in this chapter)

Using Other Report Wizards
The Report Wizard you used so far in this chapter is not the only one provided by Access. There are several others you can use, as well. You access these Wizards by choosing Report from the Insert menu. Access then displays the New Report dialog box shown in Figure 8-9.

Figure 8-9 The New Report dialog box. Notice that there is a place at the bottom of the New Report dialog box where you specify the data source (table or query) to be used as the basis of your report. At the top of the dialog box are six choices representing different actions you can take:

• • • •

Design View. Allows you to create a report using the report Design window. This is discussed in more detail in Chapter 14. Report Wizard. Creates a report using the basic Report Wizard. This is the same Wizard you access if you double-click your mouse on the Create Report By Using Wizard option in the Reports area of the Database window. (You did this earlier in this chapter.) AutoReport: Columnar. Creates a report that lists each field in a record on a separate line. (This type of report can consume large amounts of paper.) AutoReport: Tabular. Creates a report that resembles a spreadsheet--one record per line, with fields progressing left to right across the page.

• •

Chart Wizard. Creates a report that includes a chart derived from data in your table or query. Label Wizard. Create several different types of mailing labels.

Most of these report choices actually uses a different Report Wizard. Your report choice answer here will determine the questions that follow. Even though the end result is different, the questions asked in arriving at that output are very similar. All you need to do is apply the skills you have learned already in this chapter, and everything should be fine. The one exception is when you want to create mailing labels. In the next section you learn how to use the Label Wizard to create mailing labels based on the information in your database.

Chapter 8: Creating Simple Reports
(This is section 4 of 5 in this chapter)

Creating Mailing Labels
Note: This section uses the Customer database you created in Chapter 4, "Creating Another Database," and built upon in Chapter 6, "Using Queries to Examine Your Data." If you do not have that database available, you may want to use the Customer 2.mdb database located on the CD-ROM. You should copy the file to a directory on your hard drive and rename it Customer.mdb. In the previous section you learned that you can use the Label Wizard to create mailing labels. Mailing labels, of one form or another, are a common need in almost every business. Assuming you have the Customer.mdb database loaded, you can start the process of easily creating mailing labels by choosing Report from the Insert menu. This displays the New Report dialog box, shown earlier in Figure 8-9. Using the drop-down list at the bottom of the dialog box to select the Business Customers table as your data source. When you have done this, highlight (select) the Label Wizard option and click your mouse on the OK button. Shortly, Access displays the first step in the Label Wizard, as shown in Figure 8-10.

Figure 8-10 Picking a label type. The first step in creating your labels is to indicate the type of labels you want to use. Fortunately, Access makes it very easy to specify a type of label. While there are many different labels available on the market, the industry has become very standardized on several different types of labels. The Label Wizard supports over 650 different labels from 13 different manufacturers. Chances are good that you can find your specific label already predefined by Access. To pick a label type, use the three controls in the middle of the dialog box to filter which labels are displayed. The three controls are as follows:

• •

Unit of Measure. You can select either English or Metric. Your choice dictates which labels are displayed in the product list. Label Type. You can select either Sheet Feed or Continuous. (Sheet-feed labels are used with printers that require you to manually feed each sheet of labels. Continuous labels are used when the printer handles the feeding for you.) Again, your choice dictates which labels are displayed in the product list. Filter by Manufacturer. Use this pull-down list to indicate who made your labels. Only labels produced by that manufacturer will be listed in the product list.

If you are in the US, and you don't see your manufacturer listed, pick Avery. Most label manufacturers have standardized on the same sizes supported by Avery. Some even quote Avery label numbers somewhere on their packaging. (I purchased some Xerox labels the other day, and the box included an Avery number that was equivalent to the Xerox labels.) Even if you can't find an Avery label number, you can always refer to the Dimensions column on this dialog box to figure out which label you want to use. Note: If you cannot find the label you want, or an equivalent label, you can click your mouse on the Customize button to indicate the specifications of the labels you are using.

In this example, you are creating standard mailing labels (1" × 4"), two across. This is Avery label 5261. These labels let you print 6 lines per label and are designed for laser printers. Since this is a standard label size, it is also produced by many other vendors. For the purposes of this example, specify English measurements, Sheet Feed labels, and Avery as the manufacturer. Then select the 5261 labels.

Choosing Fonts and Colors
Click your mouse on the Next button to signify you are done selecting a label type. You are now ready to specify fonts and colors. As such, Access will display the dialog box shown in Figure 8-11.

Figure 8-11 Picking fonts and colors. The font and size the Wizard suggests is an 8-point Arial, using a light font weight. This is a rather small point size, and you should probably increase it a bit, if you can. Whether changing the font size works or not will depend on your printer and its capabilities. For the purposes of this example, leave the point size unchanged, but change any other type specifications you desire. For instance, you may want to change the Font Weight specification to Normal or Bold. Unless you have a color printer, there is not much sense in changing the Text Color setting.

Picking Label Content
After you have specified which font to use, click your mouse on the Next button. Access displays the dialog box shown in Figure 8-12.

Figure 8-12 Indicating label content. The Label Wizard lets you specify how you want your label to appear. At the left side of the dialog box you can choose different fields from your table. You can also enter free-form text which will appear on every label. You will want to do this to insert spaces, commas, periods, and the like. Since you only have six lines to work with, you need to plan how you want your label to appear. The Wizard will use two lines for space at the top and bottom of each label, which leaves you only four lines to work with. You know that the company name and address will take three lines. This means you have an additional line to play with. It might be helpful (to the account) if you used the top line of the label to display the account number. To create a label in this format, follow these steps:

1. 2. 3.
4.

In the Prototype label box, enter the text Your account number is. (Don't forget to add a trailing space at the end of the text. This space will separate the text from the actual account number.) Select the Account Number field in the field list, and then click your mouse on the > button. This moves the account number to the end of the first line in the label. You are now done with the first line. Press the ENTER key. This finishes the first line and moves to the second line of the label. Click your mouse on the Company Name field in the field list, and then click your mouse on the > button. Press the ENTER key. Click your mouse on the Address 1 field in the field list, and then click on the > button. Press the ENTER key.

5.
6.

7.

8. Click your mouse on the City field in the field list, and then click on the > button. 9. Type a comma followed by a space. 10. Click your mouse on the State field in the field list, and then click on the > button.

11. Type two spaces. 12. Click your mouse on the Zip Code field in the field list, and then click on the > button.

You are now done designing your label, and it should appear as shown in Figure 8-13. Take a moment to look over your label to make sure it appears correct. If it does not, you can select the line you want to change and press the DEL key to remove the line. You can then reconstruct the line properly.

Figure 8-13 The finished label layout.

Sorting Your Labels
When you are satisfied with the label layout, click your mouse on the Next button. Access displays the dialog box shown in Figure 8-14.

Figure 8-14 Selecting a sorting order. Typically, you will want to sort mailing labels by ZIP Code, but you can use any field you desire. In this example, we will do a two-level sort: first by ZIP Code, and then by company name. To do this, select each of these fields, in turn, followed by the > button. Both fields should appear on the right side of the dialog box.

Finishing Your Labels
You are now ready to finish your labels. Click your mouse on the Next button. Access displays the dialog box shown in Figure 8-15.

Figure 8-15 Finishing your labels. Because it is a good idea to see your mailing labels as they will look printed, there is nothing you need to change in this dialog box. Click your mouse on the Finish button, and Access displays a print preview of your mailing labels. Figure 8-16 shows what they should look like.

Figure 8-16 Previewing the mailing labels. When you are done reviewing the report, close the print preview window.

Chapter 8: Creating Simple Reports
(This is section 5 of 5 in this chapter)

Summary
Access has some impressive report capabilities. In fact, about the only limit on what you can do with reports is your imagination. In this chapter, you have learned how to create two simple reports with the Report Wizards built into Access. Although these reports are simple in nature, they are useful in everyday life. The process of developing these reports has taught you skills you can use to develop other reports. Before you move on to Chapter 9, make sure you understand the following key concepts:

• • • • • •

In Access, a report is much more than a simple printout. Reports give you complete control over what Access prints and how it appears on the page. You can create reports using either the Report Wizards or the report Design window. You can start a Report Wizard either from the Reports section of the Database window or by choosing Reports from the Insert menu. Report Wizards can be used to create many types of sample reports. These reports are simple in nature, and not nearly as customized as what you can create by using the report Design window. Both simple reports and mailing labels are easy to create with the Report Wizards. The Label Wizard supports many types of standard labels from many manufacturers, automatically formatting your information to fit on the labels.

If you want to know more about creating custom reports, refer to Chapter 14. In Chapter 9, you will venture into the world of form design.

Chapter 9: Creating Custom Forms
(This is section 1 of 4 in this chapter)

Throughout this book, you have entered and edited information in your tables. In this lesson, you will learn how to use Access forms for data input and editing. Forms let you go one giant step beyond regular data entry. In short, forms provide custom interfaces that let you work with data in the most natural way. For example, if you are working with an employee database, you can create a form that matches an employee record. Depending on your company, you may have several different departments that use employee records. Using Access forms, you can create a custom interface for each department. Each department's form, in turn, will present only those fields that employees in that department need to use. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • • •

What Access forms are and their benefits Ways you create forms How you start and use the Forms Wizards How you create forms from scratch How you use the form design toolbox How you can use the Access color palette within your forms How to place objects on your form How properties apply to objects and forms How to save your form within your database

Understanding Forms
In Chapter 2, "Access in an Hour," you used the Datasheet to display the contents of a table. (Datasheets can also be used to present the results of a query, as you learned in Chapter 6, "Using Queries to Examine Your Data.") Using the Datasheet, you can enter and modify information easily. Unfortunately, Datasheets are not particularly user friendly. For example, in

Chapter 4, "Creating Another Database," you developed a table to track your business customers. That table contains information about when your customers opened their accounts, what their credit limits are, and when they placed their last orders. It is not pertinent that everyone have access to this information. That is where forms come in. You can create forms that display part (or all) of your table. In addition, a form can contain information designed to aid input, such as on-screen help, buttons, or custom tools. There are many benefits to using forms. For example, the form shown in Figure 9-1 is one you will create later in this chapter. Notice that the form is much more attractive and useful (from a user's perspective) than the Datasheet.

Figure 9-1 A sample form. To use a form for input, all you need to do is select the Forms button in the Database window. If there are any forms available in your database, Access will list them. Select the form you want to use and then click your mouse on the Open button. You close a form in the same way you close any other window: you click your mouse on the Close icon in the upper-right corner of the form window. Since forms are database objects, you can manipulate them in the same way that you manipulate other database objects such as tables, queries, and reports. This means you can use Access menu options to do things like copy, rename, and delete forms.

Chapter 9: Creating Custom Forms
(This is section 2 of 4 in this chapter)

Creating a Form Using the Form Wizards

Note: This section uses the Customer database you have used in several chapters of this book, most recently in Chapter 8, "Creating Simple Reports." If you do not have that database available, you may want to use the Customer 3.mdb database located on the CD-ROM. You should copy the file to a directory on your hard drive and rename it Customer.mdb. As with may other database objects, Access provides Form Wizards you can use to create your forms. To see the full range of the Form Wizards available, choose the Form option from the Insert menu. Access displays the New Form dialog box, shown in Figure 9-2.

Figure 9-2 The New Form dialog box. This dialog box should look very familiar. You have used the same sort of dialog box to create both queries and reports. Your first task is to use the pull-down list at the bottom of the dialog box to select a table or query whose data you want to use for the form. You should select the Business Customers table. At the top of the dialog box are seven choices which define the different resources you can use to create a new form. The choices are as follows:

• • • • • • •

Design View. Allows you to create a report using the report Design window. This is discussed in more detail later in this chapter. Form Wizard. Creates a form using the basic Form Wizard. This is the same Wizard you access if you double-click your mouse on the Create Form By Using Wizard option in the Forms area of the Database window. AutoForm: Columnar. The form displays all the fields in your table in a single column, with the field's Caption property to the left of the field. AutoForm: Tabular. The form displays all the fields in your table horizontally, with each record occupying a single row. AutoForm: Datasheet. The form looks very similar to the Datasheet view of the table or query. Chart Wizard. The form displays a graph based on your table data. PivotTable Wizard. Access creates a form that includes a PivotTable. These may be familiar to users of Excel; they allow information to be viewed in a condensed form that is helpful for some types of data analysis.

The only basic difference between these forms is the layout of the information within the form. There is no intrinsic difference in what the forms do or how they do it. In the following sections, you will learn how to use the Form Wizards to create several of these types of forms.

Creating a Basic Form
To create a basic form, make sure you select the Form Wizard option from the New Form dialog box, and then click your mouse on the OK button. (You can also double-click your mouse on the Create Form By Using Wizard option in the Forms section of the Database window.) Access displays the Form Wizard dialog box, shown in Figure 9-3.

Figure 9-3 The Form Wizards dialog box. Notice that the dialog box includes the Business Customers table as the data source for your form. (Remember; you should have selected this in the New Form dialog box.) At the bottom left side of the dialog box you can see the different fields available in the data source. All you need to do is select which ones you want included in your form, and use the controls to the right of the list to transfer them to the Selected Fields area. For example, let's say you want to create a data input form that can be used when setting up a new account. There are only a few fields that are necessary for this type of form. You should, in turn, select each of the fields except Account Number (this is assigned automatically), Account Opened (that is today), Credit Limit (you want that set by a different department), Last Order (there are no orders yet), and Active (this should be set automatically). Select the desired fields, and then click your mouse on the > button. The field names appear in the right side of the dialog box. When you are done, the dialog box should appear as shown in Figure 9-4.

Figure 9-4 After selecting a few fields. When you are ready to proceed, click your mouse on the Next button. Access displays the dialog box shown in Figure 9-5.

Figure 9-5 Selecting a layout.

Here you are presented with four different layouts from which you can choose. The Columnar, Tabular, and Datasheet layouts are essentially the same as what you can create with the third, fourth, and fifth options in the New Form dialog box (see Figure 9-2). As you select the option button associated with each type of layout, notice that the preview of the form changes. This gives you a general idea of how your form will appear. For this example, make sure the Columnar option is selected. You can then click your mouse on the Next button. Access displays the next step in the Wizard, as shown in Figure 9-6.

Figure 9-6 Selecting a form style. Here you have the option of selecting from any of ten different form styles. As you select each style, you can see what the effect is in the dialog box. The styles offer quite a bit of variety in data presentation, which reflects the wide range of data that can be represented in Access. Since this is business data we are working with (the Business Customers table), select the very conservative Standard style. You can then click your mouse on the Next button. The Form Wizard displays the final dialog box, as shown in Figure 9-7.

Figure 9-7 Finishing up your form. That's basically all there is to creating your basic form. All you need to do now is provide a form title. This title will appear at the top of the form itself, and it is used to save the form definition in the database. Since the default form suggestion is acceptable, click your mouse on the Finish button. The Wizard creates the form according to your specifications and then displays it on the screen. Figure 9-8 shows what this form looks like.

Figure 9-8 Your completed form.

You will find an input form such as this much friendlier than a Datasheet. At the top of the form, in the form's title bar, is the title you specified. At the bottom of the form is the control area where you can navigate from record to record in the table. You can either review and edit existing records, or you can go to the end of the file and add new records. Note: Form Wizards don't always do the best job creating forms. For instance, notice in Figure 9-8 that the phone number fields are too small, resulting in phone numbers that are cut off. For this reason, many people use the Wizards to create a "starting point" for forms. They can then fine tune them to meet their specific needs. Later in this chapter you learn how to create a form from scratch. The same techniques are used to edit a form you previously created and saved.

Creating an AutoForm Form
If you examine the New Form dialog box (Figure 9-2), you can see that Access allows you to create three types of AutoForms: Columnar, Tabular, and Datasheet. AutoForms (regardless of the version) are the simplest type of form you can create. The Wizard makes all the decisions for you; all you need to do is specify a table or query to use as the source of data used in the form. As an example of how to use an AutoForm Wizard, display the New Form dialog box. Make sure that Business Customers is selected as the data source (at the bottom of the dialog box). Then select the AutoForm: Tabular option and click your mouse on the OK button. The Wizard, in turn, creates a form that includes all your table fields in tabular format. You don't get the opportunity to select any formats, styles, fields, or other items that may affect how the form appears. Figure 9-9 shows an example of the form created by the AutoForm: Tabular Wizard.

Figure 9-9 A completed AutoForm. When you are through working with the form, you can close it. Access asks you if you want to save the form. If you click your mouse on Yes, you are given the opportunity to provide a name for the form. Provide a name, such as AutoForm Example, and click your mouse on the OK button. Access will save the form and update the Database window.

Chapter 9: Creating Custom Forms
(This is section 3 of 4 in this chapter)

Defining Your Own Forms
When the Wizard's forms don't meet your needs, you can use the form Design window to create your own form or edit an existing form. While the form Design window gives you much greater control over how your form works than the Wizards do, using the form Design window is a much more involved process than using the Wizards. Note: This section uses the My Friends.mdb database you have used in several chapters of this book, most recently in Chapter 8, "Creating Simple Reports." If you do not have that database available, you may want to use the Friends 2.mdb database located on the

CD-ROM. You should copy the file to a directory on your hard drive and rename it My Friends.mdb. To create your own form, click your mouse on the Forms button in the Database window. Then double-click your mouse on the Create Form in Design View option. Access displays the form Design window shown in Figure 9-10.

Figure 9-10 The form Design window. There are several items you should notice in this window. First, notice the toolbar has some new tools on it. You will learn the purpose of each tool later in this chapter. Second, notice the form Design toolbox. You will use the toolbox to put together your form. As such, the toolbox is described very shortly. The main part of the Design window contains a grid. This grid becomes your form. The idea is to design your form by placing objects on the grid. Objects can be fields or things such as labels, buttons, controls, or other items. You will learn how to place objects on the form grid later in this chapter. These objects have properties which specify how they appear and behave.

Editing Existing Forms You can edit existing forms using the same techniques you use to create new ones. Select the form name from the Database window, and then click your mouse on the Design button. Many people use the Forms Wizards to create a base form, and then use the Design window to change the form to meet their needs.
Notice that the form grid is not very large; it is only two inches high by five inches wide. If you want to increase the size of the grid (and thereby increase the size of the form you are creating), move the mouse pointer over one of the outside grid borders. Next, hold down the mouse button and drag the border to a new position. When you release the mouse button, the grid remains enlarged. Note: You can enlarge or reduce the size of the form grid at any time. You will need to enlarge the grid to add new objects, and, before you reduce the size of the grid, you must remove any objects that appear in the part of the grid you are eliminating.

Attaching a Data Source
If you are not editing an existing form, chances are good that the form you are creating does not have a data source identified for it. The first task in creating a new form from scratch is to identify a data source. Follow these steps:
1. 2. Click your mouse on the Properties tool on the toolbar. As an alternative, you can choose Properties from the View menu. Access displays the Properties dialog box for your form. Make sure the Data tab is selected. Access displays the Properties dialog box as shown in Figure 9-11.

Figure 9-11 The Properties dialog box for the new form.
3. 4. Examine the Record Source property. If there is something in the property, then you already have a data source defined for your form. In that case, you can skip the next step. Select the Record Source property and use the pull-down arrow at the right of the property to select My Friends as the record source. (Remember; My Friends is the name of the table you created in the My Friends database.) Close the Properties dialog box by clicking your mouse on the Close button in the upper-right corner of the dialog box.

5.

Notice that the Record Source is a property for your form. Later in this chapter you learn more about how to change properties for your form.

The Form Design Toolbox
Somewhere on your screen is the form Design toolbox. (In Figure 9-10, it is located at the right side of the screen.) This toolbox contains objects which you can place in your form. These objects are often called controls. You can alternately hide or display the toolbox by clicking your mouse on the Toolbox tool on the toolbar or by choosing Toolbox from the View menu. The toolbox contains the 20 tools detailed in Table 9-1.

Tool Select Objects Control Wizards Label Text Box Option Group Toggle Button Option Button Check Box Combo Box List Box Command Button Image Unbound Object Frame Bound Object Frame Page Break Tab Control Subform/Subrepor t Line Rectangle More Controls

Description The arrow, used to select existing objects on the form. When selected, Access uses Wizards to help you create controls on your form. Places text on the form. Obtains some sort of user input or displays an existing value, such as the contents of a field. Groups toggle buttons, option buttons, and check boxes together. Any of these objects within an option group is mutually exclusive (only one can be selected in the group). An on or off button. When it is selected, it is considered a True value. Sometimes called a radio button. Appears as a circle; when selected, it is filled in. Appears as a square; when selected, it has an × in it. A list box with a text box at the top of it. A box containing a series of values from which the user can choose. Runs a macro or Access Basic module. Inserts an image in the form. Links and displays a picture, graphic, or other OLE object. The object is not stored within a table or query. Same as an object frame, except the object is stored in a table or query. Splits a form into two screens. Used to add tabs to a form, just like many dialog boxes contain tabs. Adds another form or report to the existing form or report. Typically displays information in related tables. Draws a straight line. Adds a rectangle or square. Allows you to add additional controls the the Toolbox.

Table 9-1 Tools in the toolbox (from top to bottom and left to right). Note: You can use your mouse to move the toolbox or change its size. Point to the title bar of the toolbox and use the mouse to drag it to a new position. Point to the toolbox border and use the mouse to drag it to a new size.

To place a control on your form, click your mouse on the tool that represents the control you desire and use the mouse to indicate where you want the control on the form. For instance, if you want to place a text box, first click your mouse on the Text Box tool. Then, use the mouse pointer to indicate where you would like a corner of the box. When you click and hold the mouse button, Access anchors the corner of the box. As you drag the mouse, the text box gets larger. When the box is the size you desire, release the mouse button. The text box will remain on the form. You can place any other control in the same manner. Once you place an object on a form, you can move it or resize it by using your mouse. Every object, when selected, is surrounded by handles. These are small squares that reside around the perimeter of the control. You can use your mouse to drag these handles to new positions, which resizes the object. If you simply point to the edge of the object and hold down the mouse button, you can drag the entire object to a new position on the form. When you release the mouse button, the object stays at the new position. Note: You can tell when you can move an object because the mouse pointer changes to a hand. On some objects, the hand appears when you pass over the edge of the object (not where a handle is located). On other objects, the hand appears when the mouse pointer is in the center of the object. Just move the mouse around until the hand appears, and then you can use the mouse to drag the object to a new position.

Placing Objects on Your Form
Assume you want to create a form so you can review the records in the My Friends table one at a time. Further, you want to give the form some "personality." You start this task by using the form Design window in much the same manner as you would use a paint or drawing program. You place an object, refine how it looks, and then place another object. You repeat this process until you achieve the desired effect. Your first task in creating your form is to adjust the size of the form grid to the size you think you will need. You do this using the mouse, as described earlier. You can then change the background color of the form as a whole. To assign the form's background color, select the form (just click your mouse on the form grid) and then adjust the color using the Fill/Back Color tool on the toolbar. (This tool looks like a small paint bucket.) Click your mouse on the downarrow to the right side of the tool, and Access displays a color palette. You can set any color you desire, but it seems that light gray is typically in fashion these days. Click your mouse on the color desired. If you pick a color other than the current color, notice that Access shades the entire grid with that color.

Placing a Label Object
Next, you will probably want to have some sort of a title on your form. To assign a title, place a Label object. In the toolbox, click your mouse on the Label tool (the one with the capital A in it). Place the label box near the form's upper left corner and size it all the way across the form. At this point, the Design window will look like what is shown in Figure 9-12.

Figure 9-12 After placing the Label box. Access is now waiting for you to type the text that should appear in the label. Type the words My Friends, and then press ENTER. The text should appear at the left side of the box and handles will appear around the box. You can now set colors for the object. To make the box itself seem to disappear, so all you see is the text, set the background color (using the Fill/Back Color tool) and border color (using the Line/Border Color tool) either to the same color you used for the background of the form or to Transparent. You can then set the text color using the Font/Fore Color tool to something such as red. Notice the toolbar at the top of the screen. When you have selected an object that contains text, the text controls are active. You can set attributes such as text alignment, font, point size, and so on. You should probably change the text attributes for this label so the text looks more like a heading for your form. Figure 9-13 shows how the Design window appears after changing to 14-point bold Tahoma, centered in the box.

Figure 9-13 The finished heading for the form.

Placing Fields on the Form
Now you are ready to place your fields on the grid. To begin, select the Field List option from the View menu. This displays the field list for your data source (the My Friends table). Next, use the mouse to select a field and drag it onto the form. For instance, click your mouse on the FirstName field and then drag it onto the form grid. When you release the mouse button, Access places two objects on the form: a Label and a Text Box. The Label contains the Caption property for the field, and the Text Box contains the name of the field itself, as shown in Figure 9-14.

Figure 9-14 Placing a field on the form. You can use the mouse to individually position the Label and Text Box objects on the screen. (If you move the Label some and move the Text Box so it is separated only slightly from the Label, you will find they look better.) Use the color and formatting controls on the toolbar to adjust the objects so they blend nicely on the form. When you are done, the Label and Text Box objects should appear similar to what you see in Figure 9-15.

Figure 9-15 Formatting controls. It would be nice to change the caption in the Label object so it only says Name instead of First Name. In this way, users can input an entire name to the right of the Label object. To do this, select the Label object. As you move the mouse pointer over the Label text, notice that the pointer changes to an insertion point. Click the mouse button, and you can change the text in the Label object quite easily. When you press ENTER, Access makes your change. Note: Whenever you change the text in a label, you may also want to change the size of the label object or the text alignment. Make any adjustments you feel necessary to make the objects look their best. Now you are ready to place the field for the middle initial. You do this in the same manner that you placed the field for the first name. Simply click your mouse on the field name (Middle Initial) in the field list and drag the name onto the form grid. Position the mouse pointer so it is just a little to the right of the Text Box used for the FirstName field. When you release the mouse button, Access places the two objects on the form, as shown in Figure 9-16.

Figure 9-16 Placing a second field. Notice that the Label object overlays the previous field you placed. Since you don't really need the middle initial label anyway (since the previous label object, Name, is adequate for the entire row), use the mouse to select it and then press the DEL key. The Label box disappears, but the Text box remains. You can now position the Text box and change its attributes and size so it looks good with the other objects. You can now go through the same process to place the LastName field. The resulting Design window is shown in Figure 9-17.

Figure 9-17 The first line of the form is complete. That's basically all there is to placing fields on your form. You should go ahead and place the rest of your fields, positioning them and changing their attributes so they look good. Use the same techniques you learned in placing the first three fields. (Don't place the Christmas Card field yet; it will be covered in the next section.) Figure 9-18 shows what the completed Design window should look like (minus the Christmas Card field, of course).

Figure 9-18 A form that is almost completed.

Placing a Check Box
The final field you need to place is the Christmas Card field. You may remember from Chapter 2, "Access in an Hour," that this is a Yes/No field that indicates whether you should send a Christmas card to an individual. Represent Yes/No fields with any of the following objects:

• • •

Toggle Button Option Button Check Box

Any of these controls can indicate a Yes/No or On/Off condition--the only difference between them is their appearance. Their effect on a user may be quite different, however. For instance, a toggle button indicates if something is on or off, whereas the other two more accurately represent yes or no. The actual object you use is up to you, but for this example, place a Check Box object. To place a check box for the Christmas Card field, click your mouse on the Check Box tool in the toolbox. Then, drag the Christmas Card field from the Field List to your form. When you release the mouse button, Access places two objects on the form: the Check Box object and a Label object. This is shown in Figure 9-19.

Figure 9-19 Placing a check box. Notice that, unlike when you placed the other fields, Access put the Label object on the right side of the Check Box object (instead of on the left). Access uses the right side to keep with the regular interface guidelines for Windows. Go ahead and position the form's objects and format them so they look proper. You will also want to change the text in the Label object so it reflects the purpose of the check box; you could change it to something like Send Christmas card?

Understanding Properties
By this point, you should already be familiar with the concept of properties. You already know that Access uses properties to describe how an object appears or how Access is to treat it. You know that fields possess properties, both in tables and queries. Objects you place on a form can also have properties. The exact properties available depend on the object you have selected. In general, properties describe attributes of an object. For instance, the height, width, and position of an object are all attributes. So are things like colors and font. As you worked through the previous section, you set properties for various objects, although you may not have been aware of it.

Changing Object Properties
You can view and change an object's attributes by either of these two methods:

• •

Double-click your mouse on the object. Select the object and click your mouse on the Properties tool on the toolbar.

When you do either of these things, Access displays the Properties dialog box for that object. For example, Figure 9-20 shows the Properties dialog box for the first Label object you placed in the form (the one used for the heading of the form).

Figure 9-20 The Properties dialog box. In all, there are 38 properties for this one object. For some objects, there may be more properties; for some, there may be less. The number is not particularly important, but it is important to know how to change a property. To change a property, first display the Properties dialog box. Next, use the mouse to select the property category from the tabs in the dialog box. You can then use the mouse to select the property you want to change. You can either type in a new value or (if it is available) use a pull-down arrow or builder to select an appropriate value. When you change a property, the effect is immediate. Note: When changing some properties, it is easier not to use the Properties dialog box. For instance, it is much easier to use the toolbar to change colors. If you can, use a different method of changing properties whenever one is available. Since there are so many different properties available, you may begin to wonder what a particular property is used for. The easiest way to find out is to select the property and then look at the Access status bar. There you will find information that provides quick help on what the property does. If you need additional help, you can use the regular Access online Help system. The Help system thoroughly describes the purpose and use of various properties. As an example of how to change properties, select the Check Box object you placed on the form at the end of the previous section. Notice that the properties in the Properties dialog box

change to reflect the new object you have selected. When you select the Other tab in the dialog box, it should appear as shown in Figure 9-21.

Figure 9-21 The Properties dialog box. A helpful property you can change is the Status Bar Text property. This is the property that specifies what Access should display on the status bar when the Christmas Card control is selected. All you need to do is choose the Status Bar Text property and start typing the text you want displayed. For this example, type the text Should this person receive a Christmas card? When you are done with the Properties dialog box, go ahead and close it. Note: Many of the properties for form objects are the same as properties for other Access items, such as fields. You may want to refer to Chapter 3, "Taking a Closer Look," for more information about some properties.

Changing Form Properties
Your form (the entire thing) also has properties. You can view these properties by selecting the form. To select a form, click your mouse on the small gray block just to the left of the horizontal ruler at the top of the form Detail window. If the Properties dialog box is open, you will see the properties for the entire form. There are 81 properties applicable to forms, and Access sets many of them automatically when you edit your form. You changed one of the form properties earlier in the chapter when you changed the Record Source property for the form. Many properties are useful only for advanced programming. However, you can use some of them immediately. For example, you may want to change the wording that appears in the title bar for your form. By default, the title bar will display the word "Form" followed by a colon and the name of the form. However, since you are working with the table that contains all your friends, you might want the title bar to display your name. To make this change, simply modify the Caption property for the form. Whatever you put in the Caption property will appear in the form's title bar. When I changed the Caption property to my name, Access changed the title bar of the form, as shown in Figure 9-22.

Figure 9-22 Properties control many appearance items. Note: Access does not change the title bar until you actually open the form. If you are using the Design window, the form uses the default title bar.

Saving Your Form
When you are done creating or changing a form, you need to save it. Access automatically gives you the chance to do this when you close the form Design window. If you have made changes to the form (or if you are designing the form for the first time), Access will ask if you want to save your changes. The effect of clicking your mouse on the Yes button varies, depending on whether you are editing or creating a form. If you are editing, Access saves your changes under the same form name as you used previously. If you are creating a form, Access will ask you to provide a form name, as shown in Figure 9-23.

Figure 9-23 You can specify a name for your form. In this instance, Access suggests a name based on the caption I supplied for the form. (You remember; this was set when changing the form properties.) This name can be changed in any way desired. You should use a name that represents the purpose of the form. In this case I

changed the form name to Reviewing Friends. When you press ENTER, Access saves the form and returns you to the Database window. Note: Access saves forms with your database in the same way that it saves other database objects. Forms are an integral part of the database; they are not saved in a separate file. You can see what forms are available with a database by clicking your mouse on the Form button in the Database window.

Chapter 9: Creating Custom Forms
(This is section 4 of 4 in this chapter)

Summary
Forms provide a convenient and attractive way to both present information and solicit input from the user. If you are developing an application using Access, it is not unusual to have several different forms, each with a different purpose. Through effectively using forms, you increase the usability of your database tremendously. This chapter has taught you how to develop your own forms. Before you move on to Chapter 10, make sure you understand the following key concepts:

• • • • •

Forms are a tool you can use to change or add information in a table or query. From a user's perspective, forms are much more usable than the Datasheet. You can create forms using the Form Wizards, or you can create them from scratch. You can create only a limited number of forms with the Form Wizards. You greatly increase your options if you create your forms from scratch. You use the form design toolbox to select objects that can be placed in your form. As with most other objects in Access, you can set properties for objects in your forms. These properties define the attributes which control the object.

In Chapter 10, you will learn how to use some of the additional tools provided with Access to make your data more valid.

Chapter 10: Using Additional Tools
(This is section 1 of 5 in this chapter)

In many ways, Access is more than just a plain database manager. One of the biggest evidences of this is the additional tools that Microsoft chose to include with Access. These tools are not necessarily related to making your data easier to use, but to making it easier to create. Thus, the tools can be viewed as helping you improve the validity of your data by making it more correct than it would be without the use of the tools. By the time you finish this chapter, you will understand the following key concepts:

How to use Access' built-in spell checker

• • •

How the AutoCorrect feature can make inputting data faster Why analyzing a table may result in more efficient databases How to use the Performance Analyzer tool

Using the Spell Checker
Although Access can be viewed as an individual program, in reality it is part of the Office 2000 suite of products. As such, it shares several tools in common with other Office products. One of these tools is the spell checker, which allows you to check the spelling of words in your database. The spell-checking program used in Microsoft Office (and therefore in Access) does not just allow you to check the spelling in a table. You can, in addition, check the spelling of any database object that is ultimately based on a table. Thus, you can check the spelling of a table, query, view, or form. The next couple of sections describe how to utilize this tool.

Spell Checking Your Data
Before you can check the spelling of your data, you must first select the database object that you want to spell check. For instance, if you want to check the spelling of data in a particular table, you must first select the table. You can select the object at the Database window, or you can open the object before starting the spell checker. If you want to only spell check a particular portion of your data, you can also select the rows or columns before initiating the spell checker. This, obviously, is best done when viewing the Datasheet for a table or query. Once you have selected what you want checked, there are three ways you can start to use the spell checker. Any of the following will do the trick:

• • •

Choose Spelling from the Tools menu. Click your mouse on the Spelling tool on the toolbar. Press F7.

Regardless of the method you choose, Access begins to compare the text of the data in your database object to its built-in list of words. If Access finds an error, it highlights the word in the Datasheet or form, displays the Spelling dialog box, and waits for your response. Figure 10-1 shows an example of what you would see if you started to spell check the My Friends table developed in earlier chapters of this book.

Figure 10-1 Access includes a full-featured spell checker. The Spelling dialog box shows you the word Access suspects as being incorrect, along with any suggestions for changing. The buttons in the dialog box provide several different ways for you to proceed:

• • • • •

Ignore Field. Use this button to cause Access to continue the spell check, but ignore any data in the noted field. Ignore. Use this button to ignore the suggested change. Access moves on to the next incorrectly spelled word. Ignore All. Use this button to ignore the suggested change. Access moves on to the next incorrectly spelled word, but ignores any other occurrences of the highlighted word. Change. Use this button if you want to change the highlighted word to the selected suggestion (the one in the Change To box). Access makes the change and moves on to the next incorrectly spelled word. Change All. Use this button if you want to change all occurrences of the highlighted word to the selected suggestion (the one in the Change To box). Access makes the change (and any additional identical changes) and moves on to the next incorrectly spelled word.

• •

Add. Use this button to add the highlighted word to the dictionary. This option is discussed in greater detail in the next section. Suggest. Use this button to cause Access to suggest additional words that could be used to correct the misspelled word.

Most often you will select one of the proffered suggestions and then click your mouse on Change, or you will simply click your mouse on the Ignore button. The Ignore All button is another commonly used button; it is great for commonly recurring names, which are easily flagged as being misspelled. After clicking your mouse on a button, Access continues on its quest for misspelled words. If any other problems are found, you are given the same opportunity to make changes.

Adding Custom Words
Chances are good that your individual use of Access will be somewhat different than the way in which other people use the program. This is nowhere more evident than in the use of the spell checker. You may use words in your data that are unique to your work or circle of friends. Fortunately, Access provides a way you can easily build your own dictionary of custom words. Take a look back at the Spelling dialog box in Figure 10-1. Notice that there is an Add button in the dialog box. If you click your mouse on this button when Access is suggesting a spelling correction, the word that Access thought was incorrect is added to your custom dictionary. You will not be asked again if the word is incorrect. It is a good idea to add words to the custom dictionary as necessary. In this way you can force Access to match your spelling needs without requiring you to always ignore errors that Access thinks it has found. Note: Any words you add to your custom dictionary are automatically remembered and used by other Office 2000 applications. Thus, any words you add to the custom dictionary in Access will also be available in Word, and vice versa.

Chapter 10: Using Additional Tools
(This is section 2 of 5 in this chapter)

Using AutoCorrect
If you are a user of Word, Microsoft's popular word processor, you may already be familiar with the AutoCorrect feature. This handy tool has been adapted to Access to help you become more productive in your data entry. The purpose of AutoCorrect is to automatically change things you type, as you type them. This may sound strange, but it can really be a benefit. For instance, if you know you always misspell a certain word, you can force Access to recognize that word and replace it with the proper one. Similarly, you can define short codes that can be automatically replaced with long words or phrases. It is this last use of AutoCorrect that is perhaps the most powerful when entering data. For instance, let's say you work for Amalgamated Widgets Worldwide, and you have to enter that

name frequently in your data. You could define an AutoCorrect entry that every time you typed aww, it was automatically replaced with Amalgamated Widgets Worldwide.

Adding Entries
To add information to AutoCorrect, choose the AutoCorrect option from the Tools menu. Access displays the AutoCorrect dialog box, as shown in Figure 10-2.

Figure 10-2 The AutoCorrect dialog box controls all the features of this popular tool. At the top of the dialog box you can control some of the automatic changes which AutoCorrect can make for you; these are pretty much self-explanatory. Near the bottom of the dialog box is a list of AutoCorrect entries. Each entry is made up of two parts; the part on the left is what you would type as you are entering data in a Datasheet or form, and the part on the right is what Access automatically uses instead of what you typed. For instance, in Figure 10-2 you can see an entry which has two words: abbout and about. If you were to type "abbout" in your text, Access would automatically recognize it and replace it with "about." If you scroll through the list you can see an extensive number of changes which Access is already configured to make. To add your own custom AutoText entries, you do it using the Replace and With boxes. All you need to do is type in the Replace box what you want Access to recognize and in the With box what you want it replaced with. Continuing the example started in the last section, you would place "aww" in the Replace box and "Amalgamated Widgets Worldwide" in the With box. When you click your mouse on the Add button, Access adds the entry to the AutoCorrect list it uses.

Removing Entries
Just as you can add entries to the AutoCorrect list, you can also delete them. To do this, simply select the entry in the AutoCorrect dialog box (Figure 10-2), and then click your mouse on the Delete button. Access removes the entry from the list and no more automatic changes are made using it. You should note that Access does not ask you to verify your deletion; it just does it. The deleted information remains in the Replace and With fields right after the deletion, however. This means that if you discover you deleted something you didn't want to delete, you can immediately click your mouse on the Add button to put the entry right back in the AutoCorrect list.

Chapter 10: Using Additional Tools
(This is section 3 of 5 in this chapter)

Analyzing a Table
Access includes a very powerful tool that you can use to analyze the data in one of your tables. The purpose of the analysis is to determine if there is a less redundant way to store the data in the table. "Less redundant" means that Access tries to remove any data that is stored in the table more than once. For instance, it may discover that a "company name" field should be pulled out of the original table and placed in a new table. Even though the result would be two tables (the original and a new table), the database would be more efficient since Information would not be duplicated in the original table. An example is perhaps the best way to demonstrate the analysis process. The example in this section is based on the database Accounts.mdb, which is supplied on the companion CD. The database contains a single table, named Invoices, that has a bit over 1,000 invoices in it. A minimal number of fields are maintained in the table, but there is quite a bit of redundancy in the data.

Starting the Analysis
With the Accounts database loaded, click your mouse on the Tables button in the database window. Click your mouse on the Invoices table to select it. Then choose the Analyze option from the Tools menu. Access displays a submenu from which you should choose Table. Shortly Access displays the Table Analyzer Wizard, as shown in Figure 10-3.

Figure 10-3 The Table Analyzer Wizard dialog box. The first couple of screens in the Wizard are explanation of the purpose of the Table Analyzer Wizard. You can read through the information, if desired, clicking your mouse on the Next button whenever you are ready to proceed. Eventually (on the third dialog box) you come to the actual start of the action. This dialog box is shown in Figure 10-4.

Figure 10-4 Selecting a table to analyze. In many databases there may be more than a single table. Fortunately, this one only contains a single table named Invoices. Since this is the default table selected, click your mouse on the Next button. Access displays the dialog box shown in Figure 10-5.

Figure 10-5 Letting Access do the work. In this step you need to decide whether you want to let the Wizard decide which table fields should be moved, or you want to do it manually. Since the job of a Wizard is to make your life easier, go ahead and let the Wizard do the deciding. Even if the Wizard makes a bad choice or two, you can countermand the decision at the next step in the Wizard.

Confirming Table Splitting
When you are ready to proceed, click your mouse on the Next button. Access displays the dialog box shown in Figure 10-6.

Figure 10-6 Verifying the table splitting. Note: The Table Analyzer Wizard sets up relationships between tables. You will learn more about how relationships work in Chapter 13, "Understanding Data Relationships." At this point, the Wizard is showing you what it is proposing to do. In the case of the original Invoices table, the Wizard is proposing splitting it into three tables. Looking at the fields it suggests moving to each table, it is easy to see that one will contain invoice transactions, another customers, and the third products. Using this dialog box, you have the opportunity to name each of the tables. To see how this works, make sure Table1 is selected (it is in Figure 10-6). Then click your mouse on the Rename button, as indicated near the upper-right corner of the dialog box. Access displays a small dialog box that allows you to specify a new name for the table. In the case of Table1, the name should be Invoice Transactions. Enter this, then click your mouse on the OK button. Access updates the name shown in the title bar for the table to your new name. You should repeat this same general process to name both Table2 and Table3 to Customers and Products, respectively. Click your mouse on the title bar for each table, and then rename it. When you are done, the tables should appear as shown in Figure 10-7.

Figure 10-7 The tables have been renamed. The other thing you can do in this dialog box (besides naming the tables) is to override how the Wizard has split the tables. When doing an analysis like this on your own tables, you will want to make sure that each proposed table contains the relevant fields from the original table. In the case of this example, the Wizard did a very good job of making the decisions. For instance, it figured out that what we have named the Customers table should contain a unique customer ID, as well as the customer name and discount. If you needed to override the Wizard's splitting decisions, all you need to do is click your mouse on a field name and drag it from one table to another. No such dragging is necessary in this example, however.

Confirming Primary Key Selections
When you click the mouse on the Next button, Access displays the next step of the Wizard, as shown in Figure 10-8.

Figure 10-8 Setting primary keys. This dialog box looks remarkably similar to the one used in the previous section. The difference, however, is in the control buttons at the top of the dialog box. The Wizard is asking you to confirm that each table has a primary key defined. The primary keys for the Customers and Products table are fine; they were created by the Wizard. However, the Wizard has somehow forgotten that the ID field should be a primary key in the Invoice Transactions table. To specify the ID field as a primary key, first click your mouse on the field name. Then click the mouse on the tool that has the key on it. Access responds by placing a small key to the left of the ID field in the Invoice Transactions table.

Correcting Typographical Errors
When you click your mouse on the Next key, the Wizard analyzes the data in the proposed tables and displays what it may believe are typographical errors. Access does this for each new table being created by the Wizard. Since two new tables (Customers and Products) are being created by the Wizard in this example, this means Access displays two typographical correction dialog boxes. The first of these is shown in Figure 10-9.

Figure 10-9 Correcting typos in the Customers table. Here the Wizard has displayed five customer names it believes may be mistyped. It figures this out using an internal algorithm. If the spelling of the data is close enough to other data in the table, then it is flagged as a potential error. All you need to do is use the pull-down list in the Correction column to select either a correct spelling or the (Leave As Is) setting. Note: If you are analyzing a large table that has been worked on by many people over a long time period, it is very possible you will have many misspellings in the file. Make sure you take the time to go through the necessary corrections in this step; it is easier than tracking down mistakes later. In the case of this example Customers table, there are no misspellings. You should change the Corrections column for all five records so the (Leave As Is) setting is selected. When you have done this, click your mouse on the Next button. The Wizard then displays the potential corrections for the Products table, as shown in Figure 10-10.

Figure 10-10 Correcting typos in the Customers table. It just so happens that there is only one record identified as a possible error in this step. In your own tables, of course, there may be more. You go through the correction process exactly as you did for the Customers table. In this case, you select the (Leave As Is) option for the single record shown.

Finishing the Analysis
When you click your mouse on the Next button, the Wizard displays its final step, as shown in Figure 10-11.

Figure 10-11 The final analysis step. The final step of the Table Analysis Wizard is where you specify if you want to create a query that looks like your original table. In this case, it means that the Wizard would create a query that is an amalgamation of the three tables, combined to look like the original Invoices table. Whether you create a query is up to you. In this instance, it doesn't hurt, so you can click your mouse on Finish to complete the analysis process. Shortly Access displays the new query on screen, as shown in Figure 10-12.

Figure 10-12 The new query created by the Table Analysis Wizard. Notice that there are some odd-looking fields in the new query. In this case, the Lookup to Customers field and the Lookup to Products field seems a little odd. In reality, these are nothing but the links that the Wizard established between the Invoice Transactions table and the other tables. You can easily turn these off by editing the query (as described in Chapter 6, "Using Queries to Examine Your Data") to not display the field.

Chapter 10: Using Additional Tools
(This is section 4 of 5 in this chapter)

Improving Database Performance
As you get to know more about databases in general, and Access in particular, you may come across ideas that can be used to improve the performance of objects in your database. For

instance, you may discover new ways of examining or processing your data to get the desired results. In recognition of the fact that most databases can be "tweaked" to be better in some ways, Access includes a performance analyzer that you can use to provide helpful hints. To see how the performance analyzer works, load the Customer database used earlier in this book. Then choose the Analyze option from the Tools menu. From the resulting submenu, choose the Performance option. Access displays the Performance Analyzer dialog box, as shown in Figure 10-13.

Figure 10-13 The Performance Analyzer dialog box. The Performance Analyzer allows you to examine virtually any object in your database. You can use the tabs in the dialog box to display the different objects available. To see how the Performance Analyzer works, select the Business Customers object by clicking your mouse on the check box to the left of the object name. You should then click your mouse on the OK button. Shortly, the Performance Analyzer displays the results of its analysis, as shown in Figure 10-14. The analysis can contain three different types of results: Recommendations, Suggestions, and Ideas. This particular results screen shows only Ideas.

Figure 10-14 The Performance Analyzer results screen. You can see the reasoning behind a specific Recommendation, Suggestion, or Idea by selecting that particular item on the results screen. The information at the bottom of the screen indicates the considerations in making the change, as well as how you go about the change. If the result is either a Recommendation or a Suggestion, you can select the result and click your mouse on the Optimize button to make the change. For Ideas, the Optimize button is not available. Instead, you must simply follow the instructions at the bottom of the screen. When you are through making your changes based on the Performance Analyzer results, click your mouse on the Close button to return to working with Access.

Chapter 10: Using Additional Tools
(This is section 5 of 5 in this chapter)

Summary
Access includes many tools which you can use to augment your database management tasks. These tools run the gamut, making it easier to both input information, maintain it, and work with tables as a whole. In this chapter you have learned how to use a number of those tools. While understanding how to use them is not critical to managing databases, doing so can make Access more valuable to you in the long run. Before you move on to Chapter 11, make sure you understand the following key concepts:

• • • • • • •

Access includes a spell checker that you can use to review and correct the spelling of words in your data. The spell-checking tool is shared with other Microsoft Office applications. You can add words to the dictionaries maintained by the spell checker. Those words are then available not only to Access, but also to other Office programs. The AutoCorrect tool allows you to specify words which should be replaced with other text as the words are typed. AutoCorrect can help you improve the quality of what you type, as well as make the entry of long words or phrases easier and faster. The table analysis tool looks at the data in a table and suggests different ways for that data to be organized. It can perform the reorganization, based on your approval. The Performance Analyzer tool examines your tables and other database objects to provide Recommendations, Suggestions, and Ideas on how those objects can be improved.

In Chapter 11, you will learn how you can use the information in your tables and queries with Word for Windows.

Chapter 11: Using Your Data with Word and Excel
(This is section 1 of 4 in this chapter)

Access is designed to work well with other Microsoft Office programs. You can use Access with these programs to perform very powerful operations with your data. For example, you can use information in your Access tables with documents you have created in Word. Specifically, you can use Access and Word to create a mail merge that places the values stored in an Access table into appropriate locations within a Word form letter. In this way, you can quickly create a custom letter for each of your accounts. In this chapter, you will focus on how you can make these two programs--Access and Word-function together well. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • •

Why you would want to use Access with Word and Excel How to use the Microsoft Word Mail Merge Wizard How to insert merge fields into a Word document How to merge Access data with the Word document How to output the contents of an entire table or query to Excel How to output a portion of a table or query to Excel How to establish an active link between Excel and Access What relationship there is between information that has been transferred and your original Access information

Why Use Other Programs with Access?

Access is great at managing your data. You can use it to maintain just about any information you may want. It is not, however, a word processor or a spreadsheet program. Recognizing that fact, Microsoft made sure that you could use your Access data with both Word and Excel, both of which are members of the Microsoft Office family of products (Access is also part of that family). Why would you want to use data from Access with Word or Excel? Primarily because "that's where the information is." In other words, if you are already using Access to maintain raw information, there should not be a need to duplicate that information in Word or Excel. Instead, you should be able to transfer the information you need when you need it. Now that you understand why it is preferable to use your Access information in other programs, at times, you are ready to turn your attention to how this is done. The rest of this chapter focuses on how you get your information from Access into both Word and Excel. Note: It is not the purpose of this book to give detailed information on how to use any program besides Access. It is assumed that you have at least some familiarity with the other programs discussed in this chapter. If you need help, you might refer to a book that is geared specifically to teaching you how to use Word or Excel.

Chapter 11: Using Your Data with Word and Excel
(This is section 2 of 4 in this chapter)

Sharing Data with Word
The primary reason to have Access and Word work together is to create mail-merge documents. These are documents into which you merge information when the document is printed. For example, you can merge names, address, account balances, or any other information you can think of. Possible uses for mail-merge documents include the following:

• • • •

Form letters Mailing labels Catalogs Order forms

Actually, the list of the merged documents you can create is limited only by your imagination! Regardless of your eventual use of the mail-merge document, the steps you go through are all the same.

The Scenario
The scenario is this: You have recently moved, and you want to send letters to all your friends to make sure they have your new address. You have the names and addresses of your friends in the My Friends table you created earlier in this book. Using Access and Word together, you can quickly and easily create the letter and get it ready to mail.

In preparation, you prepare an upbeat note in Word (see Figure 11-1). Your letter is formatted just the way you want it, and you have left room to later insert your friends' names and addresses. You save this document on your hard drive as Moved.doc.

Figure 11-1 The letter you want to send to all your friends.

Note: The file Moved.doc is included on the companion CD-ROM. You should copy it to one of your Word document directories before proceeding.

Using the Word Mail Merge Wizard
The first step is to use the Word Mail Merge Wizard, which is provided with Access. This Wizard helps you transfer information into a format you can use with Word. To start the Wizard, follow these steps:
1. 2. Open the Friends.mdb database you created earlier in this book. If you want to use information from a table, click your mouse on the Tables button in the Database window. If you want to use information from a query, click your mouse on the Queries button in the Database window. Select the name of the table or query you want to use. Choose the Office Links option from the Tools menu, and then choose Merge It with MS Word from the resulting submenu.

3. 4.

Access, in turn, starts the Microsoft Word Mail Merge Wizard, displaying the dialog box shown in Figure 11-2.

Figure 11-2 The Microsoft Word Mail Merge Wizard. This is the only step you need to perform in Access. All you need to do is choose whether you are using an existing document or a new one. Since you have already created the file in Word, select the first option and then click your mouse on the OK button.

Loading the Word Document
Access will now display the dialog box shown in Figure 11-3, asking you for the name of the Word document you are going to merge with the Access data. This is a standard Open dialog box, with which you should be well acquainted. Use the controls in the dialog box to select the file Moved.doc, and then click your mouse on the Open button.

Figure 11-3 The Select Microsoft Word Document dialog box. The Access Mail Merge Wizard causes Word to run and load the document Moved.doc. Once Word is running, you should notice the Word task on your taskbar, even if you cannot see the Word program window on your screen. To display your document, switch to Word by clicking your mouse on the Word task on the taskbar. You may then need to maximize the Word program window so you can fully see the document on which you are working. By this point, the link between your Access data and the Word document is complete. You still need to specify where you want your Access data printed, however. The vehicle for doing this is the Mail Merge toolbar near the top of the Word program window, just above the document. Figure 11-4 shows what your screen should look like.

Figure 11-4 After Moved.doc is loaded.

Inserting Merge Fields
Now you need to let Word know what information you want used from the My Friends table. To indicate which Access fields you want Word to insert in the document, follow these steps:
1. 2. Position the cursor at the beginning of the first line after the date. Click your mouse on the Insert Merge Field button. You will see a list of fields in the My Friends table, as shown in Figure 11-5. Notice that if there were originally spaces in a field name, Access has replaced the spaces with an underscore. The underscore is required by Word. Access knows this and takes care of the conversion automatically.

Figure 11-5 Selecting fields to insert.

Field Name Conversion
Word may not understand all the field names you have defined in Access. This is because Word is much more strict on what field names it can accept. To compensate for this, the Microsoft Word Mail Merge Wizard converts field names automatically. The following rules are followed: 1. Field names longer than 20 characters are truncated (cut off). 2. If a field name contains any characters other than letters, numbers, and underscores, these characters are converted to underscores. There are no changes to the original field names stored in Access. The change affects only the communication between Access and Word.

1.

Select the FirstName field. Word inserts the field in the document. Press the SPACE BAR. This space will separate the first name and last name. Click your mouse on the Insert Merge Field button and select the LastName field. Word inserts the field in the document. Advance to the next line in the document.

2.
3. 4.

5.

Click your mouse on the Insert Merge Field button and select the Address field. Word inserts the field in the document. 6. Advance to the next line in the document. 7. Click your mouse on the Insert Merge Field button and select the City field. Word inserts the field in the document. 8. Type a comma and a space. These will separate the city and the state. 9. Click your mouse on the Insert Merge Field button and select the State field. Word inserts the field in the document. 10. Press the space bar twice. These two spaces will separate the state and the ZIP Code. 11. Click your mouse on the Insert Merge Field button and select the Zip_Code field. Word inserts the field in the document.

12. Position the insertion point on the next line, after the word Dear but right before the comma.
13. Click your mouse on the Insert Merge Field button and select the FirstName field. Word inserts the field in the document.

You are now done putting your merge document together. Figure 11-6 shows how your screen should look.

Figure 11-6 The finished merge document.

Merging the Document and the Data
At this point, you have several options:

• • •

You can save the document and print it later. You can create a brand new document that is the result of the merge document and the data being joined together. You can print the merged documents directly to the printer.

For right now let's choose the second option. To do this, click your mouse on the Merge To New Document tool (this is the fifth tool from the right on the Mail Merge toolbar). Word creates a brand new document, with each page an exact copy of your one-page letter. The only difference is that each page is addressed to a different person. Your mail-merge operation has been a success!

What About Updates?
The Microsoft Word Mail Merge Wizard creates a dynamic link between your Access data and the Word document. This link means that any time you modify the data in the Access table, the updates are automatically available in your Word document. All you need to do is merge the information again from within Word. This is done in the following manner:
1. 2. 3. Start Word. Open the Moved.doc document. Click your mouse on one of the merge buttons on the Mail Merge toolbar.

That's all there is to it! If, for some reason, you need to update the Access data while you are using Word, you can click your mouse on the Edit Data Source tool on the Mail Merge toolbar. This opens the Access program window and displays the Datasheet for the table (My Friends). Again, any changes you make are immediately available to Word.

Chapter 11: Using Your Data with Word and Excel
(This is section 3 of 4 in this chapter)

Sharing Data with Excel
When it comes to analytical capabilities, Access is somewhat lacking. If you really want to analyze numerical data in your database or create a top-notch chart, you need to use a spreadsheet program. This is where Excel comes in handy. Excel lets you analyze your data in ways it is impossible to do in Access. Recognizing that fact, Microsoft made sure you can easily use your Access data with Excel. The following sections detail how you can use your Access data in an Excel workbook.

Transferring Data to Excel

Access makes it very easy to transfer an entire table to Excel. To do this, open a database such as Customer.mdb, which you created earlier in the book. To then export a file to Excel, follow these steps:
1. 2. 3. 4. Display the Database window. If you want to use information from a table, click on the Tables button. If you want to use information from a query, click on the Queries button. Select the name of the table or query you want to use. Choose the Office Links option from the Tools menu, and then choose Analyze It with MS Excel from the resulting submenu.

Access will transfer the information in the selected table or query into an Excel file format. Excel will then automatically start and load the new file (see Figure 11-7). This is the only step you need to take in Access; everything else happens in Excel.

Figure 11-7 Your table loaded in Excel. Notice that the file uses a name (Business Customers.xls) derived from the name of your Access table. The name of the table was Business Customers; the name of the Excel file is Business Customers.xls.

Sometimes, you will want to transfer only part of a table or query. For example, you may want to export only records 5 through 13 of the Business Customers table. To export only part of a table or query, perform these steps:
1. 2. 3. 4. Open the Datasheet for the Business Customers table. Select (highlight) rows 5 through 13 or the data you desire. Display the Database window. Choose the Office Links option from the Tools menu, and then choose Analyze It with MS Excel from the resulting submenu.

Notice that the steps are essentially the same as those you used when transferring an entire table. The only difference is that you select a portion of a table or query, instead of the whole thing. Access and Excel take care of the rest, transferring the desired data and displaying it in Excel.

Linking Information Between Access and Excel
So far you have learned how to output information from Access to Excel. There may be times when you want to establish an actual link between information in Access and Excel. Links are established using OLE (object linking and embedding), a standard for sharing information among Windows programs. The easiest way to establish a link between Excel and Access is to use Windows cut-and-paste capabilities. To establish a link between Access and Excel, perform these steps:
1. In Access, select the table or query you want linked to Excel. (Note that you cannot establish an active link between Excel and only a portion of an Access table or query.) Choose the Edit menu Copy option or press CTRL+C. Access will copy the information to the Clipboard. Open your Excel window and select the spreadsheet cell where you want the link to begin. Choose the Edit menu Paste Special option. Excel will display the Paste Special dialog box, shown in Figure 11-8.

2.
3. 4.

Figure 11-8 The Paste Special dialog box.
1. 2. 3. Make sure you select Paste Link at the left side of the dialog box. This forces a link between Excel and Access. In the As list, select a data format option. (See the sidebar for additional information on data formats.) Click on OK.

Access pastes the information in the Clipboard into the Excel spreadsheet and establishes a link between the information and the original table or query. Your screen will look similar to what is shown in Figure 11-9.

Figure 11-9 Excel, after linking part of an Access database.

Data Formats When Pasting in Excel
When you paste your Access information in Excel, you may have noticed that there are a number of different data formats you can use (see Figure 11-8). Excel offers these choices when pasting Access information:

Biff5. This is the Microsoft Excel Binary Interchange File Format, version 5. (That is a mouthful, isn't it?) This format allows information to appear as close to the original format as possible. HTML. Excel pastes information in HTML format, which is the native format of the Web. Unicode Text. The text of the original information is pasted using Unicode (two-byte) characters. All other attributes (formats, appearance, etc.) are left up to Excel. You would use this format if the original information contains foreign language symbols. Text. Excel pastes the text of the original information. All other attributes (formats, appearance, etc.) are left up to Excel. CSV. Excel interprets the incoming data as "comma-separated values," meaning that it tries to break fields based on locations of commas in the text. This is not necessarily a good format to use when pasting information from other Microsoft Office programs. The format you choose should be based on how you want your information to ultimately appear in Excel.

What Is Transferred?
Regardless of whether you are transferring an entire table or query or only part of one, Access transfers more than just raw data. Access also transfers the following formatting information to Excel:

• • •

Font information Row height information Column width

However, if you use the Clipboard method of linking information and you choose a text-based data format, Access transfers only the data. Access also transfers a header row which it may format with shaded text (refer back to Figure 11-7). Access provides this row for information purposes only; if you created an output file as discussed in the previous section, Transferring Data to Excel, you can safely delete the row. If you established a link between Excel and Access and there is a header row, you cannot delete it.

What About Updates?
If you simply output Access information into an Excel file format, there is no relationship between the Excel data and what is in Access. In this instance, whenever there is a change in the original Access information, you will need to again output it to an Excel file. If you establish a link between Excel and Access, the information in Excel is updated automatically as you change the Access data. Specifically, Excel updates the information when you open the Excel spreadsheet. You can also update the information manually by following these steps:

 In Excel, choose the Links option from the Edit menu. Excel displays the Links dialog box shown in Figure 11-10.

Figure 11-10 The Links dialog box.

1.
2. 3.

If necessary, select the link you want to update. Click on the Update Now button. Click on the Close button to close the dialog box and return to your spreadsheet.

Chapter 11: Using Your Data with Word and Excel
(This is section 4 of 4 in this chapter)

Summary
Even though Access does a great job of managing data, it is not good at some other tasks. For those purposes, you use programs such as Word and Excel, which join Access as members of the Microsoft Office family of productivity software. Combining the capabilities of Access and both Word and Excel was the focus of this chapter. Before you move on to Chapter 12, make sure you understand the following key concepts:

• • • •

Word can use the information in an Access table in a mail merge document. You can use mail merge documents for a variety of purposes including form letters, mailing labels, envelopes, catalogs, and order forms. To use the Microsoft Word Mail Merge Wizard, you first select the table or query you want to merge with the document, choose Office Links from the Tools menu, and then choose Merge It with MS Word from the submenu. You can merge Access data with either an existing Word document or a new document. Once the Word document is displayed on your screen, the Access data is already linked. All you need to do is indicate where the merge fields (Access table or query fields) should be inserted in the document.

• • • • •

After you position the merge fields, there are several ways you can process the Word document. You can print to the printer, create a new document, or save the file to disk. To output the contents of an entire table or query in Excel format, first select the table or query you want output, choose Office Links from the Tools menu, and then choose Analyze It with MS Excel from the submenu. To output a portion of a table or query, you select the portion you want output (rows, columns, or selection), choose Office Links from the Tools menu, and then choose Analyze It with MS Excel from the submenu. You can establish a link between Excel and Access by copying an entire table or query to the Clipboard and then using the Paste Special option from Excel's Edit menu. You can update links manually by choosing the Links option from Excel's Edit menu and then clicking your mouse on the Update Now button.

In Chapter 12, you will learn how you can take advantage of the World Wide Web within Access

Chapter 12: Using Web Features
(This is section 1 of 4 in this chapter)

A couple of years ago Microsoft made a big push to provide Internet features for all of their products. Access is no exception; it includes several features which provide close ties with the Internet and the World Wide Web, in particular. Like several other Microsoft products, Access now has close ties to the Internet. In this chapter you will learn how to take advantage of the majority of Access's Web-specific features. Here you learn the following:

• • • •

What the Web is and how it works How to use hyperlinks in your databases How to create Web pages in Access How to conduct a Net meeting

What is the Web?
There is a very good chance that if you are reading this book, you are already familiar with what the Web is, although you may not understand exactly how it works. While this chapter does not provide a detailed treatise or history of the Web, it does provide some background information you will find helpful as you try to figure out how Access is working with the Web. Believe it or not, the Web is relatively new. It has only been around for the past five or six years, and only really popular for the past four. The World Wide Web (or Web, for short) is only a portion of the Internet, although many people use the two terms (Web and Internet) interchangeably. The Internet is a world-wide network of computers that enables the common use of helpful tools, including (but not limited to) the following:

• • • •

e-mail FTP telnet gopher

• • •

World Wide Web WAIS Internet telephony

Notice that the Web is only a single tool, although it is a very popular tool. In fact, only one other tool (e-mail) is more popular than the Web.

How the Web Works
The Web functions using a type of technology referred to as client/server. This simply means that somewhere there is a computer program that is functioning as a server, and elsewhere there is one functioning as a client. The client talks to the server, and the server provides information requested by the client. This symbiotic relationship means that each program can specialize in a portion of the communication necessary to make the Web work. The server portion of this equation is supplied, oddly enough, by a program known as a Web server. These programs run on computers that are connected to the Internet around the clock. They are constantly "listening" to their Internet connection to see if there are any clients out there who need information from them. There are literally millions of Web servers available through the Internet. The client portion of the equation is provided by a program known as a Web browser. You probably have such a program installed on your computer. These go by names such as Internet Explorer (developed by Microsoft) or Netscape Navigator (developed by Netscape Communications). Figure 12-1 shows an example of such a Web browser.

Figure 12-1 Browsers are used to display Web pages. When you are accessing the Web from your computer, your browser (the client) is communicating with some Web server (the server) to request, retrieve, and display the information you see on your screen. Each "batch" of information returned by the Web server and displayed by your Web browser is referred to as a Web page. Each Web page has a unique address (discussed in the next section) and no set size. Instead, the size is determined by the person that developed the Web page, and can be as large or small as they feel necessary.

Web Addressing
It was mentioned in the previous section that every Web page you can access has a unique address. These addresses are necessary in order for your Web browser to locate and request the Web page from the proper Web server. Remember, there are millions of Web servers accessible through the Internet, and each one may have dozens or thousands of Web pages which it can provide to your Web browser.

If you take a look at the top of your Web browser, chances are good that you can see the address you are currently viewing. Looking back at the top of Figure 12-1, you can see the address, as shown in Figure 12-2.

Figure 12-2 Every Web page has an address. While this address may look confusing if you are just starting to become familiar with the Web, it is almost second nature to long-time users. It doesn't take long to understand addresses, once you know how they are put together. Every address is composed of the same basic parts, as follows:

Protocol designator. This indicates what communications standard should be used by your Web server. In Figure 12-2, this portion of the address is the characters "http://". For many modern Web browsers, this portion of the address is considered optional. If you don't provide it, the Web browser assumes you meant to and provides it for you. Server name. This indicates the name of the server responsible for the Web page you want. In Figure 12-2, this is the "www.vitalnews.com" portion of the address. This name is unique, and is registered with a world-wide directory service that ensures no one else can use it. Directory path. This indicates the directory path and optional name of the file in which the desired Web page is stored. In Figure 12-2, this is the "/WordTips/" portion of the address. If no file name is provided (which is the case here), the Web server provides a default file designated for the directory specified.

• •

Note: You may have heard people refer to a URL before. This is an acronym for Uniform Resource Locator, and is simply a fancy name for a Web address. Web addresses are the key to using the Web. Without them, your Web browser doesn't know what you want to do. With them, it can retrieve information from all over the world. Access, as you will shortly see, can also recognize and use Web addresses.

Chapter 12: Using Web Features
(This is section 2 of 4 in this chapter)

Using Hyperlinks
If you have been using the Web for any length of time, you are probably familiar with hyperlinks, even though the name may not be familiar. Hyperlinks are areas in a Web page where you can click your mouse and subsequently visit a different place on the Web. A hyperlink can be text, or it can be a graphic. In Figure 12-1, the hyperlinks that are easiest to recognize are those which show up as underlined text. If you move the mouse pointer over

these, it changes to a pointing hand. Click the mouse button and the Web browser displays the page pointed to by the hyperlink. The whole purpose of hyperlinks is to make it easy to navigate the Web. You can literally "click and go" to jump to related or interesting information anywhere in the world. This navigation process is referred to as "surfing the Web." Access allows you to include hyperlinks in your database. These hyperlinks don't just need to refer to Web pages available through the Internet. Indeed, they can refer to other locations in an Access database, or even locations in documents maintained by other Microsoft Office products.

The Hyperlink Data Type
You learned about data types in Chapter 4, "Creating Another Database." One of the data types supported by Access is the Hyperlink data type. This is a special data type that Access uses to identify addresses which should be treated as links. The links can be either to other data or to Web addresses. Information stored in a Hyperlink field consists of four parts, as follows:

• • • •

Text to display. The text that represents the hyperlink. This is analogous to the underlined text on a Web page (see Figure 12-1). Address. The address that serves as the target of the hyperlink. (You learned about addresses earlier in this chapter.) Subaddress. A location within a document specified by the Address portion of the hyperlink. Screentip. The ToolTip displayed when the mouse pointer hovers over the hyperlink.

The only part of these four that is mandatory is the address; the others are optional. Each part of a complete hyperlink is separated from the others by a # sign. Thus, the following is a valid Hyperlink field, in the eyes of Access: My Link#http://www.web.com/Stuff/ThisPage.htm##Cool location! This example does not include the optional subaddress, so that portion is left blank. All the other portions are included. While you can construct your hyperlinks by hand (using the four parts described previously), you don't have to. Fortunately, Access offers an easy way to insert hyperlinks into a Hyperlink field, as described in the following section.

Defining a Hyperlink
Before you can define a hyperlink in Access, you need to know the address you want to use for the hyperlink. Remember that hyperlinks are designed to be "clickable," and when someone clicks their mouse on one, they are taken to the target of the hyperlink. This target is nothing more than an address, which you learned about earlier in the chapter. Fortunately, Access allows you to discover the address you want used for your hyperlink after you start the insertion process. First, select the field in a Datasheet or a form that will contain the hyperlink. This field should use the Hyperlink data type, as defined in the previous section.

Then choose Hyperlink from the Insert menu. Access displays the Insert Hyperlink dialog box, as shown in Figure 12-3.

Figure 12-3 You can insert a hyperlink in Access. The Insert Hyperlink dialog box may look overwhelming at first, but it is really quite manageable if you remember that there are only two things you need to provide: text for your hyperlink and an address. In fact, if the field you selected before displaying the Insert Hyperlink dialog box had any text in it, the Text To Display field should already be filled in. The text you specify (or accept) in this field is what ends up underlined and "clickable" in the field. The other field in the dialog box is where you specify the address for your hyperlink. If you already know the address, simply type it in using the full and proper format, as described earlier in the chapter. If you don't know the address, or you are unsure of how it should be put together, you can use the controls in the dialog box to help find the address. For example, let's assume you wanted to create a hyperlink to a form in a different database. To do this, all you need to do is click your mouse on the File button. This displays the Link to File dialog box, as shown in Figure 12-4.

Figure 12-4 You can select a file to use for your hyperlink target. This dialog box is very similar to a standard Open dialog box. All you need to do is use the controls in the dialog box to help locate the file you want to use for the target of your hyperlink. When you have found the file and selected it, click your mouse on the OK button. Access places the Web address of the file (even if it is on your machine) into the address field of the Insert Hyperlink dialog box. You are still not done, however. Remember that you wanted your link to specify a form in the database. To specify a particular query, click your mouse on the Bookmark button. Access displays the Select Place in Document dialog box, as shown in Figure 12-5.

Figure 12-5 Picking an object from a database. Here you can use the tree structure in the dialog box to select an object in the document. (Yes, Access refers to databases using the generic name "document.") Simply click your mouse on the plus sign beside the Forms category to see the forms defined in the database. Then select the desired form from those listed. When you click your mouse on the OK button, Access modifies the address in the Insert Hyperlink dialog box so it shows the database file name and object name in the database. Figure 12-6 shows what the dialog box looks like with the address filled in.

Figure 12-6 Access determines an address based on your specifications. If you had not filled in anything previously in the Text to Display field, Access would have automatically filled it with the same hyperlink address you just put together. You can still change the text in the field, using any text you desire. When you finally click your mouse on OK, Access inserts the hyperlink in your document, in the field you indicated.

Activating a Hyperlink
As you learned earlier in the chapter, when you are using a Web browser and you move your mouse pointer over a hyperlink, it changes to a pointing hand. When you click on the mouse button, the Web browser jumps to the Web address that is the target of the hyperlink. Access is no different. You use your newly defined hyperlinks by moving the mouse pointer over them. The pointer changes to the same pointing hand you use in your Web browser. When you click your mouse on the hyperlink, Access jumps to the location. If necessary, Access even opens the application program required to display the target. For instance, if the hyperlink is to a location on the Web, your Web browser is opened. If the hyperlink is to a Word document, then Word is opened. It is interesting to note that you can move the mouse pointer over a hyperlink, and if you leave it poised there (when it is shaped as a pointing hand), a ToolTip appears beside the pointer to indicate the target address associated with the hyperlink. This is a handy way to tell where clicking your mouse on a hyperlink would take you, before actually taking the plunge.

Editing a Hyperlink

It is not uncommon to need to change either the text used by a hyperlink or the hyperlink target. Access allows you to change both of these. The easiest way is to simply right-click your mouse on the hyperlink, choose Hyperlink from the Context menu, and then choose Edit Hyperlink from the submenu. Access displays the Edit Hyperlink dialog box, as shown in Figure 12-7.

Figure 12-7 The Edit Hyperlink dialog box. The Edit Hyperlink dialog box looks almost exactly like the Insert Hyperlink dialog box used earlier in the chapter. All you need to do is change either the text or the target, as desired. When you are satisfied with your changes, simply click your mouse on OK.

Deleting a Hyperlink
There may come a time when you no longer need a particular hyperlink in your worksheet. Fortunately, deleting a hyperlink in Access is very easy to do. There are two ways you can go about it. First, you can right-click your mouse on the hyperlink, thereby displaying a Context menu. From the Context menu, choose the Hyperlink option. This displays a submenu from which you should choose Remove Hyperlink. Access dutifully removes the hyperlink. This method of deleting a hyperlink is handy if you like to work almost exclusively with the mouse. If you are more of a keyboard-oriented person, all you need to do is select the field in which the hyperlink is located and press the DEL key. You cannot select the field, however, by clicking on it with the mouse; trying to do so is the same as activating the hyperlink. Instead, you must select a field adjacent to the hyperlink field, and then use the arrow keys to select the field whose hyperlink you want to delete.

Chapter 12: Using Web Features
(This is section 3 of 4 in this chapter)

Creating Web Pages
One of the major ways in which Internet capabilities have been melded into Access is in its ability to create Web-page output. Using Access you can create three types of Web pages. The type you should create depends on what type of data you are using and what you ultimately want to do with that data. Access allows you to create these types of Web pages:

• • •

Static HTML. This is the simplest form of Web output you can create. The pages represent a single view of your data at the time the pages are created. Server-generated HTML. This type of Web output allows you to work with a specific Web server to create dynamic output that presents your data in a table format. Information in the Web page is generated as of the time the user accesses the page. Data Access Pages. These are actually database objects (just like tables, forms, and queries) that allow the complete sharing of information over the Web.

You should note that even though Access makes creating a Web page relatively easy, getting others to see your information on the Web may not be as easy. The biggest hurdle, for many people, is getting access to a Web server where their information can be made available. If your company has such a server, or you have rented space on someone else's Web server, you will need to transfer your Access-created Web materials to the server before they can be accessed by others. In the case of the more dynamic page-creation methods (server-generated HTML and data access pages), you should discuss what you want to do with the Web administrator. They may need to make some changes in how the server functions before you can use these types of pages.

Creating a Static HTML Web Page
Creating a static HTML Web page from an existing database is very easy in Access. All you need to do is either select the database object you want used as the basis for your Web page, or display the information you want used. Then choose Export from the File menu. This displays the Export dialog box shown in Figure 12-8.

Figure 12-8 The Export dialog box. At the bottom of the Export dialog box, you should make sure that you are saving your files as HTML documents (see Figure 12-8). You should also make sure the Save Formatted check box is selected; this ensures that Access writes the information out in fully functional HTML format. You can then use the other controls in the Export dialog box to specify a file name for the Web page you are creating, as well as a location. Once you click your mouse on Save, Access quickly displays the HTML Output Options dialog box, shown in Figure 12-9.

Figure 12-9 The HTML Output Options dialog box. Access is asking for the location of an HTML template file. This is a file, peculiar to Access, that defines how a Web page should be constructed. Template files can help enhance the appearance of your Access Web pages. Unfortunately, the template files are something you must create by hand; they are not created automatically by Access. On the plus side, they are not mandatory to the creation of Web pages using Access. Note: For more information on HTML template files, use the index of the Access online Help system to search for "HTML template." Using the information provided there, along

with your knowledge of how to hand-code HTML files (or a very good HTML tutorial book), you can create your own template file, if desired. Once you have designated which template file (if any) you want to use, click your mouse on the OK button. (If you don't have a template file, as in this instance, leave the HTML Template box blank and click your mouse on the OK button.) Access then generates the Web page according to your specifications. As an example, Figure 12-10 shows an example query Datasheet in Access, while Figure 12-11 shows the Web page generated by Access from that query.

Figure 12-10 A sample query Datasheet in Access.

Figure 12-11 The same information from an HTML file, in a Web browser. You should note that the Web page shown in Figure 12-11 was viewed on a system that did not even have Access 2000 loaded on it. This means that anyone with a Web browser will be able to view your information without any problem. Note: Exactly how your information is displayed may differ from one browser to another. Standards within the Web leave it up to the browser to determine how characteristics are displayed. Thus, one browser may display columns or fonts or bold text a bit differently than is done in a different browser. It is impossible for Access to anticipate and overcome these differences.

Creating Server-generated HTML Web Pages
One of the enhanced Web pages you can create with Access is referred to as server-generated HTML Web pages. Notice that these are server-generated pages. That should give you the first clue that the display of the information is dependent on the server, not on Access itself. In a Microsoft-centric world, this means that you must use one of Microsoft's Internet Information Server (IIS) platforms on a Windows NT Server system in order for others to access your data

using these types of pages. As long as you are using that type of Web server, you can use these types of Web pages. Note: Make sure you don't confuse the server with the browser. The key component here is the Web server; as long as the server is IIS, any browser can access servergenerated HTML Web pages from Access. Because generation of this type of Web page is server-dependent, most of the work in making the information available on the Web must be done by your Web administrator, not by you. If your administrator needs help in this area, any good Internet Information Server book should include help on setting up and publishing ASP or IDC/HTX pages from Access data.

Creating Data Access Pages
Data access pages are a new feature in Access 2000. These pages are not dependent on a particular Web server (as you learned about in the previous section), but are instead dependent on the use of Internet Explorer 5, the latest version of Microsoft's Web browser. (IE5 is provided with Microsoft Office 2000, of which Access 2000 is a component.) In reality, data access pages are saved as an object in your Access database, just like your tables, queries, and forms. There are two ways you can create data access pages: using a Wizard or from scratch. The following sections detail both approaches.

Using a Page Wizard
To create a data access page, first display the Database window and click your mouse on the Pages button. You can then double-click your mouse on the Create Data Access Page by Using Wizard option. Shortly Access displays the Page Wizard dialog box, as shown in Figure 12-12.

Figure 12-12 The Page Wizard dialog box.

At the left side of the Page Wizard dialog box, you can use the drop-down list to specify what Access should use as the source for this new data access page. Remember that you can use either tables or other queries as your source. In Figure 12-12, the Business Customers table is selected. (This is a table developed earlier in this book.) In the lower-left portion of the dialog box is a list of all the fields defined in the selected table or query. If you change the data source in the pull-down list, then the available fields change, as well. You can use the list, and the controls just to the right of the list, to indicate which fields you want included in the data access page you are creating. For the purposes of this example, select the Company Name, Address 1, Address 2, City, State, and Zip Code fields. Make sure that after selecting each one, you click your mouse on the > button. Access then displays the name of each field in the Selected Fields list at the right side of the dialog box. When you are done selecting these fields, the dialog box appears as shown in Figure 12-13.

Figure 12-13 The Page Wizard dialog box after selecting some fields. With the fields selected, you are ready to proceed to the next step in the Wizard. Click your mouse on the Next button, and Access displays the dialog box shown in Figure 12-14.

Figure 12-14 Picking which fields to group by. Your next step is to determine if you want your information grouped in any particular order. Grouping is routinely done in reports, and should be done based on the intended use of your data. For instance, if you were creating a data access page showing sales for a particular company, you might want the information in the report grouped by month, by salesperson, or by territory. In this example, it might be helpful to group your customers by state. To do this, simply choose the State field at the left side of the dialog box and click your mouse on the > button. Access changes the Report Wizard dialog box to show how your grouping will now occur, as shown in Figure 12-15.

Figure 12-15 Grouping helps you organize the output in a data access page. Notice that the State field is moved to the top of the sample layout, and the Grouping Options button becomes active. For some types of output you may want to change the grouping options. Doing so allows you to specify how Access should analyze the field by which you are grouping. In this case, changing the grouping options is not necessary. To proceed to the next step, click your mouse on the Next button. Access displays the dialog box shown in Figure 12-16.

Figure 12-16 Picking which fields to sort by. Now you need to decide how you want your information sorted. Access allows you to specify up to four fields by which sorting can be done. In most instances you will use only one or two fields. It makes sense that if you are creating a list of your customers, you would want them in alphabetical order by name. Thus, you should choose the Company Name field. Use the pull-down list for the first sort field to select the Company Name field. If desired, you can click your mouse on the order button to the right of the field to indicate whether Access should sort the field in ascending order (the default) or descending order. When you are done picking the fields you want to sort by, you can click your mouse on the Next button to proceed to the next step. Access displays the dialog box shown in Figure 12-17. This is the final dialog box in this Wizard.

Figure 12-17 The final dialog box for the Page Wizard. Here you can enter a name that you want used to save your data access page. The suggested name is simply the table name on which the page is based (Business Customers). Change the name to Customer Addresses, by State. Next, click your mouse on the Open the Page option button, then click on the Finish button. Access displays the results of your page, as shown in Figure 12-18.

Figure 12-18 The results of the Page Wizard. This viewing is being done solely in Access, not in a browser. When you are done looking at the information in the page window, close it by clicking your mouse on the Close icon in the upperright corner of the window. Access asks you if you want to save changes to the data access page. If you click your mouse on Yes, you will see the Save As Data Access Page dialog box, shown in Figure 12-19.

Figure 12-19 Saving your HTML page. Here you need to provide a name for the data access page you are creating. This is the name that will be used for the actual HTML file you will publish on your Web server. You should supply the name, then click your mouse on Save. Access generates the page and saves it in the Database window. Later, someone can access the file from within Internet Explorer 5 (see Figure 12-20). Notice that the controls look almost exactly the same as what you see in Figure 12-18. The user can now use these controls to navigate through the data in the table.

Figure 12-20 Viewing a data access page in the Web browser.

Designing Pages from Scratch
To create a data access page from scratch, first display the Database window and click your mouse on the Pages button. Then double-click your mouse on the Create Data Access Page in Design View option. Access displays the Design view window for data access pages, as shown in Figure 12-21.

Figure 12-21 Creating a data access page from scratch. Notice that this Design view page is very similar to what you use to create both reports and forms from scratch. You can use many of the same techniques you learned in Chapter 9, "Creating Custom Forms," and that you will learn in Chapter 14, "Creating Complex Reports," in creating data access pages from scratch. The first task you need to do is bind a data source to the data access page. This data source can be either a table or a query. You do this by first choosing Properties from the View menu. Access displays the Properties dialog box for your page. Click your mouse on the Data tab in the dialog box, and Access displays it as shown in Figure 12-22.

Figure 12-22 The Properties dialog box. When you select the RecordSource property, a pull-down list becomes available. Using the pulldown list, pick a table or query you want to use. For this example, I selected the query named "Active Davis Accounts Over $1,000," which was developed earlier in this book. Access then displayed a field list for the query, as shown in Figure 12-23.

Figure 12-23 The Field List dialog box. To add fields to the data access page, select the fields desired and click your mouse on the Add To Page button in the Field List dialog box. Once the desired fields are added, you can close the Field List dialog box and focus on the layout of your page. You do this by using the tools in the Toolbox to add elements to the page, as desired.

When you are through designing your data access page, click your mouse on the Close icon in the upper-right corner of the Design window. Access asks you if you want to save the page. When you click your mouse on Yes, you are given the opportunity to specify a name and location for the page, using the Save As Data Access Page. (See Figure 12-19.) Once saved, your page appears in the Database window of Access as a Page object. The page can also be viewed using Internet Explorer 5 by anyone that has access to the page.

Conducting a NetMeeting
Microsoft NetMeeting was originally a module used with Internet Explorer. It has been integrated into Office 2000 programs to facilitate the sharing of files over the Internet. Much of the technical information about using NetMeeting is beyond the scope of this book, but it is helpful to understand the basics and how you can use NetMeeting with Access. Access makes it easy for you to both schedule and be involved in online meetings. If you want to schedule a NetMeeting, using Outlook, you simply need to choose Online Collaboration from the Tools menu, and then choose Schedule Meeting. The exact steps you may go through from this point depend on many variables, including how you have Outlook configured on your system. If you are familiar with Outlook, however, you should be able to follow the on-screen directions to schedule your meeting. Note: The NetMeeting portion of Access relies very heavily on modules originally in other Microsoft programs such as Internet Explorer and Outlook. If you have not used those programs, it will be very difficult for you to understand and use the features originally belonging to those programs, even if they are now accessible from Access. For more information, refer to any good Outlook or Internet Explorer book. If you use Outlook in your office, you may have received a reminder concerning an online NetMeeting. To join the meeting, all you need to do is click your mouse on the Start NetMeeting button. If you want to decline the meeting, click your mouse on the Dismiss button. (Both of these buttons are available in Outlook.) If you don't use Outlook, but you have NetMeeting running in the background on your system, you may have received a notice that you are invited to a meeting. If this is the case, click your mouse on Accept to join in the meeting. Running Microsoft NetMeeting in this way (in the background) is done not from within Access, but directly from the Windows desktop. Participants in a NetMeeting can see and comment on the database under discussion. If you are the host of an online meeting (the person that started the meeting), you have ultimate control over the database. You can allow others in the meeting to make changes to the database, as desired. Others in the meeting can see any changes being made, as they are made.

Chapter 12: Using Web Features
(This is section 4 of 4 in this chapter)

Summary

In this chapter you have learned how you can take advantage of the Web, without even leaving Access. Many Web-centric features with which you may already be familiar are also implemented in Access. For instance, you can use hyperlinks to jump from one location to another. In this chapter you have learned the following:

• • • • • •

The World Wide Web is just a portion of the Internet, second only in popularity to e-mail. Access allows the insertion of active hyperlinks in a table field. Once hyperlinks are in place, you can easily edit or delete them. Hyperlinks are activated in Access in the same way they are activated in a Web browser. You can use Access to generate Web pages in three different ways: static HTML, server-generated HTML, and data access pages. You can participate in a NetMeeting while you are using Access.

In Chapter 13, you will learn how you can relate tables to each other.

Chapter 13: Understanding Data Relationships
(This is section 1 of 3 in this chapter)

So far in this book, you have focused primarily on using simple, straight-forward data structures to maintain your information. Access is not restricted, however, in how you can organize your data. It also lets you set up relational databases. In this chapter, you will learn what a relational database is and how to create one in Access. By the time you finish this chapter, you will understand the following key concepts:

• • • • •

What a relational database is How information in tables can be related Why keys are important in a relational environment How to create a relationship between two tables What referential integrity means

Understanding Data Relationships
You may have heard or read somewhere that Access is a relational database. In general, a relational database is a table-based database in which the information in one table relates (corresponds) to information in another table. For example, you may have a database which you use to keep track of your music collection. In one table, you have all your CDs. In another, you have all the music artists in your collection. The information in the one table relates to the information in the other since every CD has at least one musical artist. Relationships within a database are often based on relationships between objects in everyday life. For instance, the relationship between a CD and a musical artist is one that exists in the real world. Likewise, a real-world relationship exists between inventory products and the companies that make those products. In a database, you may have one table that contains

inventory products and another that contains vendors. When you tie the two tables together, you create a relational database. The idea behind relational databases is to limit the amount of redundant information in a database. Consider the inventory example again. If you want to keep track of your inventory, you might have a table that includes the following items:

• • • • • • • • • • • • • •

Item ID Description Color Weight Cost Price Quantity On Hand Reorder Level Vendor Name Vendor Address Vendor City Vendor State Vendor Zip Code Vendor Phone

For most inventories, you need to maintain this information for each item. The problem occurs when you start ordering more than one item from a given vendor. Pretty soon, you have a lot of redundant information in your table. Redundant information simply consumes disk space. Consider how much space could be wasted by thirty occurrences of the same vendor address and phone number! Multiply that wasted space by several dozen vendors, and you can begin to see the magnitude of the problem. Wasted space is only one problem. Another is that redundant interaction increases the chance of error. For instance, when employees enter new inventory items, it is possible that they will enter typos when they put in vendor names, addresses, and phone numbers. Down the road, when you need to refer back to the information, how do you determine which names, addresses, and phone numbers are correct? Still another problem is that redundant data increases your update and processing time. For instance, if you need to change the phone number of a vendor, you must pull up each inventory record and retype the information. As you can guess, such changes can take quite a bit of time. If you use a command (such as the Replace command in the Datasheet), the command may not change some numbers if they were initially entered incorrectly. All in all, the redundant data increases your personal management tasks. Luckily, using a relational database, you can use two related tables, one containing inventory items and one containing vendor information. For instance, consider the following two tables:

Item ID Description Color

Vendor ID Name Address

Weight Cost Price Quantity On Hand Reorder Level Vendor ID

City State Zip Code Phone

Notice that the Vendor ID field exists in both tables. Access uses the Vendor ID field to tie the information in the inventory table to the information in the vendor table. By setting up your database in this way, you don't have any redundant information and thus you eliminate many of the problems inherent in the other layout:

• • •

Less storage space. Since there is only one record for each vendor, you eliminate the redundancy inherent in the other layout. Less chance for error. Since vendors are not being entered more than once, human error is less likely to creep into the table. Faster processing time. One record per vendor means you can make universal changes quicker. (Actually, there is no universe to change--there is only one record.) Also, you can do other changes faster, such as an across-the-board price increase.

What Type of Relationships Can Exist?
Information in two tables can relate in any of three different ways. These ways are based on the number of possible relations between the records. These three types of relationships are:

• • •

One-to-one One-to-many Many-to-many

A one-to-one relationship indicates that for each record in the first table, there is one corresponding record in the second table. For instance, you might have two tables, one with general customer information and another with detailed customer information. For each record in one table, there is a corresponding record in the other. For security reasons, you might want to set up a one-to-one relationship; the general information may be open to anyone within your company, but the detailed information may be available to only those with a particular security clearance. A one-to-many relationship indicates that for each record in one table there are many records in the other table. For instance, in the example given in the previous section, for each record in the inventory table there is only one corresponding record in the vendor table. This is because each inventory item has only one supplier. However, for each record in the vendor table, there could be many records in the inventory table--because a particular vendor may supply more than one inventory item. The many-to-many relationship is the most complex relationship. For any record in either table, there can be many corresponding records in the other table. The classic example of a many-to-many relationship is the relationship between a table that contains invoices and one that contains inventory. Each invoice record can refer to many different inventory items, and each inventory item can refer to many different invoices.

Revisiting Keys
In Chapter 2, "Access in an Hour," you learned about primary keys. You learned that keys provide a method for Access to quickly organize and retrieve data. In a relational environment, however, keys can do much more than that. Keys, in effect, become the tie that binds two tables together. For instance, in the inventory example provided earlier, the key in the vendor table is Vendor ID. This key field is also used in the inventory table to tie it to the vendor table. In the vendor table, the Vendor ID is unique; in the inventory table it is not.

Characteristics of Keys
A key for any given table possesses three attributes which are important to remember:

• • •

It must be unique It cannot be undefined (null) It is, by definition, indexed

A key can be an account number (as in a Vendor ID), social security number, license number, part number, or any other numeric value or combination of characters that are unique. That is the important thing--the key is unique. No other row in the table can have the value of the key. However, other tables may share the same set of key information, as was pointed out in the inventory example. Text names (such as company names or titles) are not generally unique and cannot be used in math operations; therefore, they do not make good keys. You should instead put together your own unique value. Note: Access provides a data type which is ideally suited to being a key value--an AutoNumber data type. This sequential numeric value is guaranteed to be unique, and the user cannot change it once Access assigns it to a record.

Types of Keys
There are three types of keys which you can use in a table:

• • •

Simple key Complex key Foreign key

You have actually used a simple key throughout this book. A simple key contains the value of a single column, such as an account number. Access also supports complex keys, which use the values of more than one column. For instance, you might not have a single column that uniquely identifies a record. But if you combine the contents of two columns, you can achieve the purposes of a key. As an example, assume you are developing a billing system for your company. There are different departments in your company, and each can generate their own invoices. When you look at what you can bill for, you find that it is highly possible that there will be an overlap between departments. In other words, two departments could sell and bill for the same item. Since you want to maintain separate inventories for each department, you can't simply

combine them. Your solution is to use a complex key that is composed of the department ID and the part number, together. A foreign key is one that does not belong to the current table, but instead provides a link to another table. A good example of a foreign key is the inventory and vendor tables described earlier. The Vendor ID is a simple key for the vendor table, but it is also contained in the inventory table. To the inventory table, it is a foreign key.

Chapter 13: Understanding Data Relationships
(This is section 2 of 3 in this chapter)

Establishing Relationships
Note: This section uses the Customer database you have used in several chapters of this book. If you do not have that database available, you may want to use the Customer 4.mdb database located on the CD-ROM. You should copy the file to a directory on your hard drive and rename it Customer.mdb. You now know what database relationships are and how they are put together. It is time to put this information to good use by designing your own relational database. You will do this by expanding the Customer.mdb database you developed earlier in the book. You will recall that this database contains a single table--Business Customers. If you take a look at that table, you will notice there is some redundant data in it. The Salesperson field contains the name of an in-house sales rep that is repeated over and over again in the records. To remove this redundancy, you can put the salespeople in their own table and simply use a pointer to a Sales ID field. To accomplish this effectively, you must perform these steps:

• • • • •

Derive the unique names of existing sales people Create the table for the sales people Modify the Business Customers table Establish links between the two tables Check your links

Note: If going through this optimization process seems like a lot of work to you, you might try using the table analysis tool discussed in Chapter 10, "Using Additional Tools."

Pulling Out Unique Names
To pull out the unique names in the Salesperson field of the current Business Customers table, you can use a query. (You learned how to put queries together in Chapter 6, "Using Queries to Examine Your Data.") If you use an action query, you can create a new table with the results; that is what you will accomplish in this step.

First, you need to create a base table which can receive the results of your query. You will call this table Sales Staff. The table will serve as the basis for your work in the following section. Create this table now; it should contain the following two fields:

• •

Sales ID. This field has an AutoNumber data type. Use this field as the primary key. Name. This field has a Text data type, with a length of 50.

That's it. When you save the table, do so under the name Sales Staff. The table should appear in the Database window. Note: If you need a refresher on how to create tables from scratch, you may want to refer back to Chapter 4, "Creating Another Database." You are now ready to put together your query. To create your query, perform these steps:
1. 2. 3. 4. 5. Click your mouse on the Queries button in the Database window. Double-click your mouse on the Create Query in Design View option. Access will display the query Design window overlaid by the Show Table window. In the Show Table window, double-click your mouse on the Business Customers table name. Access displays a field list for the table in the query Design window. Close the Show Table window by clicking your mouse on the Close button. Choose the Append Query option from the Query menu. Access displays the Append dialog box, as shown in Figure 13-1.

Figure 13-1 The Append dialog box.

6.

Use the Table Name drop-down list to select the name of the table you just created--Sales Staff. Make sure the Current Database option is selected, and then click your mouse on OK. 7. In the field list for Business Customers, double-click your mouse on the Salesperson field. Access will display the field in the QBE grid at the bottom of the dialog box. 8. Specify that the field should be sorted in ascending order. 9. Use the pull-down list in the Append To cell of the QBE grid to select the Name field. This is the field in the Sales Staff table to which you want the Salesperson name appended. 10. Double-click your mouse in any part of the gray area to the left or right of the Business Customers field list. Access will display the Query Properties dialog box, as shown in Figure 13-2.

Figure 13-2 Properties dialog box for your query.
11. Select the Unique Values property; change it to Yes. This ensures that the query returns only unique values, meaning that you will only get one occurrence of each salesperson. 12. Close the Properties dialog box by clicking your mouse on the Close icon in the upper-right corner. 13. Close the query Design window by clicking your mouse on the Close icon in the upper-right corner. Access asks you if you want to save the query. 14. Click your mouse on the Yes button. Access displays the Save As dialog box, as shown in Figure 13-3.

Figure 13-2 The Save As dialog box.

15. Enter the name Pull Sales People and click your mouse on the OK button. Access saves the
query.

That's it. In a few short steps, you have created a query that will pull the desired information from one table and put it in another. You can run the query by selecting the query name in the Database window, and then clicking your mouse on the Open button. Shortly, Access will display a dialog box reminding you that your query will modify data in a table and asking you if you are sure you want to run it. Click your mouse on the Yes button, and Access runs the query. Note: If you get an error message, it means that you didn't create the Sales Staff base table, as discussed earlier in this section. When the query is completed successfully, Access displays a dialog box informing you that it is about to append ten rows. This refers to the fact that ten salespeople names were pulled from the Business Customers table, and are about to be appended to the Sales Staff table. Click

your mouse on the Yes button and Access completes the query, returning you to the Database window. You can check how you did by opening the Sales Staff table. It should look like what you see in Figure 13-4.

Figure 13-4 The Sales Staff table with the appended records. Notice that the table lists each salesperson in alphabetical order. As you can see, Access has assigned each salesperson a unique Sales ID. You are now ready to really create the Sales Staff table. Note: The query you created (Pull Sales People) has served its purpose. You can now delete the query, if you desire. There is no reason to save it.

Creating the Sales Staff Table
You now have a base table you can use for your sales people. However, the table is by no means complete. You still need to add fields for other record keeping and calculations. Take another look at Figure 13-4. Notice that there are only two fields identified for the table (that's all you created). Modify the table so it has the following additional fields:

• • • •

Last Name First Name Office Commission Rate

Again, these are just possible fields. The Office field lets you specify the office number in which the salesperson is located. The properties of the new fields are shown in Table 13-1.

Field Last Name First Name Office Commission Rate

Properties Text data type, length of 25, field is required Text data type, length of 25, field is required Text data type, length of 10 Number data type, Format is Percent

Table 13-1 Properties for the new table fields. When you are done, switch back to Datasheet view. Access will ask you if you want to save the table. Answer Yes. Access will then display the dialog box shown in Figure 13-5.

Figure 13-5 A warning from Access. Access displays this message because you have added some fields which are required (Last Name and First Name). Whether a field is required or not is part of the validation rules for a table. Click your mouse on the No button. Access will change the table structure and display the Datasheet view, as shown in Figure 13-6.

Figure 13-6 The Datasheet for the updated table. At this point you need to manually split up the names of the salespeople. Since there are only ten records, this will only take a minute or two. When you are done, switch back to Design

view and delete the Name field. This field is no longer necessary since you have updated the table with the other two fields (Last Name and First Name). When you are done deleting the field, switch back to the Datasheet. Your final table should look like what is shown in Figure 137.

Figure 13-7 The final Sales Staff table. At this point, you can add information to the other fields, if desired. For this example, there is no need to do so right now. As such, you are ready to modify the Business Customers table. Close the Sales Staff table before moving on to the next step.

Modifying the Business Customers Table
Because you have placed the Salesperson information into its own table, your next step is to modify the design of the Business Customers table. The modification will not be as extensive as what was done with the Sales Staff table. To modify the Business Customers table, perform these tasks:

 Rename the old Salesperson field. This is necessary so you can use the name for the new field.  Create a new Salesperson field. This is necessary so you can establish a link between your tables.
You accomplish both of these tasks from the Design window. From the Database window, click your mouse on the Business Customers table name and then click on the Design button. Access will display the Design window, containing the layout for the Business Customers table. Change the name of the Salesperson field to something like Old Salesperson. Next, select the row that contains the Comments field, and then select the Rows option from the Insert menu. Access adds a new row right above the Comments field, as shown in Figure 13-8.

Figure 13-8 After inserting a row. Enter Salesperson as the field name. In the Data Type column, make this field a Number. It is then very important that you set the Field Size property to Long Integer. If you omit this

change, you cannot create a link between this field and the Sales ID field in the Sales Staff table (as discussed in the next section), because both fields in both tables must be of the same data type. You are now done modifying the Business Customers table. Close the table by clicking your mouse on the Close icon in the upper-right corner of the Design window, saving your changes as you do so.

Establishing Links
As you determine how to place the links between the two tables, it is important to remember the type of relationship that exists between the two. For each record in the Business Customers table, there is one corresponding record in the Sales Staff table. This is natural; our company assigns only one salesperson to each customer. Looking at it from the perspective of the Sales Staff table, however, there can be multiple records from the Business Customers table that correspond to each record in Sales Staff. Again, this is natural. There can be multiple accounts assigned to each salesperson. Understanding how the records relate, it is easy to see that this is a one-to-many relationship. In a one-to-many relationship, the linking column is contained in the "one" table. Thus, you use the key column for the Sales Staff table to link together the two tables. Access places values from this key in the Salesperson field of the Business Customers table. To the Business Customers table, this becomes a foreign key. To establish links in Access, you begin at the Database window. Make sure you have the Tables button selected; you should see your two tables displayed in the Database window. Now choose Relationships from the Tools menu. Your screen should appear similar to Figure 13-9.

Figure 13-9 After opening the Relationships window. Notice that Access lists both of the tables in your database in the Show Table window. You use this window to select the tables or queries between which you want to establish relationships. All you need to do is select each table, in turn, and then click your mouse on the Add button. Access will display field lists for each table in the Relationships window. When you have added both tables, click your mouse on the Close button. The Relationships window will now appear as shown in Figure 13-10.

Figure 13-10 The Relationships window. To establish a relationship between the two tables, drag the linked field from one table to the other. Remember that the relationship between the two tables is one-to-many (as discussed earlier in this chapter). In this case, you should start with the "one" side of the relationship, meaning the field in the Sales Staff table. Use the mouse to point to the Sales ID field, hold down the mouse button, and drag it over the top of the Salesperson field in the Business Customers field list. When you release the button, Access will display the Edit Relationships dialog box shown in Figure 13-11.

Figure 13-11 The Edit Relationships dialog box.

Notice that the left side of the grid (in the Table/Query column) shows the Sales Staff table; this is where you pulled the Sales ID field from. The other side (in the Related Table/Query column) shows the Salesperson field in the Business Customers table; this is where you dropped the field. This is the relationship you want to create. The only other option you can choose here is to select the Enforce Referential Integrity check box. When you select this option, Access will automatically make sure that anything you enter in the Salesperson field of the Business Customers table is a valid record in the Sales ID field of the Sales Staff table. This is a powerful feature which helps to maintain the validity of your data. You should select this option. When you select the Enforce Referential Integrity check box, notice that the options at the bottom of the dialog box become available. That is because these options indicate how Access is to apply the integrity rules.

The Cascade Update Related Fields check box only has relevance if the field on the "one" side of the relationship can be edited. This is not the case with the Sales ID field; it is an AutoNumber data type, which cannot be edited. If the Sales ID field were a data type which you could edit, selecting this box would cause Access to automatically update any related records in the "many" tables whenever you made changes to the Sales ID field. Since this feature has no relevance to your table, leave this check box unchecked. The Cascade Delete Related Records check box indicates if you want records in Business Customers deleted when you delete the Sales ID record in Sales Staff table. This check box provides you with a quick way to make sure Access deletes any records belonging to a salesperson when you delete the salesperson's record. In most organizations, you don't want to do that, however. Instead, you want to reassign those accounts to a different salesperson. Leave this check box unchecked.

Now, to establish the relationship you have specified, click your mouse on the Create button. Access closes the Edit Relationship dialog box and again displays the Relationship window. Figure 13-12 shows this window with the updated link.

Figure 13-12 The updated link. You can now close the Relationship window by clicking your mouse on the Close icon at the upper-right corner of the window. Access will ask you if you want to save your change to the table layout. Click your mouse on the Yes button, and Access will save your work, returning you to the Database window. Your two tables are now related.

Checking Your Link
There are two things you still need to do with the Business Customers table. One is to check your link to make sure it is in place. The other is to get rid of the Old Salesperson field. Open the Datasheet for the Business Customers table. If you display the Salesperson column, you will notice it is empty (rightly so; you haven't put any data in it yet). Try entering a value that is out of the range of valid Sales IDs. You know the valid IDs are in the range of 1 through 10, so try putting in a 62 or some such number. When you try to leave the field, you should get an error message, as shown in Figure 13-13.

Figure 13-13 Access doesn't permit invalid reference numbers.

Access won't let you put in an invalid value. Click your mouse on the OK button to clear the message, and you can try again. There is one thing you can do to make putting in the IDs for the sales staff easier. Remember that when you exported the names of the sales staff, you did it in alphabetical order. Thus, Sales ID 1 is at the first of the alphabet, and Sales ID 10 is at the last. Select the Old Salesperson field and click your mouse on the Sort Ascending tool. This puts the records in order, by the salesperson's name. In the Salesperson column, you can now enter 1 for the first salesperson, 2 for the second, and so on. When you are through, you can switch to Design view and delete the Old Salesperson column. Your database now fully utilizes relational tables.

Chapter 13: Understanding Data Relationships
(This is section 3 of 3 in this chapter)

Summary
Access lets you set up very complex and very powerful data relationships. The ability to implement relationships between tables lets you reap many benefits which can make you more productive and your database more efficient. This chapter has introduced you to both the concepts of relational databases and the steps necessary to put them into action. Before you move on to Chapter 14, make sure you understand the following key concepts:

• • • • • • • •

A relational database is a database in which you establish links between data in one table and data in another. You establish a one-to-one relationship when a record in one table can have only one corresponding record in another table. You establish a one-to-many relationship when a record in one table can have more than one corresponding record in another table. You establish a many-to-many relationship when any record in one table can have more than one corresponding record in another table, and vice versa. Within index tables, keys establish links with information in other tables. Keys need to be unique and cannot be undefined (null). In the table that owns the key, the key field is called a primary key. If a key field is used to establish a link with another table, then in the other table it is a foreign key. In Access, you use the Relationships option from the Tools menu to create links between tables. Referential integrity means that the use of keys across a link is both consistent and correct. You can instruct Access to enforce referential integrity.

In Chapter 14, you will revisit creating Access reports. There you will learn how to use the report Design window, in detail.

Chapter 14: Creating Complex Reports
(This is section 1 of 4 in this chapter)

In Chapter 8, "Creating Simple Reports," you learned how you can use the Report Wizards to create reports. You also learned, however, that the reports you create with the Wizards are generally very simple reports. Normally, the Wizard's reports serve as a starting point for your own custom reports. In this chapter, you will learn how to create your own reports from scratch. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • •

How to display the report Design window How the report Design window relates to other Design windows What the different sections of a report are What report properties mean and how you use them How to design each section of your report How to group data in your report How to view your report as you design How to save your report within your database

Creating a New Report
Note: This section uses the Customer database you have used in several chapters of this book. If you do not have that database available, you may want to use the Customer 5.mdb database located on the CD-ROM. You should copy the file to a directory on your hard drive and rename it Customer.mdb. To create a report, first display the Reports section of the Database window and double-click your mouse on the Create Report in Design View option. Access will display the report Design window shown in Figure 14-1.

Figure 14-1 The report Design window. This window may look familiar; it is very similar to the form Design window you used in Chapter 9, "Creating Custom Forms." In fact, you will find that you are able to apply much of your knowledge about forms to the development of reports. The relevant difference between forms and reports is that forms are destined for the screen and reports are destined for the printer. Access lets you use many of the same objects and controls on reports that you use with forms. The only real difference is in the actual use of the Design window itself. Note: The toolbox works much the same in reports as it does in forms. If you need a refresher on how the toolbox works, you may want to refer back to Chapter 9. The first task you need to do is to indicate what table or query you want to use as the data source for your report. You do this by following these steps:
1. 2. Choose Properties from the View menu. Access displays the Properties dialog box for the report. Click your mouse on the Data tab.

3. 4.

Use the pull-down list for the Record Source property and select Business Customers. Access displays the Business Customers field list on your screen. Close the Properties dialog box by clicking your mouse on the Close icon in the upper-right corner of the dialog box.

You are now ready to begin exploring the report Design window and actually creating your report.

Chapter 14: Creating Complex Reports
(This is section 2 of 4 in this chapter)

Understanding the Design Window
Notice that the Design window consists of a grid that is divided into three sections. Actually, the window contains three sections, each of which contains its own grid. Later in this chapter, you will learn about the different report sections. You use the grids to specify what you want to appear in each section of the report. The idea is to design your report by placing objects on the grid, in the section where you want them to appear. Objects can be fields or things such as labels, buttons, controls, or other items. You will learn how to place objects on the grid later in this chapter. These objects have properties which tell Access how they should appear and behave. Notice that the grids in each section are not very large. The default size for the page header and footer is .25 inches, and the detail section is only two inches high. Regardless of the section, the grid is the same width: five inches. If you want to increase the size of a grid, move the mouse pointer over one of the outside borders of a grid. Next, hold down the mouse button and drag the border to a new position. When you release the mouse button, the grid remains enlarged.

Parts of a Report
You can see from looking at the Design window that a report is divided into sections. There are five sections in any Access report:

• • • • •

Report header Page header Detail Page footer Report footer

Each of these sections is independent of the others. You can place any object in any one of these sections; it is up to you. Notice that when you first start to create a report, Access only displays three of the five sections. You can control which sections Access displays using choices under the View menu. If you choose Page Header/Footer, Access displays both the

page header and footer. Likewise, if you choose Report Header/Footer, Access displays those sections. When you look at the Format menu, you may notice a check mark beside one or both of these menu items. If the check mark is present, Access is displaying the section. To turn the check mark off, select the option again. In response, Access removes the section from the screen.

Report Header and Footer
This report section defines what appears at both the beginning and end of a report. The report header information appears at the very beginning, before anything else--including the page header. It might include preface information or information about who created the report. The report footer appears at the end of a report, after the body but before the page footer. The report footer is typically used for things like grand totals or final messages.

Page Header and Footer
Each page of your report can also have a header and footer. Headers and footers contain information that you want to appear at the top and bottom of each page. The one exception is the first page, where the page header appears after the report header information. In the page header, you might include things like a page number or column heads. To increase the amount of space that appears after the page header, but before the report detail, increase the amount of white space on the grid after the last item in the page header. The opposite is true for the page footer--to leave space between the detail and the page footer, increase the amount of white space before the first item in the page footer.

Detail
You define the body of your report in the detail section. Within the report body, you list what you want to appear for each record Access prints in the report. You will do most of your work in the detail section. Access also lets you create groups within the body of your reports. Basically, groups are a way of sorting your report and creating subtotals. You can specify exactly how Access should group your data, and whether Access should include additional headers and footers around a group.

Report Properties
As you know, Access uses properties to describe how an object appears or how Access is to treat it. You also know that fields possess properties and that objects in forms have properties. In addition, objects you place in your reports also have properties. The exact properties available (and there can be dozens of them) depend on the object. Note: Many of the properties for report objects are the same as properties for other objects, such as fields. You may want to refer to Chapter 3, "Taking a Closer Look," for more information about some properties. In general, properties describe an object's attributes. For instance, an object's height, width, and position are all properties. So are things like colors and font. Many of these properties change as you simply place and work with the object.

There are two properties that are peculiar to objects on reports: Can Grow and Can Shrink properties. When you place a Text Box object in the detail section of a report, Access assumes you want the field or value assigned to the object to be printed. Access also assumes that you want the object to take the amount of space you set aside by the size of the Text Box. The Can Grow property tells Access to treat the Text Box differently. With this property set to Yes, the vertical (up and down) size of the Text Box will grow on a record-by-record basis, if necessary. This means that Access will not truncate the value in the Text Box. This is a great feature, particularly if you are printing long strings or the contents of a Memo field. The Can Shrink property works in the opposite manner. If there is nothing to print in the Text Box, Access will close up any vertical space it can. Setting this property to Yes is helpful in making your report look more professional--there won't be any glaring gaps in your report. Note: If you want to discover how you can use a particular property, display the Properties dialog box, select the property, and view the information in the status bar of the Access program window. You can also use the Access online Help system to find more information about properties.

Chapter 14: Creating Complex Reports
(This is section 3 of 4 in this chapter)

Designing Your Report
Assume you want to create a report that lists all the accounts in the Business Customers table you developed earlier in the book. To do this, you use the report Design window in much the same way as you would use a paint or drawing program, and in a very similar manner to how you created forms in Chapter 9, "Creating Custom Forms." You place an object, refine how it looks, and then place another object. You repeat this process until you achieve the desired effect.

Setting Up the Detail Section
To create your report, you first need to define the report's body. This may sound strange to some, since there are other parts of the report that Access prints before the body. The reason for this is quite simple, however. The position of information in the body of the report will dictate the position of information in headers. With that in mind, you need to first decide what you want Access to print in the report. For this report, you will use the following fields:

• • • • •

Company Name Address 1 Address 2 City State

• • •

Zip Code Main Phone Account Opened

You could have included other fields , but this report is for general information. The other fields are too specific for a general audience. Further, you may want to group the report by state. If you group your customer data by state, your sales representatives can quickly identify their regions' customers without having to sort or query the state. With this information in mind, you can start to place the fields on your report. You do this by using the mouse to select and drag a field from the Business Customers field list into the detail section of your report. For instance, click your mouse on the Company Name field, and then drag it onto the detail section of the report. When you release the mouse button, Access places two objects on the form: a Label and a Text Box. The Label contains the Caption property for the field, and the Text Box contains the name of the field itself, as shown in Figure 14-2.

Figure 14-2 Placing a field on the report. When you place the field, Access automatically selects both objects (the Label and the Text Box). Make sure that only the Label object is selected (when it is selected it will have the handles around it), and then press the DEL key. This gets rid of the Label object. This is a good idea since you don't want labels printed with every single record in the table. Instead, you will later want them to appear as column headers in the page header section of the report. After you delete the Label object, you should accomplish these tasks:

Position the Text Box object so it is near the top left corner of the detail section.

• •

Make the Text Box object wide enough that it can print the widest company name in your table. If your printer will support them and you want to use them, change colors or other attributes of the Text Box.

That's basically all there is to placing fields in your report. You place them, format them, and go on to the next one. Note: The placement and sizing of objects within your report is important. If an object such as a text box or a label is not the right size, the text within the object will not print fully. Make sure your objects are an appropriate size for what you want them to print. You are now ready to place the address on the report line. You could place individual fields for both Address 1 and Address 2, just as you did when you placed the company name, but that would take quite a bit of space on the line. Instead, you can use an expression to combine the two fields together and print them in a single text box. To create an expression, select the Text Box tool from the toolbox. Then place the Text Box in the detail section of your report. Notice that placing the Text Box automatically places a Label object, the same as when you drag-anddrop field names. Delete the Label and select the Text Box. Then, display the Properties dialog box for the Text Box, as shown in Figure 14-3.

Figure 14-3 Properties dialog box for a Text Box object. To combine the two address fields, you must change the Control Source property. This is the property that controls what Access displays in the Text Box. Normally, you would set this to the name of a particular field in your table. In this instance, you are going to use an expression. Select the property and enter the following: =[Address 1] & IIf(IsNull([Address 2]),"",", " & [Address 2]) This expression may look complicated, but it is not really. Access lets you create expressions based on field names (enclosed here within brackets) and functions. You will learn more about functions in later chapters, when you learn more about macros and Visual Basic for Applications. This particular expression simply tells Access that you want to display the contents of the Address 1 field and (if Address 2 is not empty) a comma, a space, and the contents of the Address 2 field. At this point, you can change the formatting of the Text Box in the same manner as you did earlier for the company name.

You can apply the same sort of idea to the display of the next part of the address: the city and state. Perform the steps you just finished to place the Text Box on the screen. Next, enter the following expression in the Control Source property: =[City] & ", " & [State] This expression is much shorter; it combines the contents of both fields. This is perfectly acceptable in this instance since you can feel reasonably sure that there will be a state listed for each of your customers. Place the text boxes for the remaining fields (Zip Code, Main Phone, and Account Opened). If you desire, you can set the Format property of the Account Opened field so it displays a fuller date on the report. Note: If you need a refresher about the Format property, refer to Chapter 9, "Creating Custom Forms." When you are through placing fields and adjusting them, you should have six of them on the screen. Adjust the details section grid so it does not take so much vertical room. Remember that Access uses the size of the grid area to determine how much room to leave for each record in the body of your report. As you placed the objects in the detail section, you should also have adjusted the width of the report to fit your needs. Figure 14-4 shows what your report Design window should look like.

Figure 14-4 The detail section of the report is complete.

Grouping Your Data
To finish out the body of your report, you still need to group your data by state. To group your data, choose Sorting and Grouping from the View menu, or click your mouse on the Sorting and Grouping tool on the toolbar. When you do, Access displays the Sorting and Grouping dialog box shown in Figure 14-5.

Figure 14-5 The Sorting and Grouping dialog box. Next, specify the field you want to use to control the grouping. Click your mouse on the pulldown arrow at the right side of the cell and select the State field from the drop-down list. When you do, Access displays a set of group properties at the bottom of the dialog box. These properties control how Access handles the group. If you accept the default properties, Access only sorts the report. You can, however, also instruct Access to include a group header or footer. To see how these work, change the Group Footer property to Yes and then close the Sorting and Grouping dialog box by clicking your mouse on the Close icon in the upper-right corner of the dialog box. You will notice there is a new section on the report, as shown in Figure 14-6.

Figure 14-6 After a group footer section has been added. The group footer section (or the group header section, for that matter) is just like any other header or footer. You use it to tell Access what you want printed at the end of every group. For instance, you could print a count of the number of customers in each state. To do this, add a

Text Box at the right side of the group footer section. In the Control Source property for the Text Box, you need to enter the following expression: =IIf(Count([State])=1,"There is 1 customer in this state","There are " & Count([State]) & " customers in this state") This may seem like a long expression, but it displays proper grammar (There is versus There are), depending on the number of customers in a state. With the entry of this expression, you are done with the body of your report.

Setting Up the Page Header
You already know that a page header contains information you want Access to print at the top of every page of your report. With this in mind, you should include information in the page header such as recurring headings, page numbers, and column headings. For your customer list, a page number and column headings will do fine. You can place most of these items using the Label object. Click your mouse on the Label tool (the one with the capital A in it). Next, start the label box directly above the Company Name field, near the lower left corner of the page header section. Make the label box wide enough to hold the column heading for the Company Name field; you can always resize the box later. At this point, the Design window will look like what you see in Figure 14-7.

Figure 14-7 After placing a Label object. Access is waiting for you to type the text that should appear in the label. Type the words Company Name and then press ENTER. The words should appear at the left side of the box and handles will appear around the box. You can now format the label to your liking. You may want to set each of these:

Click your mouse on the Bold tool so the text is printed bold.

• •

Change the font or point size of the text. Change the color or shading of the text (if your printer supports those features and you want to use them).

After you are through defining this label object, you can use the same technique to set the other label objects in the page header section. Place labels for each of the following columns:

• • • • •

Address City & State Zip Code Phone Number Customer Since

Don't forget to position the label boxes so they appear directly above the actual fields in the detail section. Figure 14-8 shows what your Design window should look like when you are done.

Figure 14-8 After adding the column heads. There is still something uninspiring about the page header. In order to liven things up a bit, you should place a line under the column headers. The line will serve to separate the headings from the body of the report. To create the line, you use the Line tool in the toolbox. Select the line tool, and then use the mouse to define the line. Hold down the mouse button at one end of the line and release it when you have moved the mouse point to the other end. After you place the line, you can use the tools on the toolbar to specify the line's thickness and type.

Using Other Objects
You have a pretty good idea of how the Label and Text Box objects will print on a report, but it might not be quite clear how the other objects in the toolbox will print. For example, you may not know how a check box or a toggle button will print.

When you print a report, Access is quite literal. If you place a check box on the report grid, Access prints it on the report exactly like you placed it. The difference between the use of these objects on a report and a form is simple. On a form, these objects let you review and change data. On a report, the objects show the current state of your data.

Setting Up the Page Footer
Finally, you should add a page number to the report. Although there are a number of places a page number can appear, the bottom right corner of every page is very acceptable for this report (this is in the page footer area). Put a line at the top of the page footer area, and position a Text Box at the bottom right corner of the page. (You can again delete the Label object that Access always places with a Text Box.) Within this text box, place the following expression in the Control Source property: = "Page " & [Page] & " of " & [Pages] This expression results in the current page number and the total page numbers printing on every page (Page 1 of 13, Page 2 of 13, etc.). You can format the text with any font or point size you want. You should also align the text so it is at the right side of the text box. To justify the text at the right edge of the Text Box object, click your mouse on the Align Right tool. At this point, your Design window should look like that shown in Figure 14-9.

Figure 14-9 The completed report.

Setting Up the Page
Even though your report is essentially done, you have one more minor item to complete. You probably noticed that as you set up your report, each record required a fair amount of horizontal space to fit everything. If you look at Figure 14-9, you can see that according to the ruler at the top of the report Design window, the last field ends at just short of 9.5 inches. That is too wide for a report that is printed in portrait mode; you need to instruct Access to print the report in landscape mode.

To change the orientation of your page, simply choose Page Setup from the File menu. This displays the Page Setup dialog box. Make sure the Page tab is selected, as shown in Figure 1410.

Figure 14-10 The Page Setup dialog box. Click your mouse on the Landscape option button to inform Access how you want the report printed. You should also adjust the other settings in the dialog box, particularly the margins, to make sure that your 9.5-inch wide report will print on the page with no problems. You can then change the other settings, if desired. When you are done, click your mouse on the OK button.

Viewing Your Report
To take a look at how your report is turning out, click your mouse on the View button on the toolbar. Access displays what your report looks like in a manner that is very similar to print preview. Figure 14-11 illustrates a sample of how this report will appear.

Figure 14-11 Reviewing the report. When you are done reviewing the sample report, click your mouse on the View tool again to return to the Design window. Many people, as they design reports, will switch back and forth between the preview and Design windows to see how their design is progressing. If you want to see what the report will look like when Access prints it, choose the File menu Print option. Specify a printer and other print specifications, as you learned to do in Chapter 8, "Creating Simple Reports." Access will send your report to your printer and will return you to the Design window. Figure 14-12 shows an example of the printed report.

Figure 14-12 A printout of the report's first page.

Saving Your Report
When you are done creating or changing a report, you need to save it. When you close the report Design window, Access will ask you if you want to save your report. The effect of clicking your mouse on the Yes button varies, depending on whether you are editing or creating a report. If you are editing, Access saves your changes under the same report name

as you used previously. If you are creating a report, Access will ask you to provide a report name, as shown in Figure 14-13.

Figure 14-13 The Save As dialog box. Type in a report name and select OK. Access will save the report, returning you to the Database window. Note: Access saves reports in your database in the same way that it saves other database objects. Reports are an integral part of the database; Access does not save them in a separate file. You can see what reports are available with a database by selecting the Reports button in the Database window.

Chapter 14: Creating Complex Reports
(This is section 4 of 4 in this chapter)

Summary
Access includes a built-in report designer that lets you easily and quickly define exactly what you want in a report. This chapter has taken you beyond the simple reports you can create with the Report Wizards. In this chapter, you have learned the full range of what Access can do when you create reports from scratch. Before you move on to Chapter 15, make sure you understand the following key concepts:

• • • • • • •

You can display the report Design window by selecting the Reports button in the Database window and then double-clicking your mouse on the Create Report in Design View option. You control the report Design window in much the same way as you control other Design windows in Access. It also shares many of the same features, such as a toolbox, field list, and toolbar tools. There are two general categories of sections in a report: detail and header/footer. In the header/footer category, there are three different types: report, page, and group. Objects you place in a report have properties which control how Access handles and displays them. You can sort and group data in your report by clicking your mouse on the Sorting and Grouping tool on the toolbar. To see how your report looks before you actually have it done, click your mouse on the View tool. Access automatically gives you the opportunity to save your report when you close the report Design window.

In Chapter 15, you will learn about getting information into Access (importing) and getting it out (exporting).

Chapter 15: Importing and Exporting Information
(This is section 1 of 3 in this chapter)

One of the marks of a good neighbor is a willingness to cooperate with those around you. Access is a good neighbor--using Access import and export capabilities, you can easily share information with virtually any other program you can think of. In this chapter, you will learn how to use those capabilities. By the time you finish this chapter, you will understand the following key concepts:

• • • • • •

What data importing is What types of files Access can import How to import a file What data exporting is What types of files Access can export How to export a database object

Using Information from Other Databases
There are many different database programs on the market, and it is not unreasonable to expect that some of these other programs have been used to accumulate quite a bit of data. If you have some of this older data, you may want to convert it to an Access format. Also, you may need to work with data stored in a different format by someone else in your company or by one of your clients. Regardless of the reason why you have data in a different format, Access will let you import information from a variety of formats into an Access database. Importing data is the process of combining information from an external source (such as a dBASE file) with information in the current database. To import data, you must already have an Access database open; this is where Access will place the imported data. You also need to know the format of the data you plan to import.

Picking a Source Format
To import data into an open database, choose Get External Data from the File menu. This displays a submenu from which you should choose the Import option. Access, in turn, displays the Import dialog box, as shown in Figure 15-1.

Figure 15-1 Picking a source file. The Import dialog box is very similar to the standard Open dialog box used by Access. Perhaps the most important feature, however, is the Files Of Type pull-down list at the bottom of the dialog box. Using this pull-down list, you can specify which types of files Access should show in the rest of the dialog box. Table 15-1 shows the various choices you can make with the pulldown list.

Option Microsoft Access Text Files Microsoft Excel Exchange Outlook Lotus 1-2-3 Lotus 1-23/DOS Paradox HTML Documents dBASE III dBASE IV dBASE 5 ODBC Databases

Meaning Source is another Access database Source is a text file Source is an Excel spreadsheet Source is a Microsoft Exchange address book Source is a Microsoft Outlook address book Source is a Lotus 1-2-3 file Source is a Lotus 1-2-3 file from a DOS version of the program Source is a Paradox database file Source is a Web page Source is a database created in dBASE III format Source is a database created in dBASE IV format Source is a database created in dBASE 5 format Source is any database that utilizes ODBC

Table 15-1 Import format selections supported by Access.

Select the option that corresponds to your source data. If you don't know, you may need to ask whoever gave you the data. After you select a source format, use the controls in the rest of the dialog box to select the file you want to convert. When you are ready to proceed, click your mouse on the Import button. The dialog boxes you see from this point forward will depend on the type of source file you are importing.

Importing Access Files
Access lets you import objects from another Access database. If you are importing an Access file, you will see the Import Objects dialog box, as shown in Figure 15-2.

Figure 15-2 Importing Access objects. You use this dialog box to specify which objects you want to import. As mentioned earlier, you can specify any type of database object you want. All you need to do is click on one of the tabs that represents the category of object you want to import. The dialog box then displays any objects of that category that are in the other database. You can then select the actual object to import. If you don't want to import an entire object, you can click on the Options button. Access expands the Import Objects dialog box, as shown in Figure 15-3.

Figure 15-3 Controlling what is imported. Using the controls in the bottom of the expanded dialog box, you can specify exactly how data should be imported to the current database. When you are ready to proceed, click your mouse on the OK button. Access copies the specified information to your database.

Importing Delimited Text Files
There are two types of text files you can import into Access: delimited and fixed-width. These file types differ only in how they separate fields. A delimited file has some special character, such as a tab or comma, between fields. A fixed-width file uses a specific number of characters for each field, often separating fields with spaces. This section covers how you import a delimited file; importing fixed-width files is covered in the following section. Note: If you are using a database program other than those which Access can import directly, you can probably save information from the database in some sort of text or ASCII format. If you can, you can easily import it into Access. Access imports text files using a Wizard. When you specify a text file as your import source, the Wizard is automatically started. Figure 15-4 shows the first dialog box used by the Wizard.

Figure 15-4 The Import Text Wizard. Access has determined that the file contains delimited text. All you need to do is click your mouse on the Next button. The Wizard displays the next step, as shown in Figure 15-5.

Figure 15-5 Specifying table information. The exact composition of any delimited text file depends on the program used to create the file. Some programs use tabs to separate fields, while others use a different character or sequence of characters. This dialog box allows you to specify exactly which character is used to separate the fields in your file. You can also indicate whether the first row of the file contains the names of the fields in the file. If this is the case, you should select the check box at the left side of the dialog box. When you are ready to proceed, click your mouse on the Next button. The Wizard displays the dialog box shown in Figure 15-6.

Figure 15-6 Where to store the information. Here you are asked to specify how you want Access to treat the imported information. The default is for Access to create a new table. Quite honestly, this is the best option to choose unless you are very sure of the quality of the data you are importing. Remember, you can always merge two tables together after you are sure the import worked properly. When you are satisfied with your selection, click your mouse on the Next button. The Wizard displays the dialog box shown in Figure 15-7.

Figure 15-7 Controlling individual fields. This dialog box allows you to control how Access imports each field from the source file. You can step through the fields and specify at the top of the dialog box how that particular field should be treated. You can even tell Access to ignore a particular field (select the check box) if you don't want to use it. Stepping through the individual fields and indicating what data types should be used and other characteristics may seem like a time-consuming task. It does take a while, but your data will be in much better shape after the import if you take the time to provide the specifications now. When you are satisfied with how the importing of fields is to be done, click your mouse on the Next button. The Wizard displays the next dialog box, shown in Figure 15-8.

Figure 15-8 Picking a key. Access gives you the opportunity to use keys in your data tables. You learned about keys in some detail in Chapter 13, "Understanding Data Relationships." This dialog box allows you to specify how a key should be handled for the import file. You have three choices, controlled by option buttons at the top of the dialog box:

• • •

Let Access decide. This option causes Access to add a key field that utilizes the AutoNumber data type. This is a common way to create key fields. Pick a field. This option allows you to specify which field from the import table should be considered a key field. Ignore the key. This option means that no key field is specified for the table.

When you have made your selection, click your mouse on the Next button. The Wizard displays the final dialog box, shown in Figure 15-9.

Figure 15-9 Indicating a table name to use. The major purpose of this dialog box is to provide a way for you to specify the name to be assigned to the table created by the Wizard. Indicate a table name, and then click on Finish to complete the importing. When the import is complete, Access informs you it is done. Note: At the bottom of the dialog box shown in Figure 15-9 is a check box that allows you to analyze your imported data table. This option, if selected, runs the table analysis feature described in Chapter 10, "Using Additional Tools." You may have noticed that each dialog box used by the Import Text Wizard included an Advanced button at the bottom. This button, if selected, allows you to set additional parameters which Access will use when importing the textual information. When you click your mouse on the Advanced button, the Wizard displays the dialog box shown in Figure 15-10.

Figure 15-10 The Specifications dialog box. You use this dialog box to set the complete specifications for importing a particular type of text file. You can see in Figure 15-10 that the dialog box contains all the specifications set by working through the previous steps in the Wizard. The value of setting specifications for a file type is evident if you routinely import a consistent type of text file created by a different application. You can save the specifications in a file (notice the Save As button), and then recall them as a group when you need to import the next file. In most instances, you will not need to change these import options.

Importing Fixed-Width Text Files
A fixed-width text file is one that uses spaces to separate columns. These types of files are common if you have a program that produces reports destined for an older monospace printer. (Monospace means that every character has the same width as every other character.) If you are importing a fixed-width text file, the process is very similar to when you import a delimited text file. The first Import Text Wizard dialog box you see is shown in Figure 15-11.

Figure 15-11 Importing a fixed-width text file. Access has figured out that the file is fixed-width in nature, and is asking for your concurrence. All you need to do is click on the Next button. The Wizard displays the dialog box shown in Figure 15-12.

Figure 15-12 Confirming column breaks. In this dialog box you are asked to confirm the column breaks that Access has figured out. The vertical lines in the bottom of the dialog box indicate column breaks. When the actual importing is done, Access considers each column an individual field in the resulting table. All you need to do is scroll through the table and make sure that the column breaks, as noted, are correct. You can move column marks by clicking on them with the mouse pointer and dragging them left or right. You can delete a column marker entirely by double-clicking your mouse on it. When you are satisfied with the position of the column marks, click your mouse on the Next button. The Wizard displays a dialog box asking you to indicate where the imported information should be stored. This is the same dialog box shown in Figure 15-6. From this point on, the Wizard is exactly the same as the Import Text Wizard for a delimited text file. You can refer to the previous section to continue the importing.

Importing Excel Files
When you choose to import an Excel spreadsheet file, and you have specified a file name, Access displays the Import Spreadsheet Wizard dialog box, as shown in Figure 15-13.

Figure 15-13 The Import Spreadsheet Wizard dialog box. All you need to do in this dialog box is indicate which worksheet or named range of cells you want to import to Access. The dialog box lists each available worksheet or range, and you can see what they contain by clicking your mouse on them. The contents appear at the bottom of the dialog box. When you have selected what you want to import, click your mouse on the Next button. The Wizard displays the dialog box shown in Figure 15-14.

Figure 15-14 The Import Spreadsheet Wizard dialog box. At the top of the dialog box, you can indicate whether the first row of the worksheet contains field names. Select or clear the check box, as appropriate, then click your mouse on the Next button. The Wizard displays the dialog box shown in Figure 15-15.

Figure 15-15 Where to store the information. Here you can indicate whether you want Access to create a new table with the imported information. For most worksheets, this will be the only option you can choose. The exception is when the worksheet you are importing has the same number of columns as there are fields in one of your existing Access tables. You can simply click your mouse on the Next button to proceed to the next dialog box, shown in Figure 15-16.

Figure 15-16 How to process fields. This dialog box allows you to control how Access imports each column from the worksheet. You can step through the columns--which correlate to fields in Access terminology--and specify at the top of the dialog box how that particular field should be treated. Simply click on a column, then change the settings at the top of the dialog box. You can even tell Access to ignore a particular field (select the check box) if you don't want to use it. When you are satisfied with how the importing of columns is to be done, click your mouse on the Next button. The Wizard displays the next dialog box, shown in Figure 15-17.

Figure 15-17 Picking a key. Access gives you the opportunity to use keys in your data tables. You use this dialog box to specify how a key should be created for the table Access is creating. You have three choices, controlled by option buttons at the top of the dialog box:

• • •

Let Access decide. This option causes Access to add a key field that utilizes the AutoNumber data type. This is a common way to create key fields. Pick a field. This option allows you to specify which column from the worksheet should be considered a key field. Ignore the key. This option means that no key field is specified for the table.

When you have made your selection, click your mouse on the Next button. The Wizard displays the final dialog box, shown in Figure 15-18.

Figure 15-18 Indicating a table name to use. The major purpose of this dialog box is to provide a way for you to specify the name to be assigned to the table created by the Wizard. Indicate a table name, and then click your mouse on Finish to complete the importing. When the import is completed, Access lets you know it is finished.

Importing Lotus Files
Lotus 1-2-3 is a popular spreadsheet program that is still widely used. Because Lotus 1-2-3 is a spreadsheet program, Access lets you import its files in much the same fashion as when you import Excel files. When you start to import a Lotus 1-2-3 file, the import Wizard asks you to specify what you want to import from the file. In the various dialog boxes used by the Wizard, you specify the information to import, indicate how it should be converted, instruct Access what to do about a key for the table, and then provide a table name. When you are done importing, you will be able to use the Lotus 1-2-3 data in Access, the same as you would any other database table.

Importing Paradox and dBASE Files
Importing these types of files is perhaps the easiest importing you can do. This is because after you select a file name and click on the Import button, Access imports the file right away-there are no other dialog boxes to work through. Access stores the file in a table with the same name as the file you are importing. Access then returns you to the file selection dialog box. You can select another file, or you can click on Close to return to the Database window.

Chapter 16: Ensuring Data Security
(This is section 1 of 7 in this chapter)

As you work with Access, over time you invest quite a bit into your databases. You develop forms, reports, and queries that allow you to manage your data just as you desire. These efforts take time. You validate your data and make sure that it is correct and that all redundancy is removed. These efforts take more time. In addition, the data itself takes time and money to acquire. The result is that at some time you are going to realize that your data has value--real value--to your company. At some point you are going to be faced with the necessity of safeguarding your database. Fortunately, Access includes some security features you can use to heighten the security of your data. In this chapter, you learn how to use those features. By the time you finish this chapter, you will understand the following key concepts:

• • • • • •

Different levels of security you can use in Access How to set a database password What it means to encrypt a database file How to configure user-level security How to work with accounts and permissions How to use the User-Level Security Wizard

Setting a Database Password
If your databases are used primarily in a single-user environment, then a good place to begin your security efforts is with a password for your database. By "single-user environment," I mean that your database is used by a single person at a time, not necessarily the same person. Thus you could work on it this morning, and a coworker could use it later in the afternoon, but you are not both using it at the same time. If, instead, there are multiple users working with your data at the same time, then you will need more powerful and flexible security measures, as covered later in the chapter in the section on users and groups. If you are developing a single-user database, then you should apply the ideas in this section. You can always increase security measures at a later time. Note: Password-protecting your database is also a good option if the database contains sensitive data. For instance, personnel pay records or your company's secret formulas. Setting a password for your database is analogous to adding a deadbolt to the front door of your house. The purpose is to keep unauthorized people from entering the house. Once the deadbolt has been bypassed, however, the full contents of the house are open to the intruder. It is the same way with a database password. The entire purpose of the password is to make it more difficult for someone to open your database. Once open, however, the data in the database is entirely open to the user. (Later in this chapter you will learn how you can actually encrypt your database, which is a much more secure method of protecting your data, instead of just protecting the front door.)

Because the database password opens all the data to a user, this means that the password is just as important as the key to the deadbolt on your house. You should not leave it lying about where others can see it and figure out what it is for. You should also be sure that you use a password that is at least moderately difficult for someone to guess.

Adding the Password
You can define a password for a database by first closing the database. Make sure you stay in Access, but close the database that you want protected. Then choose the Open option from the File menu. This displays the familiar Open dialog box, as shown in Figure 16-1.

Figure 16-1 Picking a database to protect. You should use the controls in the dialog box to select the file you want to protect. Simply click your mouse on the file so it is selected or the name appears in the File Name box; do not actually open it yet. Then click your mouse on the down-arrow to the right of the Open button. This displays a menu of four different ways you can open the database file you have specified. Select the Open Exclusive option. Access opens the database, as directed. So far, however, there is nothing different about the database. You can work with it as you would any Access database. You now need to inform Access of the password you want to use. Do this by selecting the Security option from the Tools menu. This displays a submenu from which you should choose Set Database Password. Access displays the Set Database Password dialog box, as shown in Figure 16-2.

Figure 16-2 Specifying a password. In the Password box, enter the password you want to use. Pay particular attention to the way you type the password, as uppercase and lowercase are considered important. You will also need to enter the same password again in the Verify box. When you are ready to proceed, click your mouse on the OK button. The password is now set. You can continue using the database as you normally would. When you are done working with the database, simply close it.

Using the Password
Once you have password-protected a database, that database cannot be opened unless you first supply the password. The password is stored with the database, so if you copy the database elsewhere or move it to a new location, the password is moved as well. When someone tries to open the database, Access displays the dialog box shown in Figure 16-3.

Figure 16-3 You must enter a password before opening a database. At this point, simply enter the password. This should be entered exactly as when first defined. Thus, if you entered the password of "MyPassword", you should not try to use "mypassword." Upper- and lowercase is significant in your password. If you enter the wrong password, Access displays a dialog box warning, as shown in Figure 16-4. You can then click your mouse on OK and try to enter the password again.

Figure 16-4 Access informs you if you use the wrong password.

Once you successfully enter your password, you can use the Access database as you normally would. If you close the database, you will again need to reenter the password before working with the database. It is particularly important to realize that if you forget the password you assigned to a database, you simply won't be able to use the database. Microsoft provides no method or utility that will help you gain access to a database for which you don't know or have forgotten the password. This has important ramifications in a corporate environment where an employee could password-protect a database, and then leave the company or become unavailable after an accident. If others may eventually need access to a database, it is best to write down passwords and store them in a secure place. Note: If you absolutely need access to a file for which the password has been lost, you may be able to find a third-party utility that will remove the password. Search the Internet for such utilities by using keywords such as "Access" and "password" and "removal."

Removing the Password
At some point you may wish to remove a password you assigned to a database. For instance, the data may no longer be considered sensitive, or you may want to remove the password and reassign a new one. To remove a password, you need to open the database for exclusive access. Close the database, and then choose Open from the File menu. This displays the Open dialog box, as shown earlier in Figure 16-1. Select the database whose password you want to remove, then click your mouse on the down-arrow to the right of the Open button. From the resulting menu, choose Open Exclusive. At this point you are asked to enter the password for the database. Enter it as you normally would, and Access opens the database, as expected. Now all you need to do is choose Security from the Tools menu, and then choose Unset Database Password from the resulting submenu. Access displays the dialog box shown in Figure 16-5.

Figure 16-5 You need the password in order to remove the password. Enter your password in the Password box, and click your mouse on the OK button. Access dismisses the dialog box, and your database is unprotected. Note: Anyone who has the password for a database can remove that password. Thus, if you share a database password with a coworker, both you and the coworker can remove the password.

Chapter 16: Ensuring Data Security
(This is section 2 of 7 in this chapter)

Using Database Encryption
One of the drawbacks of password protection (which you learned about earlier in this chapter) is that it protects only the front door of your database. If someone chooses to not use the front door, often they can uncover the data stored in your database. For instance, someone could load your database into a third-party utility or a word processing program and see the raw data stored there. One way around this problem is to use data encryption. This simply means that the information in a database is "scrambled" using a mathematical algorithm so that it cannot be understood by any application other than Access. This option is particularly useful if you are going to send the database to a different user, and you want to make sure it is secure during transmission or transport. Note: Encryption, by itself, does not stop someone from opening a database in Access. Thus, encryption does little good if it is used without some other form of protection, such as a database password. Make sure you use encryption with some other form of protection, as well. To encrypt a database, make sure you don't have any databases open. You cannot encrypt a database if it is open. Choose the Security option from the Tools menu. Access displays a submenu from which you should choose Encrypt/Decrypt Database. Access then displays the Encrypt/Decrypt Database dialog box, as shown in Figure 16-6.

Figure 16-6 Picking a database to encrypt. All you need to do is use the controls in the dialog box to select the database you want to encrypt, and then click your mouse on the OK button. Access then displays the Encrypt Database As dialog box, shown in Figure 16-7.

Figure 16-7 Specifying a name for the target file. Here you need to supply a name (and optionally a different location) for the encrypted database that Access is going to create. The original database is not affected; only the newly created file is encrypted. Once a name has been provided, clicking your mouse on the Save button starts the actual encryption process. When completed, you have two versions of the database: one encrypted and one not. From this point you should use one or the other, but not both. Remember that you can still use an encrypted database in Access, the same as you would use a regular database.

Chapter 16: Ensuring Data Security
(This is section 3 of 7 in this chapter)

Working with Users and Groups
Access includes an advanced form of user security that is based on essentially the same concepts used in protecting computer networks: users and groups. You can define users, organize those users into groups, and then assign different permissions to the groups.

Microsoft refers to this approach to security as user-level security. In reality, this type of security is built-in to Access, and turned on all the time. You may not realize it, of course, because everyone starts out as an Administrator, able to do anything in Access. When you fully implement user-level security, you are simply "demoting" certain users from their original Administrator status to a regular user status, and limiting what they can do in the databases to which they have access. When you use user-level security, you have complete control over who has access to not only databases, but also individual objects within a database. Users are required to supply both a user name and password to access information. The simplicity or complexity of the security system is left entirely up to you. Note: User-level security is typically set up on a company-wide level. If your company has standardized on Access for data management tasks, you may already have a userlevel security system in place. In this case, the information in the balance of this chapter will be of little use. If you want to better understand the security system you are using, speak with your network administrator.

Understanding Workgroup Information Files
In implementing user-level security, Access relies on workgroup information files. When you first install Access, a default workgroup information file is created on your system. Later, if you set up user-level security on the system, you can modify the default workgroup information file or create a new workgroup information file for a particular database. The file indicates which users and groups have which permissions for a particular database and its objects. Changes to a workgroup information file are done using the Workgroup Administrator program. A workgroup information file contains the following predefined accounts:

• • •

Admin. This is the default user account, and is exactly the same for every copy of Access. Admins. This is a group account whose members are allowed to administer the Access database. There must be at least one user account in the Admins group at all times. When first created, the Admins group contains only a single user account, which is the Admin account. Users. This is a group account whose members are allowed to use the Access database. When a new user account is created by an administrator, that user is added to the Users group automatically.

You can add additional user accounts or group accounts to any workgroup information file, provided you have Administer permissions for the database. Because the Admin account is available in every copy of Access, the first step in securing any database is to create additional administrator accounts and then remove the Admin account from the Admins group. In this way, anyone using the Admin account (any plain-vanilla Access user) will belong to the Users group instead of the Admins group.

Understanding Permissions
Access to specific databases and the objects in a database are granted based on what permissions have been granted to a user. There are two types of permissions understood by Access: explicit and implicit permissions. Explicit permissions are those granted directly to a user account, while implicit permissions are those inherited by a user account based on its membership in a particular group.

The rights which a user enjoys are defined by the least restrictive of a user's explicit and implicit permissions. Thus, if a user is permitted to view a particular database object by virtue of belonging to a particular group, but an explicit permission granted to the user allows him or her to modify that object, then the modification permission is least restrictive, and the one that is enjoyed by the user. For most purposes, it is best to stick strictly with implicit permissions. These are the most flexible type of permissions, and the easiest to administer. For instance, if you have user groups set up along company organizational lines, you may have different groups for sales and marketing. When a person changes jobs at the company and moves from sales to marketing, it is a simple matter to move their user account from one group to the other. The account then inherits the permissions of the group of which it is a member. If you instead used explicit permissions, you would need to modify the individual permissions granted to the user when the switch was made. There are a number of different permissions which can be used in Access. The exact permissions which are available depend on the object in question. Some permissions are applicable to databases as a whole, while others are applicable only to individual objects within a database. Table 16-1 details the different permissions you can use in Access.

Permission Open/Run Open Exclusive Read Design Modify Design Administer Read Data Update Data Insert Data Delete Data

Meaning Allows user to open an object Allows user to open an object for exclusive access Allows user to look at objects in Design view Allows user to look at and change objects in Design view Allows user complete control over an object Allows user to look at data Allows user to look at and change existing data, but not add or remove records Allows user to look at existing data and add new records Allows user to look at existing data and remove records, but not change existing data or add records
Table 16-1 Permissions in Access.

Applicable Objects Databases, forms, reports, macros Databases Tables, queries, forms, reports, macros Tables, queries, forms, reports, macros Databases, tables, queries, forms, reports, macros Tables, queries Tables, queries Tables, queries Tables, queries

A member of the Admins group for a particular database can change permissions for any object in that database. In addition, permissions for a particular object can be changed by the owner of that object (generally the person that created the object) or by anyone who has Administer permission for the object.

Chapter 16: Ensuring Data Security

(This is section 4 of 7 in this chapter)

Using the User-Level Security Wizard
As you can probably imagine, setting up user-level security can be an overwhelming task. Fortunately, Access includes a User-Level Security Wizard which can pay attention to the details for you and get you started. The Wizard implements a rather simple security system, based on only two groups: Administrators and Users. For many implementations of Access, particularly in small companies, this may be more than adequate. If you have more complex security needs, then using the User-Level Security Wizard can provide a good starting point for your security efforts. To start the Wizard, make sure you have opened the database you want secured. Then choose the Security option from the Tools menu. On the resulting submenu, choose the User-Level Security Wizard option. The Wizard begins, displaying the first dialog box, shown in Figure 168.

Figure 16-8 The Security Wizard dialog box. Here you are asked if you want to create a new workgroup information file for your database. Typically, this is a good idea. In fact, if this is the first time you are using the User-Level Security Wizard, it may be the only option you can choose (see Figure 16-8). Click your mouse on the Next button to proceed to the next step, as shown in Figure 16-9.

Figure 16-9 Specifying a workgroup ID. As you will learn later in this chapter, when you create a workgroup information file, you must assign it a unique workgroup ID (WID). The WID must be between 4 and 20 characters in length, and you can set it to any value you desire. When you use the User-Level Security Wizard, Access can create a WID for you automatically, as you can see in Figure 16-9. This dialog box gives you the opportunity to change the WID if you so desire. For now, you should accept the default WID. Later in this chapter you will learn how to create workgroup information files for yourself, and the ramifications of selecting a WID. You can also specify in this dialog box other information about how the workgroup information file should be created and how Access should treat it. If you want, you can specify the new file to be the default workgroup information file (not a particularly good idea), or you can create a desktop shortcut to open this secured database (this is quite handy). To continue, click your mouse on the Next button. Access displays the dialog box shown in Figure 16-10.

Figure 16-10 Picking objects to be secured. When you run the User-Level Security Wizard, it assumes you want to secure all the objects in the database you had open when you ran the Wizard. The purpose of this dialog box is to give you the opportunity to specify which objects you don't want secured. You should use the tabs in the dialog box, along with the check boxes beside each database object, to indicate any objects you don't want secured. Unless you have a compelling reason to not secure an object, it is generally a good idea to leave them all secured. If you need to change the permissions for a specific object at a later time, you can do so using the information provided later in this chapter. Click your mouse on the Next button to proceed to the next step of the Wizard. Access displays the dialog box shown in Figure 16-11.

Figure 16-11 Creating optional user groups. Earlier in this chapter you learned a bit about user groups. The User-Level Security Wizard allows you to easily create additional user groups that have different permissions to use your database. This dialog box allows you to specify which additional groups are to be created. Remember that the Wizard automatically creates both an Admins group and a Users group, besides those you select here. You can select which ever groups you feel compelled to use. If you do not envision assigning any users to a particular group, then it makes no sense to create that group. Make your selections and click your mouse on the Next button to go to the next step. Access displays the dialog box shown in Figure 16-12.

Figure 16-12 Assigning permissions to the Users group. The default Users group created by the Wizard has a certain number of permissions granted to it. You can modify which permissions the Wizard assigns by using the dialog box shown in Figure 16-12. Click your mouse on the Yes option button to change the permissions, and then use the tabs and the check boxes to make your changes. (You may want to refer back to Table 16-1 to refresh yourself on what each permission does.) When you are done, click your mouse on the Next button to proceed to the next step. Access displays the dialog box shown in Figure 16-13.

Figure 16-13 Creating users in the workgroup information file. Using this dialog box you can identify the names of the individual users you want created for your workgroup information file. By default, you are created as a user, but no one else is identified. If you know the names of the people that will be working on this particular database file, create user accounts for each of them here. All you need to do is enter the user name in the User Name box, and supply a password in the Password box. It is best to let Access generate the PID (personal ID) for each user you define, although you can override the PID with one of your own choosing. Another task you should do is to select your own user account in the dialog box and change the password for that account. The default is for the Wizard to set up your account without a password, but good security practice dictates that you set up a password. The reason is quite simple. Anyone who does a little bit of study of how Access handles security (for instance, if they read this chapter), understands that the administrator's account is set initially to be the name of the person that runs this Wizard. (In Figure 16-13 you can see that this is me, Allen.) Someone could figure out my name and simply provide that name when they log in to Access. Without a password, there is nothing to stop them from doing this. Not setting a password for this account would be a huge hole in your security plans. It is good to remember that you can always add users to the workgroup information file at a later time, if desired. Once all your users are defined and you have set your own password, click your mouse on the Next button to proceed. Access displays the dialog box shown in Figure 16-14.

Figure 16-14 Assigning users to groups. Now you get to assign users to specific groups. If you did not create any optional user groups, this step is very easy. By default, users belong to the Users group, so the only thing you need to do is pick which other groups they belong to. The easiest way to do this is to choose the Select a Group and Assign Users to the Group option button. Then you can use the pull-down list to pick each of your groups in turn. The check boxes in the body of the dialog box are then used to signify which users belong to each of the groups. When you are done, click your mouse on the Next button. Access displays the final step of the Wizard, as shown in Figure 16-15.

Figure 16-15 Naming your backup file. In this step you simply indicate the name you want to use for your backup database file. The backup is created, of course, in case you want to delete the new secure database and start over with the unsecured version. (It also comes in handy in case the power goes out while the Wizard is applying your security settings to the database.) In most cases you can simply select the default backup file name. When you click your mouse on the Finish button, Access secures the database. Access also creates a report that shows what security was applied to the database. Access displays this in a print preview window, as shown in Figure 16-16.

Figure 16-16 The Wizard's security report. It is interesting that the name of the report is "One-step Security Wizard Report." Not only is this not the name of the Wizard (it is the User-Level Security Wizard), but there was more than one step involved in using the Wizard. Regardless, the information in the report is very beneficial. Thus, it is a good idea to print the report and file it in a secure location. That way you always know how security was applied to the database.

Chapter 16: Ensuring Data Security
(This is section 5 of 7 in this chapter)

Opening a Secured Database
Once you have applied user-level security to a database, the database can only be opened in a very specific way. The database is still available from the Open dialog box, but it cannot be accessed from that dialog box. If you try to do so, Access displays an informational dialog box, as shown in Figure 16-17.

Figure 16-17 You cannot access a secured database directly. Instead, you must access the database using the shortcut the Wizard created on your desktop. When you double-click your mouse on the shortcut, Access is started and the proper Logon dialog box is displayed (see Figure 16-18). All you need to do is provide your name (if it is different than what is shown) and your password. You can then click your mouse on the OK button and access the database according to the permission levels granted you.

Figure 16-18 Logging in to a secure database.

Chapter 16: Ensuring Data Security
(This is section 6 of 7 in this chapter)

Managing Security Accounts
As you are working with the user-level security features of Access, there will be many times when you will need to perform management tasks to fine-tune the security. The following sections describe many of the security-related tasks you can perform.

Creating a New Workgroup Information File
There may be times when you want to create a new workgroup information file for use in Access. For instance, you might want to create one from scratch that will be used in a database you are creating in the future. When you installed Access, a special program was also installed that allows you to create workgroup information files. This is called, oddly enough, the Workgroup Administrator program. To run the program, follow these steps:
1. 2. 3. Exit Access completely. Locate the file wrkgadm.exe on your hard drive. You may need to use the Find feature of Windows to locate the file. Run the program. Figure 16-19 shows what the program interface looks like.

Figure 16-19 Creating a workgroup information file from scratch. To create your new workgroup information file, click your mouse on the Create button. Workgroup Administrator asks you for three pieces of information:

• • •

Name. Your name. Organization. Your company name. Workgroup ID. This is the WID discussed earlier in the chapter. This can be any unique string, between 4 and 20 characters in length. You need to take care that you remember what WID you assign to a particular workgroup information file, as it is needed to do some administrative tasks. It is your knowledge of what the WID is that identifies you as an authorized person to perform some tasks.

When you have supplied all this information, click your mouse on the OK button. Workgroup Administrator asks you to specify where the file should be created and under what name. You can indicate any location desired, although the Workgroup Administrator suggests the location of the other workgroup information files on your system. When you click your mouse on the OK button, Workgroup Administrator displays the information you entered (see Figure 16-20) and asks you to confirm it.

Figure 16-20 Confirming your workgroup information file specifications.

When you click your mouse on the OK button, Workgroup Administrator creates the requested workgroup information file and informs you it is done. When you are done with the program, simply click your mouse on the Exit button.

Adding an Account
You can add an account to a database that has been secured relatively easily, provided you logged on to the database using an administrator account. All you need to then do is choose Security from the Tools menu and then choose User and Group Accounts from the resulting submenu. Access displays the User and Group Accounts dialog box, as shown in Figure 16-21.

Figure 16-21 The User and Group Accounts dialog box. To add a new account, click your mouse on the New button. Access displays the New User/Group dialog box, which asks for two pieces of information:

• •

Name. The name of the new user account you are adding. Personal ID. This is a unique identification string, between 4 and 20 characters in length. It serves to internally identify the account to Access.

When you have supplied the information, click your mouse on the OK button. Access adds the user account, as directed. Adding a new group account is very similar. All you need to do is click your mouse on the Groups tab in the User and Group Accounts dialog box (Figure 16-21), and then click on the New button. The same two pieces of information are requested for new group accounts as are

requested for new user accounts. Supply the information and click your mouse on OK to add the account.

Removing an Account
To remove an existing user account, you start from the same User and Group Accounts dialog box shown in Figure 16-21. Using the pull-down Name list, select the account you want to delete. Then click your mouse on the Delete button. Access asks you to confirm your deletion. When you click your mouse on the Yes button, the account is removed. The only difference in removing a group account is that you must first click your mouse on the Groups tab. Otherwise, all the steps are the same.

Changing Account Permissions
As you learned earlier in this chapter, you can assign permissions to specific objects to either individual users (explicit permissions) or to groups (implicit permissions for the users in that group). To change permissions, all you need to do is log an as an administrator and choose the Security option from the Tools menu. On the resulting submenu, choose User and Group Permissions. Access displays the User and Group Permissions dialog box, as shown in Figure 16-22.

Figure 16-22 The User and Group Permissions dialog box.

If you want to specify explicit permissions for an individual user, all you need to do is select the user in the left side of the dialog box and the desired object in the right side. Then you can modify the permissions at the bottom of the dialog box. If you instead want to work with implicit permissions, click your mouse on the Groups option button in the middle of the dialog box. Then, the left side lists the groups you have defined. Pick a group, pick an object, and set the permissions at the bottom of the dialog box. Changes in permissions take effect immediately after you click your mouse on the OK button.

Printing a Security Overview
After you make any major changes in the security configuration for a database, you will want to print out a list of users and groups. All you need to then do is choose Security from the Tools menu and then choose User and Group Accounts from the resulting submenu. Access displays the User and Group Accounts dialog box, as shown earlier in Figure 16-21. You can then click your mouse on the Print Users and Groups button. Access responds by displaying the Print Security dialog box, as shown in Figure 16-23.

Figure 16-23 The Print Security dialog box. All you need to do is specify whether you want to print a report on users, groups, or both. When you have made your selection, click your mouse on OK. Access prints the requested report directly to your default printer. (It is interesting that Access doesn't first display the report in a print preview window, as it does with all other reports.) The report produced by Access shows a list of users with the names of the groups to which they are assigned. It then lists the names of the groups, along with all the users for that group. Unfortunately, the report does not show what permissions each user has. For that information, you are strictly limited to working on-screen.

Chapter 16: Ensuring Data Security
(This is section 7 of 7 in this chapter)

Summary
Data security is a huge topic, and of pre-eminent concern in many organizations. Access includes a full-featured security system that you can use to make sure your data is as secure as desired. This chapter has covered the highlights of ensuring the security of your data. If you are interested in more information, you may want to refer to a specialized book on the topic.

Before moving on to Chapter 17, make sure you understand the following key concepts:

• • • • • •

You can protect a database file by assigning a password to the file. This protects the file from unauthorized access. Database encryption can protect a file from being viewed with third-party tools, but is of little use without password-protecting the file. User-level security is implemented through the use of user and group accounts, workgroup information files, and permissions. Access provides a User-Level Security Wizard that can help you quickly apply user-level security to a database. If you have administrator privileges for a database, you can quickly and easily add or remove user and group accounts. You can change permissions for different users and groups on an object-by-object basis for a database.

In Chapter 17, you will learn how to how to use object linking and embedding (OLE) to place graphics, audio clips, and even videos within your database.

Chapter 17: Using OLE
(This is section 1 of 5 in this chapter)

In Chapter 15, "Importing and Exporting Information," you learned how Access can be a "good neighbor" by sharing data with other programs. In this chapter, you will learn how Access can work with other programs in a different way--by letting you link or embed information within a table. In short, this chapter teaches you how to take advantage of OLE, object linking and embedding, from within Access. By the time you finish this chapter, you will understand the following key concepts:

• • • • •

What OLE is and how it works What types of objects you can use with Access How to insert objects in your table How to modify links between Access and other programs How to edit objects you have linked to Access

What is OLE?
OLE (sometimes pronounced o-lay) is an acronym for object linking and embedding. When you use this technology in a program such as Access, you can combine information from different applications into your tables. For instance, you can combine text from a word processor (such as Word), spreadsheets or graphs from Excel, and graphics, sound, or video from a wide variety of sources--all within a single database. There are actually two parts to OLE. The first is object linking and the second is object embedding. Object linking means that you can establish a dynamic connection or link between Access and another program. The magic of this link is that Windows ensures that any changes to the linked data are updated automatically within your Access database. For example, if you

link an Excel chart into your table and later modify the spreadsheet data (from within Excel) on which the chart is based, the linked chart within your Access table automatically changes to reflect the modifications. If you had linked the chart to seven different tables, each table will automatically show the change. Object embedding means you can insert information created by another application into your Access tables. Object embedding is different from object linking in two ways:

• •

When information is embedded, it consists of both the representation of the information (perhaps a chart) and the data that makes up the information (for example, spreadsheet values used to create the chart). Embedded objects are not updated dynamically, but you may update them manually. You update an embedded object by double-clicking your mouse on the object with your mouse. Doubleclicking your mouse on the object activates it and launches the application used to create it. You can then change the information and update the document in which the object is embedded. For example, if you embed an Excel chart within your table, double-clicking your mouse on the chart will cause Excel to run with the spreadsheet data preloaded. When you exit Excel, the chart will reflect any changes you made.

As you work with OLE, there are a number of terms you will come across. Some of them may seem quite foreign, but don't let that confuse you. As with any technology, there is always a series of special words you must understand. With OLE, there are three particular terms whose meaning you need to know:

• • •

Client. A program that lets you link or embed information from other programs. Server. A program that lets you create information that can be used by a client. Object. Information that is exchanged between a server and a client.

As you examined the OLE terms, you may have gotten the idea that there are programs that provide information (servers) and those that receive it (clients). This is true, although some Windows programs have the capability to act both as a server and as a client. The extent to which a program implements OLE is up to the program's developers. Thus, some of your programs may be able to function as an OLE server, but not a client; some may not support OLE at all. Access has the capability to act as an OLE client. This means you can take information from other OLE servers, such as Excel, and insert it in your Access tables.

What Are Objects?
Notice that the definition of an object is fairly general. An object is information an OLE server program exchanges with OLE clients. The object definition is general on purpose, since objects really are nothing but information. Examples of objects include pictures, video, sound, text, images, or virtually anything else you can think of. Don't confuse OLE objects with database objects. Throughout this book, you have learned about database objects and how to use them. Database objects include tables, queries, forms, reports, macros, data access pages, and modules. These are not OLE objects, however. In the Access environment, a program other than Access creates the OLE objects. Access simply stores the OLE object in a table.

Types of Objects Available

OLE is a programming specification, and any program that adheres to the specification can take advantage of OLE technology. This means that there is not a finite number of programs that implement OLE--any program can do it. The program's developers define the program's OLE capabilities. A possible problem with OLE is the fact that not everyone has the same programs installed on their systems. For instance, there is a pretty good chance you have Access (most readers of this book will), and you probably have Excel and Word (since you probably have the Microsoft Office 2000 package). But that is about it. No other assumptions can be made about your system and what is on it. So how do you find out what objects are available to you? It is quite easy, really. When you install a program under Windows, the program lets Windows know whether it can support OLE and to what extent. Windows stores this information in a registration database and uses it when you want to insert an object in your table. For example, when you are creating a form, you can choose the Object option from the Insert menu. When you do, Access displays the Insert Object dialog box shown in Figure 17-1.

Figure 17-1 The Insert Object dialog box. The programs listed in the dialog box are those which registered with Windows as OLE servers. (Remember that your program list will probably be different from the one shown here.) This means that any of these programs can create objects (information) which you can link with or embed in your table. Later in this chapter, you will learn more about how to insert objects in your tables.

Chapter 17: Using OLE
(This is section 2 of 5 in this chapter)

Putting Objects in Your Table

To place OLE objects in one of your Access tables, you need to define a field to hold the object. To do this, Access provides a special data type called OLE Object. You may remember this data type from Chapter 2, "Access in an Hour." This is the only data type you can use to store OLE objects. As an example of how to use this data type, assume you are going to set up a database to keep track of your graphical images. You have created a database, which has the layout shown in Figure 17-2.

Figure 17-2 Layout of the Images database. The last field in this layout, Image, is the OLE field. Notice that the field's data type is OLE Object. This field can contain any object linked to or embedded from an OLE server. Note: The companion CD-ROM included with this book includes a file called Images 1.mdb. You can copy this database file to your hard drive (use the name Images.mdb) for the purposes of this chapter, if you would like.

Inserting an Existing Object
There is a form in the Images database called Review Images. If you open this form, you will see that it lets you leaf through a small, image collection. If you want to add an image to this collection, simply move to the last record. The form should appear as shown in Figure 17-3.

Figure 17-3 Ready to input a record. Click your mouse on the image area and then choose the Object option from the Insert menu. Access displays the Insert Object dialog box shown earlier in Figure 17-1. This is the generalpurpose object insertion dialog box. Since you want to insert an object from an existing file, click your mouse on the Create from File option at the left side of the dialog box. Notice that quite a few options change; the dialog box now looks like that shown in Figure 17-4.

Figure 17-4 Inserting an existing file. Access uses the dialog box to ask you for the name of the file you want to insert. Type in the name of a graphic file, or click your mouse on the Browse button to find the file on your disk. Make sure that the file you provide has a program associated with it that can function as an OLE server. Remember, Access will let you insert objects only if they were created or manipulated with an OLE server. For example, when I installed Microsoft Office 2000 on my system, Internet Explorer 5 was automatically installed at the same time. IE5 can be used as an OLE server. This program can be used to view many of the common types of files found on the Internet, including many graphics files. When IE5 was installed on my system, it registered the common file types with Windows so that the operating system knew which program could be used to load and view those types of files. Since IE5 is an OLE server, I can insert images it can display into Access. Having found the file I want to insert as an object, all I need to do is click my mouse on the OK button. Windows takes care of the rest; it recognizes that the graphics file I am inserting is one of the types handled by IE5. It therefore inserts an IE5 icon in my database to show that there is an active OLE link between the image and the program Windows thinks should handle the image. This icon is shown in Figure 17-5.

Figure 17-5 After inserting an image. With the image inserted, the other fields in the database record can be modified to indicate the proper information for the image. When you are done, you can move on to another record or close the form window entirely.

Inserting a New Object
Inserting a new object comes in handy if you are creating objects on the fly. One practical use for objects is as sound annotation for your database. If you have a sound card with a microphone hooked up, you can add "verbal comments" (annotations) to your records. Such annotations could come in handy when you later want to review what you thought about a particular item. In addition, verbal comments are often more effective than those typed into a field. This is because you can use your voice to express emotion; it is impossible to do that effectively with the written word. Figure 17-6 shows an example of how a sound annotation can be added to a form. Notice that the sound shows up as a regular sound icon in the small OLE object field. (Windows doesn't do a great job of centering icons in the available space, so the icon looks a bit off-center.)

Figure 17-6 The Images file with sound annotation added. To add such a field, all you need to do is add another field to the table with the OLE Object data type. Then, modify the form design to position the field in the proper place. When you later use the form, all you need to do is select the sound annotation field and choose the Object option from the Insert option. Access will display the Insert Object dialog box; you should scroll though the Object Type list until you select the Wave Sound object, as shown in Figure 17-7.

Figure 17-7 The Insert Object dialog box. When you click your mouse on the OK button, Access starts the OLE server (the Sound Recorder). You can then record your comments. When you close the recorder, Access saves your comments as part of the Images database.

Creating an Editable Link
You also can use the Windows Clipboard to insert a linked image into your database. Assume you are working with an Excel worksheet, and you create a chart (see Figure 17-8). You are so impressed with the chart that you decide you want to save it for posterity in your Image database.

Figure 17-8 The Excel worksheet and chart. While you could establish an OLE link with the chart as you have done previously in this chapter, you can also use the Clipboard to save the chart. To do so, perform these steps:
1. Use your mouse to select the Excel chart. Choose the Copy option from the Edit menu, or press CTRL+C to copy the chart to the Clipboard. Switch to the window containing Access. Display the record where you want to save the chart. Select the image area in the record. Choose the Paste Special option from the Edit menu. Access displays the Paste Special dialog box, shown in Figure 17-9.

2.
3. 4. 5.

Figure 17-9 The Paste Special dialog box.
1. 2. Choose the Paste Link option, on the left side of the dialog box. Click your mouse on OK. Access pastes the image into the database, as shown in Figure 17-10.

Figure 17-10 The pasted image. Depending on the application you are copying information from, the Paste Special dialog box can contain many different formats in which you can save your image. When the original image comes from an OLE server program, at least one of the formats will always be the name of the OLE server program. To establish the best relationship between the server and Access (the client), you should pick the link option and the OLE server program format. Later in this chapter, you will learn how you can edit the links you set up in this section.

Changing Object Icons
You may have noticed that when you start dealing with OLE links, Access can be very slow to refresh your screen. Access slows down because of the amount of information that it must process to display each screen. This slow down is particularly evident when you are working with large graphic files. To minimize this condition, you can tell Access to display your objects as icons instead of as actual images. Displaying an object as an icon will speed up how fast Access can display information. It also will make your database less visual, meaning the object's purpose won't always be readily

apparent. To use icons instead of actual images, take another look at the Insert Object dialog box shown in Figure 17-11.

Figure 17-11 The Insert Object dialog box. Notice the Display As Icon option on the right side of the dialog box. If you pick one of the object types and then select the Display As Icon option, you will notice a change in the dialog box, as shown in Figure 17-12.

Figure 17-12 After selecting the Display As Icon option. If you select a different object type, the icon that Access will use for an object will change to represent the different source. You can also change the icon by clicking your mouse on the Change Icon button. When you do, Access displays the Change Icon dialog box, as shown in Figure 17-13.

Figure 17-13 The Change Icon dialog box. The number of different icons available varies from program to program; there is no standard amount. For instance, this program file (for Paintbrush) has many different icons available. You can select the icon that suits you best. Note: If you don't like the icons available in the current program file, click your mouse on the Browse button to look through other files for icons. You might try looking through the file MORICONS.DLL in the Windows subdirectory. It contains quite a few different icons. When you are done picking an icon, click your mouse on the OK button. Access returns you to the Insert Object dialog box, where you can complete the object insertion. Click your mouse on OK from the Insert Object dialog box to insert the object in the database. In this case, Access only displays an icon, as shown in Figure 17-14.

Figure 17-14 Icon displayed instead of an image.

Chapter 17: Using OLE
(This is section 3 of 5 in this chapter)

Modifying Links
There are three different ways you can insert an OLE object into an Access database:

• • •

As an editable link As an active link As an object with no link (an embedded object)

So far you have learned about each of these ways, but you may not know how to change the status of the links. The following sections will teach you this information.

Editable Links
To change the status of an editable link, select the OLE object and then choose the OLE/DDE Links option from the Edit menu. When you do, Access displays the Links dialog box, as shown in Figure 17-15.

Figure 17-15 The Links dialog box. Note: If Access lists no links for your object in the Edit Links dialog box, your object probably has only an active link. The following section discusses how you can change the status of an active link. To modify an editable link, use your mouse to select the link in the Links list box. You can then edit the link in any of these ways:

• • • • •

At the bottom of the dialog box, change how the link is updated (from Automatic to Manual or back again). Click your mouse on the Update Now button to force an immediate update. Click your mouse on Open Source to pull up the program that created the object. You can then change the object and resave it in Access. Click your mouse on Change Source to pick a different object for linking in this field. This is done with a Change Source dialog box, which is very similar to the familiar Open File dialog box. Click your mouse on Break Link to change the object status. Once a link is broken, it cannot be replaced. The image no longer has a link and is just a picture in the database.

When you are done modifying your link, click your mouse on the OK button to return to the database.

Active Links
The only thing you can do with an active link is to break it. To break a link, select the Edit menu Object option. Note: The Object option is the last item on the Edit menu. Its full name will typically be something else, such as "Hijaak Image Object" or "Spreadsheet Object."

When you select this option, Access displays a submenu. The options available on the submenu will depend on the type of object you have selected. There will typically be an Edit option (which calls up the linked OLE server) and a Convert to Picture option. Regardless of the type of object you have selected, you use this last option to break any links between the object and the OLE server. After you choose this option, you will not be able to edit or change anything about the object.

No Links
To place an object with no links, use the Clipboard and then choose the Edit menu Paste Special option. In the Paste Special dialog box, choose the Paste option. Effectively, the object becomes a regular image, and, as such, it cannot be edited. If you try to select the OLE/DDE Links option from the Edit menu, Access will not allow you to since there are no existing links.

Chapter 17: Using OLE
(This is section 4 of 5 in this chapter)

Editing Objects
Under OLE technology, you can edit an object that is linked to your Access table in any one of three ways:

• • •

Double-click your mouse on the object. Choose the Edit menu Object option, then choose Edit from the submenu. Click your mouse on the Open Source button in the Edit Links dialog box.

Obviously, the first option of double-clicking your mouse on the object is the easiest. For example, if you wanted to edit the spreadsheet chart you placed in the database earlier, all you need to do is double-click your mouse on it in the Access database. Excel will then start and display the original spreadsheet and chart, as shown in Figure 17-16.

Figure 17-16 Editing an OLE object. When you finish making changes in the data or in the chart, exit the program. Both the original data and the chart in your database will be updated. Note: You cannot edit an object unless there is at least an active link between the object and a program that can manipulate the object.

Chapter 17: Using OLE
(This is section 5 of 5 in this chapter)

Summary
OLE lets you take advantage of the strengths of other programs. Using OLE, you can manage the data (objects) from those programs through Access. In this chapter, you have learned what OLE is and how it is implemented in Access.

Before you move on to Chapter 18, make sure you understand the following key concepts:

• • • • • • •

OLE is an acronym for object linking and embedding. It is a technology that permits you to dynamically insert information from other programs into your Access tables. Access is an OLE client, meaning it accepts information prepared by OLE servers. The information that Access accepts is an object. There are literally dozens of different programs which can be OLE servers. This means there are just as many different types of objects you can embed or link to your tables. You can place objects in any Access field which is set to the OLE Object data type. You insert objects in Access using the Edit menu Insert Object option. You can also insert objects using the Clipboard. You can use the OLE/DDE Links option from the Edit menu to modify links between objects in your table and a server program. You can edit linked objects by double-clicking your mouse on the object field. This starts the server program (if necessary) and lets you make any changes you desire. When you are done, the original information and the information in the Access table are both updated.

In Chapter 18, you will learn how you can use macros to automate many of your routine Access tasks.

Chapter 18: Creating and Using Macros
(This is section 1 of 6 in this chapter)

As you work more with Access, you will discover many ways you can tailor your Access environment to your needs. One of the ways Access lets you simplify tasks is with macros. In short, a macro is a recording of the steps you perform to accomplish a specific task. For example, to print a table, you select the File menu Print option and then choose OK within the Print dialog box. Using an Access macro, you record these steps. Later, you can play back your macro and Access will automatically perform the steps for you. As you will learn, you can write macros that perform simple or complex tasks. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • •

What macros are and how you can use them What actions and arguments are How to create macros How to use the Macro window What it means to create conditional macros How you can execute a macro How to link a macro to a command button on a form How to edit existing macros

What Are Macros?

You have probably heard the term "macro" before. In general, a macro is nothing but a series of commands an application program executes. Macros help you automate tasks within a program such as Access. Macros let you program your software without requiring you to learn or even understand the basics of writing a program. For example, both Word and Excel include macro languages you can use to automate a series of commands. When you record and then execute a macro, the macro typically mimics the steps a user would take when manually using a program. Access also has a macro language you can use. Access has 53 macro actions; you can pass values (called arguments) to a macro action to control the operations the action performs. (An action is an instruction that directs Access to perform a specific act, such as selecting an option from a menu.) Table 18-1 lists the different macro actions available.

Action AddMenu ApplyFilter Beep CancelEvent Close CopyObject DeleteObject Echo FindNext FindRecord GoToControl GoToPage GoToRecord Hourglass Maximize Minimize MoveSize MsgBox OpenDataAccessPa ge OpenDiagram OpenForm OpenModule OpenQuery OpenReport OpenStoredProced ure OpenTable OpenView OutputTo PrintOut Quit

Purpose Adds a drop-down menu to a menu bar. Used in forms and reports. Applies a filter to a table. Sounds the PC speaker. Cancels the event that ran the current macro. Closes a window. Copies a database object. Deletes a database object. Controls whether a macro updates the screen. Finds the next record meeting the search criteria specified in a FindRecord action. Finds a record that meets your criteria. Changes focus to a particular control. Changes focus to a specified page in a form. Jumps to a specified record. Controls whether the mouse pointer is displayed as an hourglass. Same as clicking your mouse on a window's maximize button. Same as clicking your mouse on a window's minimize button. Moves or resizes a window. Displays a message box. Opens a data access page. Opens a diagram showing the layout of the database. Opens a form. Opens a Visual Basic for Applications module. Opens an existing query. Opens an existing report. Opens and runs a stored SQL procedure. Opens an existing table. Opens a database view. Exports data in either Excel or text format. Prints a database object. Quits Access.

Rename RepaintObject Requery Restore RunApp RunCode RunCommand RunMacro RunSQL Save SelectObject SendKeys SendObject SetMenuItem SetValue SetWarnings ShowAllRecords ShowToolbar StopAllMacros StopMacro TransferDatabase TransferSpreadshe et TransferText

Renames a database object. Redraws a database object on the screen. Updates the information in a control. Same as clicking your mouse on a window's restore button. Runs a Windows or DOS program. Executes a Visual Basic for Applications function. Executes an Access command, such as those on the menu or toolbar. Executes another macro. Executes an SQL statement. Saves an Access object. Selects a named object. Sends information to the keyboard buffer, as if it had been typed. Sends a database object using E-mail. Changes the state of a menu item. Specifies the contents of a field, control, or property. Controls the display of system messages. Removes the effects of a filter. Displays a particular toolbar. Stops execution of all macros. Stops the current macro. Imports or exports information. Imports or exports to a spreadsheet program. Imports or exports to a text file.
Table 18-1 The Access macro actions.

Note: To see detailed help on any particular macro action, search for the action name in the Access online Help system.

Chapter 18: Creating and Using Macros
(This is section 2 of 6 in this chapter)

Creating Macros
Before you start to create a macro, you should have a firm idea of the task you want to accomplish; you may even want to write the steps down or create an outline. Thus, the general steps in creating a macro are as follows:
1. 2. 3. 4. 5. Identify the task you want to accomplish. Plan the steps you must perform to manually accomplish that task. Create the macro code necessary to implement the steps. Test the macro code. Refine your macro code.

6.

Repeat steps 4 and 5 until the macro works correctly.

As you work with macro actions, you will notice that most of them require arguments. Arguments are nothing more than values that affect how an action is carried out. Since actions most often mimic the menu commands you select manually, think of arguments as the settings you specify within the dialog boxes the menu commands display. You create macros by using the Macro window, which is described in the following section.

Understanding the Macro Window
The Macro window is where you tell Access the actions you want the macro to perform. To access the Macro window, you simply select the Macros button in the Database window and then click your mouse on the New button. As you can see from Figure 18-1, the Macro window is similar to the other Access Design windows. The Macro window includes two columns; in the left column, you specify the action you want done, and, in the right column, you include a comment that explains this particular macro step.

Figure 18-1 The Macro window. Picking the actions you want the macro to perform is as easy as choosing them from a dropdown list. Simply click your mouse on the arrow at the right side of any action cell, and Access will display the available macro actions. After you have selected an action, you can modify the action's arguments. The arguments appear at the bottom of the Macro window, similar to

properties in other Design windows. For instance, Figure 18-2 shows the arguments for the OpenForm action.

Figure 18-2 Arguments for the OpenForm action. Once you understand actions and arguments, you can use the Macro window like any other Design window in Access. You can add information, manipulate it, and save it using the same commands. For instance, you can add rows to the macro window using the Rows option from the Insert menu. Conversely, you can delete rows by using the Delete Rows option from the Edit menu. In this case, when you add or delete a row, you actually add or delete an Access action.

A Macro to Automate Adding Records
Note: This rest of this chapter uses the My Friends.mdb database you used in several chapters of this book. If you do not have a copy handy, you may want to use the Friends 3.mdb database located on the companion CD-ROM. You should copy the file to a directory on your hard drive and rename it My Friends.mdb. As an example of how you can use a macro, assume you have a large number of records in the My Friends table. When you use your data entry form, you may find it a nuisance to go through the steps necessary in order to get to where you can add a new record. To get around this, you can develop a macro that will automatically open the form and take you to the end of the table.

Since you've already decided exactly what you want to do, your next step is to decide how the macro will do it. Examining the task, you figure out you want to:

• • • •

Turn off screen updating (the changing screens are distracting) Open the Review Friends form Jump to the last record Turn on screen updating

To begin, display the Macro window. The first action you will use is Echo. The Echo action lets you control whether Access displays what your macro is doing (shows the screen updates). Normally, when a macro action occurs that affects the screen, Access updates the screen right away. In addition to being distracting, screen updates can slow macros down since you must wait for Access to update the screen after each step. The Echo action lets you turn screen updating off. In this way, Access won't update the screen until you later turn screen updating back on. To begin, select the first row in the Action column of the Macro window. Click your mouse on the pull-down arrow at the right side of the column, and you will see the list of available actions, as shown in Figure 18-3.

Figure 18-3 Selecting an action for your macro. Scroll through the list until you find the Echo action. When you click your mouse on the action, the list disappears and Access displays the Echo action in the Macro window. Notice, also, that Access displays the arguments for the Echo action at the bottom of the Macro window. There

are only two arguments for Echo; the first one lets you indicate if Echo is on, and the second lets you display some information for the status line. To turn off screen updates, set the first argument to No. The second argument--the Status Bar Text argument--may take a bit of explaining. If you leave the screen updates off, someone using the macro may wrongly believe that their computer has hung. After all, when Access is busy executing your macro, it will not respond to the user. One way you relieve user anxiety is to display a message letting them know what is going on. You use the Status Bar Text argument to display, in the status bar, a message that informs the user that a macro is executing. With that in mind, type the text Preparing information; one moment please... When you are done, the Macro window should appear as shown in Figure 18-4.

Figure 18-4 The first step of your macro is complete. At this point, you may also want to add a special feature for those who use your macro and have slow computers. For the next macro action, you can make Access change the mouse pointer from the regular arrow to an hourglass. The hourglass is a universal Windows symbol that indicates something is going on and the user should wait. You can do this with the Access Hourglass action. Select the next empty row (the second one) in the Action column. Again, click your mouse on the pull-down arrow and select the Hourglass action. Notice that this action has only one argument--one that controls whether Access displays the hourglass or not. When the argument is set to Yes (as it is by default), Access displays the hourglass. Set the Hourglass action's argument to Yes and move on to your third macro action.

In the next step, you want Access to open the form you use to input information in the My Friends table. To open the form, you use the OpenForm action. Move to the third row in the Action column and select the OpenForm action. As you can see in Figure 18-5, the OpenForm action has quite a few arguments.

Figure 18-5 Arguments for the OpenForm action. At a minimum, you need to fill in the Form Name argument, which tells Access the form you want to open. When you select this argument, notice that Access displays a pull-down arrow at the right side of the argument box. If you click your mouse on this arrow, you can select one of the forms defined in the database. Select the Review Friends form. There are other arguments available for the OpenForm option, as follows:

• • • •

View. This argument defaults to the Form view, exactly as it should be (you want to display the form). You can change this argument to other modes, such as Datasheet, Design, or Print Preview. Filter Name. You can apply a saved query or filter to the information the form displays. Since you are only going to be adding information, simply leave this argument blank. Where Condition. Another way you can filter the information available in the form is to use an SQL Where clause. You can use these clauses to select records in much the same way as you would use a filter. Again, leave this blank. Data Mode. Here you specify one of three choices: Add, Edit or Read Only. Add allows you to add records using the form. Edit lets you change information in the form and add records, and Read Only lets you view but not change the records. While you can select either Add or Edit mode to

allow the addition of records (the purpose of the macro in the first place), it is best to pick Edit in this instance, since it gives you the most versatility.

Window Mode. Controls how Access displays the window. Your choices are Normal, Hidden, Icon, and Dialog. Normal (the default) displays the window as designed by the form designer. Hidden removes the window from view. Icon minimizes the window to the size of an icon. Dialog makes the window pop up as a dialog box.

You can set all of these, if you wish, but there is really no need to do so, other than the Data Mode argument. Once done, you are ready to move to the next step. In the fourth row of the Action column, you need to select an action that will let you enter a new record. To do so, select the GoToRecord action. Access, in turn, will list four arguments in the Macro window. You need to set only one of these arguments (Record). Normally, the Record argument is set to Next, which means Access selects the next record. There are other options available for this argument, however:

• • • • • •

Previous. Selects the previous record. Next. Selects the following record. First. Selects the first record in the table. Last. Selects the last record in the table. Go To. Lets you specify a record you want selected. New. Lets you enter a new record.

The final option (New) is the one which will accomplish your goal of letting the user enter a brand new record. Make the change to the argument, and your macro is essentially complete. There are still a few "clean up" steps which you will want to include, however. For instance, you need to redisplay the screen updates (using the Echo action) and change the mouse pointer back to normal (using the Hourglass action). Go ahead and add these actions, in this order. Your completed macro should now look like what you see in Figure 18-6.

Figure 18-6 The completed macro.

Saving Your Macro
Now that the macro is complete, go ahead and close the macro window. Access, in turn, will ask if you want to save your macro. Select Yes, and Access displays the Save As dialog box (Figure 18-7) to allow you to name the new macro.

[Insert HOA1807.BMP]
Figure 18-7 Naming the macro. Use a descriptive macro name, such as Add New Record. When you press ENTER or click your mouse on the OK button, Access saves the macro and updates the Database window. At this point your macro is ready for use.

A Special Macro
The name you provide for your macro is important. Besides providing a descriptive name, you can also use the special name AutoExec. If you do, Access will automatically run the macro every time you start Access. You use an automatic macro when you want to do any of the following: Automatically configure Access for use Automatically run a special program tied into the macro Automatically load a project you are working on If you don't want the AutoExec macro to run when you start Access, simply hold down the SHIFT key when you start the program.

Chapter 18: Creating and Using Macros
(This is section 3 of 6 in this chapter)

Conditional Macros
When Access runs a simple macro, Access begins at the macro's first action and performs each action, one after another, in the order the actions appear in the Macro window Action column. As your macros become more complex, you may want them to make decisions, based on specific conditions. For example, if a file does not exist, you may want the macro to end. You let macros make such decisions by adding conditions to your macros. These conditions determine when certain macro actions run. For example, assume you want to create a macro to automatically generate a report of the records in the My Friends table. You created a report to do this in Chapter 8, "Creating Simple Reports," so all you need to do is make a simple macro that will run the report. The condition you will check now is whether there are any records in the table to print. If there are no records, you will want to exit the macro without printing anything. With this in mind, the steps for your macro would be:

• • • •

Check to see if records exist. If no records exist, stop the macro. Turn off screen updating Print the report Turn on screen updating

To begin, display the Macro window. Notice that there are only two columns, Action and Comment. To add conditional statements to your macro, you need to add a third column. To do so, click your mouse on the Conditions tool (to the left of the Insert Rows tool) or choose the Conditions option from the View menu. Access changes the Macro window to look like that shown in Figure 18-8.

Figure 18-8 Access displays the Condition column. In the Condition column, you specify expressions that tell Access when it should execute the actions you specify in the Action column. For a review on Access expressions, turn to Chapter 3, "Taking a Closer Look." Condition expressions are the same as those you use in validation rules, filtering, and sorting. For the first step of this macro, you will want Access to determine if there are any records to print. You do this using the DCount function in the macro. The DCount function determines the number of records that meet a specific criteria. When you use DCount in the following conditional statement, DCount returns the number of records in the entire table: DCount("*","My Friends") = 0 Enter this condition in the first row of the Condition column. If the count of records in the My Friends table is equal to 0, the condition is True, and Access will execute the action on that row. As you'll recall, if there are no records in the table, you want the macro to end. To end the macro, you can use the Stop Macro action. Type in StopMacro in the Action column. At this point, your Macro window should look like that shown in Figure 18-9. (Notice that there are no arguments for the StopMacro action; it simply stops the macro.)

Figure 18-9 The conditional and its action have been entered. You can now enter the rest of the macro's actions. You already know how to enter the actions that turn off the screen updating (the Echo action) and change the mouse pointer to an hourglass (the Hourglass action); you used these actions in the previous macro. When you are done entering these two actions, you should be ready to enter the fourth action--the one that will print the report. To print a report, use the OpenReport action. In the fourth row of the Action column, select this action; your Macro window will appear as shown in Figure 18-10.

Figure 18-10 After entering the fourth macro step. There are two arguments for the OpenReport action which you must set. The first, Report Name, is where you identify the name of the report you want to use. If you select this argument, you can click your mouse on the pull-down arrow at the right of the argument box. When you do, you can select from the reports you have defined in your database. Select the My Friends, Alphabetic List report (this is the only one you have defined). The View argument lets you indicate how you want Access to handle the report. If you click your mouse on the pull-down arrow for this argument, you can see that there are three options:

• • •

Print. Prints the report immediately. This is the default setting for the View argument. Design. Displays the Design window for the report. Print Preview. Shows the report in print preview, after which the user can print, if desired.

In this case, the default option (Print) should be selected. This argument causes Access to immediately print the report. The other two arguments for the OpenReport action are Filter Name and Where Condition; these arguments serve the same purpose as they did when you used the OpenForm option earlier in the chapter. For this macro, you can leave both of these arguments blank. When Access finishes printing the report, Access leaves the Report open on the screen. The user won't know this, of course, since the macro has turned the screen updating off. But you

will want to close the report so Access won't display it when the macro turns the screen updating back on. To close the report you use the Close action. Select this action in the fifth row. The Close action, by default, closes the object currently active. You can, however, specify that it close another object by using the three arguments available with the Close action. You should always be as specific as possible when you design a macro. As such, set the arguments to specify the object you want closed (the report). To do this, select the first argument, Object Type, and then click your mouse on the pull-down arrow to its right. You will see a list of different types of objects in the database. Select the Report object and then move on to the next argument. The Object Name argument lets you specify which particular object you want closed. Again, click your mouse on the pull-down arrow and you will see a list of the Report objects available in the database. Choose the My Friends, Alphabetic List report. The Save argument allows you to specify whether Access should save the object when it is closed. This is an important argument, if either your macro changed the object or you allowed the user to change it. In the case of this macro, however, there are no changes. Thus, you can change the Save argument to No. Your Macro window should now look like what you see in Figure 18-11.

Figure 18-11 After closing the report.

Your macro now contains the actions to print and close the report. Now you need to turn on screen updating and to change the mouse pointer back to normal. Your macro performs these steps using the same actions as earlier (Echo and Hourglass). Add these two actions to your macro now. Your Macro window should appear as shown in Figure 18-12.

Figure 18-12 The completed macro. You have now completed your first macro that uses a conditional argument. All you need to do now is save it. Again, to save you macro, close the Macro window and provide a macro name. In this case, use a name such as Print Report. Access will then return you to your updated Database window.

Chapter 18: Creating and Using Macros
(This is section 4 of 6 in this chapter)

Running Macros

It is quite easy to run macros in Access. You can run macros directly or add them to a form. This section describes how to run a macro directly, while the next section will cover how to add a macro to a form. You can run a macro directly in the same way that you would execute or open any other database object. Select the Macros button in the Database window, and then double-click your mouse on the macro name. (You could also select the macro name and click your mouse on the Run button.) For instance, if you double-click your mouse on the Add New Record macro, your screen will appear as shown in Figure 18-13.

Figure 18-13 Running a macro. Notice that your macro worked as you have planned; Access has opened the Add New Records form and prepared it to receive a new record. The macro is actually done at this point, and you can use the form window as you normally would.

Adding a Macro to a Form

When you learned how to create forms (in Chapter 9, "Creating Custom Forms"), you learned that one of the objects you can place on your form is a command button. You can use this type of button to execute macros very nicely. In this section, you will learn how to add such a button to your forms. To begin, assume you want to create a macro that will print the contents of a particular record in your database. This is an easy macro, entailing the following steps:

• • •

Change the mouse pointer to an hourglass Print the form Change the mouse pointer back to normal

You know how to do the first and last steps, but perhaps not the second one. To print an object in Access, you use the PrintOut action. When you first select the PrintOut action, the default arguments cause it to print the currently selected database object--all of that object, meaning every record in the form. Since you only want to print a record, you should change the Print Range argument to Selection; this will cause only the current record to print. Create a macro called Print Form now. Your macro should contain the three steps just described: Hourglass, PrintOut, and Hourglass again. After you save the macro, you will be ready to add it to a form. Next, click your mouse on the Forms button in the Database window. Access displays the forms in your database. Select the Review Friends form. When you click your mouse on the Design button, you will see the Design window for the form, as shown in Figure 18-14.

Figure 18-14 The form Design window. At this point, you should turn off the Control Wizards button in the toolbox. (This is the button in the upper-right corner of the toolbox.) It is important to turn Control Wizards off so Access does not run a Wizard when you use the Command Button tool. It is better, at this point, that you understand how to set up a command button directly. After you turn off the Control Wizards button, choose the toolbox Command Button tool. Place a command button in the lower-right corner of your form. Your button will appear as shown in Figure 18-15.

Figure 18-15 The new command button. Go ahead and double-click your mouse on the button so you can see the Properties dialog box. There are quite a few properties which are available for this control. Most of them are fine just the way they are, but you will want to change a few of them. First, change the Name property (on the Other tab) to something descriptive, such as Print Form. This helps you remember the purpose of the control and can come in handy if you later refer to the control. Next, select the Picture property on the Format tab. Setting this property lets you select a graphic image for the face of your button. When you click your mouse on the builder icon (...) to the right of the property, you will see the Picture Builder window shown in Figure 18-16.

Figure 18-16 The Picture Builder window. Scroll through the pictures in the Available Pictures list, and you will find one that says Printer. Select this picture and click your mouse on the OK button. Access makes the button look like a printer icon. This icon helps the user understand the button's purpose. The Picture Builder will disappear, and you should again see the Properties dialog box. The next property to change is the one that controls what happens when someone clicks the mouse on the command button. Display the Event tab and select the On Click property. If you select the pull-down arrow to the right of this property's box, Access displays a list of macros available. Select the Print Form macro you developed at the beginning of this section. You are now done setting this control button's properties. When you close the Properties dialog box, Access displays the form Design window as shown in Figure 18-17.

Figure 18-17 The finished form design. Close the Design window, saving the new form design as you do so. To test the button, open the form, select a record, and click your mouse on the button. Access, in turn, should provide you with a printout of the form on the screen. Notice, as well, that the button shows up on the printout.

Chapter 18: Creating and Using Macros
(This is section 5 of 6 in this chapter)

Editing Your Macros

To edit an existing macro, click your mouse on the Macros button in the Database window. Access, in turn, will display a list of macros available in your database, as shown in Figure 1818.

Figure 18-18 The Database window with the Macro tab selected. Highlight the name of the macro you want to edit and then click your mouse on the Design button. Access, in turn, displays the Macro window, where you can make changes as described earlier in this chapter. When you are done making changes, close the Macro window. Access, in turn, asks if you want to save your changes; if you click your mouse on Yes, Access saves your changes and return you to the Database window.

Chapter 18: Creating and Using Macros
(This is section 6 of 6 in this chapter)

Summary
With macros, you can tailor your Access environment to your own needs. You can create commands and sequences of commands that help you increase your productivity tremendously. In this chapter, you have learned how you can start to use macros. Before you move on to Chapter 19, make sure you understand the following key concepts:

• • • • • • • •

Macros are sequences of commands which you save under a name. You can replay a macro sequence to accomplish tasks both quickly and easily. You can use macros to accomplish virtually any task you can accomplish manually. You can use macros to make complex operations simple (so you don't need to remember all the steps) or to automate tasks that are repetitive in nature. An action is a macro command; an argument is a setting that defines how Access will carry out the action. You use the Macro window to create macros. In this window, you define the actions the macro executes, the order in which the actions occur, and each action's arguments. To display the Macro window, click your mouse on the Macros button in the Database window and then click on the New button. A macro can contain a conditional statement that tells it to execute an action only if a particular condition is met. To define a condition, you use the same expressions as when you create validation rules, filters, and queries. To execute a macro, select the macro name in the Database window and then click your mouse on the Run button. You can also execute a macro by simply double-clicking your mouse on the macro name. Command buttons let you execute macros from a form or report. After you place the button in the form or report design, you use the object properties of the button to assign the macro to events that can occur with the object. For instance, you assign the macro to run when someone clicks the object with the mouse. To edit existing macros, select the macro name in the Database window and then click your mouse on the Design button. Access, in turn, will display the Macro window where you can make changes to the macro.

In Chapter 19, you will learn more about macros. You will learn how you can use them to create message boxes and menu systems.

Chapter 19: Working with Dialog Boxes and Menus
(This is section 1 of 3 in this chapter)

In the previous chapter, you learned how you can use macros to automate many of the tasks you normally perform in Access. In this chapter, you will build on that information to learn how you can create your own dialog boxes and menus. Using the tools presented in this chapter, you will make your macros behave very much like Access itself--using menus and dialog boxes to improve the user's interface. By the time you finish this chapter, you will understand the following key concepts:

• • • •

How to use message boxes How to design dialog boxes How to create a switchboard menu How to add custom menus to your forms and reports

Adding Dialog Boxes

Note: One of the databases used in this is the My Friends.mdb database you used in Chapter 18, "Creating and Using Macros." If you do not have a copy handy, you may want to use the Friends 4.mdb database located on the companion CD-ROM. You should copy the file to a directory on your hard drive and rename it My Friends.mdb. Within Windows and Windows-based programs, message boxes and dialog boxes are the primary means of communicating with a user. As you will learn, the same is true for your Access macros. Access lets you create and use both message boxes and dialog boxes within your macros.

Displaying Messages
You see them all the time in Windows--message boxes that inform you that something is going on or that your program needs more information in order to accomplish a task. In Access, you see a message box every time you leave a Design window after making some changes, asking if you want to save your changes. Message boxes contain some sort of small icon, a message, and some buttons which control the actions that Access performs. You can create your own simple message boxes within Access macros. For example, a very simple use of a message box is to inform the user that Access has saved a particular record. To create such a dialog box, you develop a macro and use the MsgBox action within the macro. First, click your mouse on the Macros button in the Database window. Next, click your mouse on the New button. Access, in turn, displays the Macro window. In this case, you will develop a one-line macro. Within the first row of the Action column, select the MsgBox action. Access displays the Macro window as shown in Figure 19-1.

Figure 19-1 The Macro window. Now you need to set the arguments for this action. The arguments control what message, icon, and buttons Access displays within the message box. The Message argument defines what Access displays in the message box. The text can be any message you desire, up to 255 characters in length. Access takes care of formatting the text that will appear within the message box. For this message box, set the Message property to something like The record has been added. The Beep argument controls whether Access sounds the bell when it displays the message box. The Beep argument is a simple Yes/No property. Since Access displays this message box every time the user enters a record, the beep could get annoying; change the property to No. The Type argument determines the type of icon Access displays in the message box. You have five choices, as detailed in Table 19-1. You should pick the type of icon that is most appropriate for the purpose of the message box. In this case, the message box is purely for informational purposes, so set the Type property to Information.

Setting None Critical Warning? Warning!

Meaning Access displays no icon. Access displays a stop sign. Access displays a question mark. Access displays an exclamation

point. Informatio n Access displays a lowercase i.

Table 19-1 Possible settings for the Type property of the MsgBox action. The Title argument controls what title Access shows at the top of the message box. If you don't specify a title, Access uses the words "Microsoft Access" as a title. The title you select is entirely up to you. Note: When you use message boxes within a macro, Access does not let you specify the number of buttons to display in the message box. If you want to do that, you must use Visual Basic for Applications, which lets you create more versatile message boxes. The MsgBox action displays only an OK button, which makes it well suited to displaying error or other informational messages. You are now ready to save your macro (yes, with only one action). Click your mouse on the Close icon in the upper-right corner of the macro window and save the macro under the name Record Saved. To attach the macro to a form, you first need to display a form. (This makes sense, right?) Open the Design window of the Review Friends form and display the Properties dialog box for the entire form. In the Properties dialog box, notice the After Insert property on the Event tab. This property lets you specify which macro Access should run right after it adds a new record to the table. Select this property and then click your mouse on the pull-down arrow at the right side of the property box. Specify that Access run the Record Saved macro. Now Access will display your new message box every time you add a new record to the table. Note: If you need a refresher on how to set properties for an entire form, refer to Chapter 9, "Creating Custom Forms." Save the form and then open it. Next, add a new record to the table. When you leave the record to display the next one, Access inserts the record in the table and displays your message box, as shown in Figure 19-2.

Figure 19-2 The completed message box.

Your Forms as Pop-Up Windows
Note: This section uses a version of the Customers.mdb database you developed in earlier chapters of the book. You should copy the Customer 7.mdb database from the companion CD-ROM to a directory on your hard drive and rename it Customer.mdb. You

should do this rather than using any older versions of Customers.mdb, since the CD-ROM version has some new forms in it that you will need. You already know from Chapter 9, "Creating Custom Forms," that Access lets you create virtually any type of form you can imagine. These forms can contain many different controls, and you can use them to enter, display, edit, or manipulate the information in your data tables. Normally, you simply open these forms in order to use them. You may, however, want to create a form that Access uses as a pop-up window. For example, consider this scenario:

• • •

You create a form that adds customers to a table. You can use this form for the main information about the customers. You create a second form for ancillary customer information, such as the name of the sales rep on the account. On the main form, you place a command button titled "Other Info." When the user clicks the mouse on this button, Access again presents the secondary form. When the secondary form is closed, Access returns the user to the main form.

Such a scenario is easy to implement in Access. All you need to do is develop the two forms and then tie them together using the command button. If you loaded the Customer database, the two forms for this example are the New Account Setup and the Ancillary Info forms (the are already created in the database). These forms are tied into each other, such that Ancillary Info "pops up" on the screen when you click your mouse on the command button. Figure 19-3 shows how the New Account Setup form looks when it is running.

Figure 19-3 The New Account Setup form. When you click your mouse on the Other Info command button, Access executes the Display Ancillary macro. This macro contains only a single line, which opens the Ancillary Info form using the OpenForm action. The action's Where argument is set to an expression that ensures

the record Access displays in Ancillary Info is the same record Access displayed in the New Account Setup form. Take a look at the form properties set in the Ancillary Info form. There are a number of these properties set to make the form behave like a dialog box. These properties include the following:

• • • • • • • • •

Scroll Bars (Format tab properties). This property determines if Access displays scroll bars in the form. This is set to Neither, since dialog boxes typically don't have scroll bars. Record Selectors (Format tab properties). If set to Yes (the default), a record selector appears at the bottom of the dialog box. Since this form edits only the current record, as determined in the New Account Setup form, this property is set to No. Navigation Buttons (Format tab properties). Determines if the record navigation buttons appear at the bottom of the dialog box. This property is set to No since you don't want users to move around the records using this form. Pop Up (Other tab properties). This property is set to Yes to indicate that you are creating a pop-up dialog box rather than a normal form. Modal (Other tab properties). If set to Yes, this dialog box (the form you are creating) will remain active until the user closes it. This property is set to Yes to force the user to deal with the dialog box before proceeding. Border Style (Format tab properties). There are four types of borders you can use for the form: None, Thin, Sizable, and Dialog. Since you are creating a dialog box, this property is set to Dialog. Control Box (Format tab properties). Determines whether the Control menu is available from the dialog box. Most dialog boxes have a Control menu, so this property is set to Yes. Min Max Buttons (Format tab properties). Determines if the dialog box has a minimize and maximize buttons. Typically, this property should be set to None. Close Button (Format tab properties). Determines if the dialog box has a Close button. Typically, this property should be set to Yes.

Note: Don't set both the Control Box and Close Button properties to No unless you provide some other way to close the dialog box (such as a Close button on the dialog box itself). If you don't provide some way out of the dialog box, you will never be able to close it. With these properties set in this way, the Ancillary Info form behaves just like a regular dialog box. When a user clicks their mouse on the Other Info button, the dialog box appears. The user can then change or enter the information and close the dialog box. Figure 19-4 shows what your screen looks like when Access displays this dialog box.

Figure 19-4 Using the Ancillary Info form as a dialog box.

Chapter 19: Working with Dialog Boxes and Menus
(This is section 2 of 3 in this chapter)

Adding Menus
As you start to develop a series of forms for a particular database, you might want to create some method to display them in an organized manner. You probably have a good idea of how to use your forms and how to access them from the Database window. However, others who use your database may not have your Access knowledge. One answer to this problem is to develop menus from which any user can make choices.

There are two types of menu systems you can devise in Access:

• •

Switchboard menus Custom menus

The following two sections cover these two menu types.

Developing Switchboard Menus
A switchboard menu is a series of forms you develop that only call other forms. For instance, you might have a form named Startup which has nothing to do with any tables in your database. Instead, the form simply presents a series of command buttons, each of which leads to another form or report. In other words, the menu acts much like a switchboard operator, connecting you to the form you desire. As an example, assume you want to develop a switchboard menu for the My Friends database. In this case, your switchboard menu should contain four choices:

• • • •

Add records Review records Print records Exit

To build your switchboard menu, simply create a new form called Startup. This form doesn't have to be particularly large, but you want it big enough to contain a graphic and the four buttons. You will tie each of the buttons to a macro that accomplishes the desired action. The Startup form looks like what is shown in Figure 19-5. Notice that the Startup form displays no database records; the only thing this switchboard menu does is provide users with a way to access the features you have programmed (via macros) into the database.

Figure 19-5 The Startup form.

In earlier chapters, you examined each of the techniques you need to create the Startup form. You learned how to create forms and change form properties in Chapter 9, "Creating Custom Forms." In Chapter 17, "Using OLE," you learned how to use OLE objects with databases and forms. In Chapter 18, "Creating and Using Macros," you learned how to tie macros to a form command button. The following sections describe what happens when you click your mouse on the various command buttons on this switchboard menu. Note: The Startup form and the other macros and forms required to use it are contained on the companion CD-ROM in the Friends 6.mdb database. You should copy the file to a directory on your hard drive and rename it My Friends.mdb.

Add Records
When you click your mouse on the form's Add Records command button, the form runs the Add New Record macro. You created this macro in Chapter 18. As you will recall, the macro displays the Review Friends form, positioning the record counter at the point where you can add a new record, as shown in Figure 19-6.

Figure 19-6 Adding a new record.

Review Records
Click your mouse on the switchboard form's Review Records button. The form, in turn, will call up a new macro, Review Records. This macro consists of a single action: OpenForm. Figure 197 shows the Macro window, with the arguments for the OpenForm action.

Figure 19-7 The single action used in the Review Records macro. By setting the Data Mode argument to Read Only, you stop users from editing the data in the table. This lets them browse through the records, but stops them from making changes. If the user wants to make changes, they can use the Add Records option. The Add Records macro opens the same form (Review Friends) with the Data Mode argument set to Edit.

Print Records
In Chapter 18, "Creating and Using Macros," you developed the Print Report macro. This switchboard menu ties the Print Report macro to the Print Records command button. The Print Report macro prints all the records in the table in alphabetical order. When you click your mouse on this button, you will simply see the notice that the report is printing. When the macro is done, Access displays the switchboard menu, where you can choose another option.

Exit

When you click your mouse on this option, the switchboard menu calls a single-line Exit macro which contains a single Close action. When you execute the Exit macro, it closes the current object (which happens to be the Startup form), which, in turn, effectively ends the program.

Adding Custom Menus
With a simple application, such as the My Friends database, using a switchboard design is simple and straight-forward. As your database increases in complexity, however, the switchboard approach develops a few drawbacks. The main disadvantage is that users can quickly become lost in a maze of forms, buttons, and choices. Luckily, Access provides another alternative, custom menus. You can design your own menu system to either augment or replace the built-in Access menus. Custom menus offer an easy method to view multiple options at a single glance. Also, custom menus present options in a way familiar to those users comfortable with the Windows environment.

Understanding How to Customize Menus
You customize menus in Access using very intuitive techniques. All you need to do is choose the Customize option from the Tools menu. Access displays the Customize dialog box, as shown in Figure 19-8.

Figure 19-8 The Customize dialog box. You can actually use the Customize dialog box to change just about every aspect of Access that you can see. To change menus, however, all you need to do is start working with the actual menus, while the Customize dialog box is displayed. This is easiest to understand through an example. Let's say you want to change the order in which some options appear on a menu--you may want to change the Relationships option on the Tools menu so that it appears higher on the menu. To do this, simply click your mouse on the Tools menu. The entire menu should appear, as shown in Figure 19-9.

Figure 19-9 The Tools menu during customizing. All you need to do in order to reposition the Relationships option is to click your mouse on it and drag it to a new position on the menu. When you release the mouse button, the menu option stays where you dropped it. This on-screen customizing technique is very powerful, and you can do other customizing tasks, as well. For instance, if you drag an existing menu choice off a menu, it is deleted. If you want to rename a menu item, simply right-click your mouse on the option. The result is a Context menu, as shown in Figure 19-10.

Figure 19-10 A Context menu is used to display customizing options for an existing menu item. If you change the setting in the Name field on the Context menu, the name of the menu item is changed. Notice that the existing menu name (&Relationships...) has an ampersand (&) at the beginning of it. This ampersand is the way you denote which letter is underlined in the menu option (the "R" is underlined in Relationships), and which key is therefore the hot key used to select the option from the keyboard. You can place a single ampersand before any letter you want in your menu name. If you look again at Figure 19-10, you will notice that the Context menu presents quite a few other customization actions you can use, if desired. If you like, you can experiment with these to see their affect on your menus. When you are done with your customizations, simply close the Customize dialog box. The changes you made remain persistent until you either change the menus again or reset them to their default.

Restoring Menus to Their Default

At some point you may wish to undo the changes you have made to a menu. Fortunately, Access provides a very quick way you can do this. With the Customize dialog box displayed, all you need to do is right-click your mouse on a menu. Make sure you right-click your mouse on the menu itself, not on an item in the menu. For instance, if you wanted to reset the Tools menu to its default condition, you would right-click your mouse on the Tools option on the toolbar. Access displays a Context menu with options you can use in customizing the menu. With the Context menu displayed, all you need to do is choose the Reset option. (It is the first option on the menu.) Access closes the Context menu and the Tools menu, and everything is reset to normal. In this case, "normal" means that the Tools menu will look just like it did when Access was first installed.

Creating a Custom Menu System
Access gives you complete control over the menus displayed when you are using your own forms and dialog box. The first step, however, is that you need to create the actual menus that will be used. For example, let's assume you want to create a new menu system that is displayed whenever someone has the New Account Setup form open in the Customer database. (You will, of course, need to close the My Friends database and open the Customers database.) The way that you create a menu system is a bit awkward and non-intuitive in Access. To create such a menu system, follow these steps:
1. 2. 3. Choose Customize from the Tools menu. Access displays the Customize dialog box. Make sure the Toolbars tab is selected. (I know it sounds strange to use toolbars to create menus, but that is the way Microsoft set it up.) Click your mouse on the New button. Access displays the New Toolbar dialog box, as shown in Figure 19-11.

Figure 19-11 The New Toolbar dialog box.

4.
5. 6.

In the Toolbar Name field, enter the name you want to use for your menu. For this example, enter a name of New Accounts Menu. Click your mouse on the OK button. Access adds the new toolbar/menu to the list of toolbars, and displays an empty toolbar beside the Customize dialog box. Click your mouse on the Properties button in the Customize dialog box. Access displays the Toolbar Properties dialog box, as shown in Figure 19-12.

Figure 19-12 The Toolbar Properties dialog box.
7. 8. Change the Type pull-down list so the Menu Bar option is selected. Click your mouse on the Close button. Access closes the Toolbar Properties dialog box and again displays the Customize dialog box. 9. Click your mouse on the Commands tab in the Customize dialog box. 10. In the list of Categories, select New Menu. Access displays a single option (New Menu) at the right side of the dialog box, in the Commands list. 11. Use the mouse to drag the New Menu option from the Commands list to your blank menu bar. The words "New Menu" should now appear on your menu bar. 12. Right-click your mouse on the New Menu option on your menu bar. Access displays a Context menu.

13. In the Context menu, change the Name to &File.
14. In the Categories list on the Customize dialog box, select the All Macros option. The only thing that should appear in the Commands list is the Display Ancillary macro, as shown in Figure 19-13.

Figure 19-13 Creating a new menu command.
15. Use the mouse to drag the Display Ancillary option from the Commands list to the new File menu you created on your menu bar. 16. Drag your new menu bar so it is located just below the regular Access menu bar and just above the toolbar, at the top of the program window. 17. Click your mouse on the Toolbars tab in the Customize dialog box. 18. Make sure the check box beside the New Accounts Menu option is cleared. Access removes the new menu on which you have been working. 19. Click your mouse on Close. Access closes the Customize dialog box.

In this example, you created just a single menu and a single option on that menu. Your menus can be as complex or as simple as you desired. This very simple menu system is more than acceptable to show how Access treats your custom menus.

Using Custom Menus
After you define a custom menu system, you can use it with specific forms or reports in your system. To do so, you need to set the Menu Bar property for the form or report. To continue the example begun in the last section, assume you want to use your new custom menu system

with the New Account Setup form you created earlier in the book for the Customer.mdb database. To do so, follow these steps:
1. 2. Display the Design window for the New Account Setup form. Display the Properties dialog box for your form. Make sure the Other tab is selected, as shown in Figure 19-14.

Figure 19-14 The form Properties dialog box.
3. 4. 5. 6. Select the Menu Bar property. Click your mouse on the pull-down arrow to the right of the Menu Bar property box. Select the New Accounts Menu option. This is the menu bar you created in the previous section. Close and save your form.

That's all there is to it. The next time you open this form, Access will automatically load and use the custom menu, as shown in Figure 19-15.

Figure 19-15 Using the custom menu. Note that the custom menu system you designed in the previous section is now the only menu visible when the New Account Setup form is open. When the form is closed, Access hides the custom menu and displays the default Access menu bar again.

Chapter 19: Working with Dialog Boxes and Menus
(This is section 3 of 3 in this chapter)

Summary

Once you know the basics of designing macros, you can make your database more "user friendly." Here you have learned how to use dialog boxes and menus to communicate with users. Before you move on to Chapter 20, make sure you understand the following key concepts:

• • • • •

Within macros, you use a message box to provide information to whoever uses your database. A message box typically displays a short message and includes an OK button that users can click their mouse on to close the box. The MsgBox action lets you control the message that Access displays in the message box, as well as the icon Access displays beside the message. If you want Access to use one of your forms as a dialog box, set the properties of the form so they closely resemble the attributes of a dialog box. A switchboard menu lets a user select from a series of on-screen choices. You can design a form that presents a switchboard menu that makes your database much easier to use. Access allows you to quickly and easily customize menus and create entirely new menu systems you can attach to forms and reports.

In Chapter 20, you will learn the fundamentals of the Visual Basic for Applications programming language.

Chapter 20: Using Visual Basic for Applications
(This is section 1 of 6 in this chapter)

As you work with Access, there may be times when you can't write a macro that's capable of performing the operations you need. In such cases, you can turn to Visual Basic for Applications, a programming language that's built into Access. In short, using Visual Basic for Applications, you specify a list of instructions you want Access to perform. In the previous two chapters, you have learned a bit about programming; after all, that's what macros effectively let you do. Macros, however, don't give you all the capabilities of a programming language. In this chapter, you will plunge head-first into the world of programming. By the time you finish this chapter, you will understand the following key concepts:

• • • • • • • • •

What Visual Basic for Applications is How Access uses procedures, functions, and subroutines The parts of a Visual Basic for Applications program How to use statements, variables, operators, and functions within your program How to address database objects in Visual Basic for Applications How to use the VBA Editor How to develop a Visual Basic for Applications procedure How to test your procedures How to use a procedure from an Access form

Note: This chapter is not a substitute for a good book on programming. There are books available on the market which can help you with either Visual Basic for Applications or Visual Basic (they are very similar). This chapter will provide you with a quick introduction to Access programming using Visual Basic for Applications.

What is Visual Basic for Applications?
Visual Basic for Applications (VBA) is a programming language built right into Access. This language is built on the Basic programming language made popular by PCs over the past 25 years. VBA shares similarities with other Windows implementations of the Basic language, such as Visual Basic. If you know how to program in Visual Basic, you will feel immediately comfortable programming in VBA. If you are familiar with programming in a different language (including using macros), you can quickly get up to speed with VBA. There are a few terms you should know before you start programming in VBA:

• • • •

Procedure. A section of programming code, designed to accomplish a specific task, which your program statements can use throughout your program. There are two types of procedures: functions and subroutines. Function. A procedure that returns a value after it completes its task. When called, functions typically appear on the right side of an equal sign in an expression. Subroutine. A procedure that does not return a value after it completes its task. Module. A named collection of procedures. You typically attach modules to specific forms or reports.

You can accomplish many tasks simply by using macros. This implies--and correctly so--that not everyone needs to use VBA. As a general rule, if you can accomplish a task by using macros, do so. Macros are simpler to implement and easier to maintain. However, there are certain tasks which you cannot do with macros or which you can better implement through VBA. You should use VBA instead of macros when you:

• • • • •

Want to create your own functions. Need to handle events that pass parameters or accept return values. Need to perform error handling. Need to process information on a record-by-record basis. Want your application to run as quickly as possible.

These reasons are either a side-effect of using VBA (such as the faster speed) or will be covered to one degree or another in this chapter. There are other instances when you should use VBA, as well:

• • • • •

When you don't want to manually create or manipulate database objects. If you want to create a library. If you are writing your own Wizards. When you need to perform system-level operations such as DDE and OLE. When you want to access Windows API functions or DLL files for other applications.

These reasons involve using VBA at a level that this chapter will not cover. They are still valid reasons, however. If you find you need to perform any of these advanced operations, purchase a good programming reference and any other VBA programming information you can find.

Getting Help for VBA
Throughout this book, you have learned how you can use the online Help system. When you program in VBA, you will probably use the Help system quite a bit. This is because of all the rules that you must follow when you use VBA. As you use the programming section of the online Help system, you may find these guidelines helpful: Use the Help tool to select the programming statement you need help with. Use the search feature to find information on a specific topic, function, or statement. Only use the Help system table of contents if you can't find help any other way.

Chapter 20: Using Visual Basic for Applications
(This is section 2 of 6 in this chapter)

Parts of a Program
In VBA, a program consists of procedures. These procedures are made up of individual program lines, which you must put together according to VBA rules. These rules are referred to as syntax. Think of syntax as programming grammar--if you don't follow the rules, VBA can't understand what you are trying to do. There are many elements that go into a program, including the following:

• • • • •

Statements Variables Operators Functions Database objects and collections

The following sections explain each of these elements.

Understanding Statements
When you write programs in VBA, you create programming statements. For example, consider the following line of code, which is commonly used in programs: Option Compare Database The first two words of this line are a VBA statement; the last word is a modifier for the statement--sort of like an argument when you are writing macros. VBA defines many different

statements (scores of them) you can use in your programs. These statements do everything from defining how VBA should behave to setting the current disk drive. Note: A detailed description of each VBA statement is beyond the scope of this book. If you want to see what statements are available, use the on-line Help system available within the VBA Editor to search for Statements, then choose the Statements Reference topic.

Understanding Variables
Your VBA programs use variables to store information. Just like you can store data in a field within a table, you can store information in a variable for use in a program. Variables are so named because their contents can vary, meaning your program can change their values. Variables can be very powerful. They let you create general-purpose routines that can act and make decisions based on the value within the variable. For example, consider the following logical sentence:

 If the temperature is high enough, I will go to the beach.
In this instance, the temperature is the variable. If it is high enough, you will do something; if not, you will not. Variables serve the same purpose in a program. Later in this chapter, you will learn how VBA uses variables to make decisions within your programs.

Data Types
In Chapter 3, "Taking a Closer Look," you learned about data types in relation to information stored in a database. When you define a table, you need to define what type of information Access will store in each field of the table. In this way, variables are no different than fields. You need to be aware of what information your program is going to store in the variable, and then you need to use the correct data type so VBA knows how to handle the value. VBA supports eleven different data types, as indicated in Table 20-1.

Data Type Boolean Byte Integer Long Single Double Currency Date String Object Variant

Type of Values Either logical True or logical False Numbers between 0 and 255 Numbers between -32,768 and 32,767 Numbers between -2,147,483,648 and 2,147,483,647 Numbers between -3.402823E38 and 3.402823E38 Numbers between -1.79769313486232D308 and 1.79769313486232D308 Numbers between -922,337,203,685,477.5808 and 922,337,203,685,477.5807 Date between January 1, 100 and December 31, 9999 Up to 65,535 characters (approximately) Reference to a database object Varies based on the characteristics of the information
Table 20-1 Data types supported by VBA.

Notice that some of the data types shown in Table 20-1 are numeric data types; this means they hold number values. When you take into account the setting of the Field Size property for

the field, VBA data types correspond to field data types. As you can see from Table 20-1, the primary difference between the numeric data types is the size and accuracy of the values they can hold.

Scientific Notation
Very large and very small numbers are cumbersome to write. To get around this, you can use scientific notation. Using scientific notation, you can express very large or very small numbers easily. For example, you can write the number 7,884,000,000,000,000 as 7.884E15 (notice the E in the middle of the number). Likewise, you can write the number 0.000123 as 1.23E-4. To convert a number to scientific notation, move the decimal point so that the resulting number is between 1 and 10. After the E, specify the number of places you moved the decimal point. If you move the decimal point to the left, use a positive number after the E; if you move it to the right, use a negative number. To convert a number from scientific notation, you reverse this process--move the decimal place the number of positions indicated by the number after the E. The way VBA expresses scientific notation depends on the data type being used. VBA uses the E when you work with a single data type and the D when you work with a double data type. Thus, 9.14E12 refers to a single-precision data type, and 9.14D12 refers to a double-precision value. The String data type is comparable to the text data type for fields. String type variables can contain any characters--letters, numbers, or symbols--which you can think of. The Date data type is not really a date, as you might expect. For instance, humans represent a date such as June 11, 1999. VBA, however, works with date serial numbers. These are a numeric representation of a date, in the form of a number. The appearance of that number (for instance, June 11, 1999) is the format of the number, not the date serial number itself. Access uses the special Date data type to contain serial numbers which represent any date between January 1, 100 and December 31, 9999. (No Y2K bug here!) Another interesting data type is Object. This data type is actually a reference to an object tracked by Access. Many of the actions you perform in VBA are upon objects, such as tables, forms, reports, and the pieces thereof. Each of these are objects, and can be addressed within VBA. Later in this chapter you will learn more about VBA objects in Access. The last VBA data type is not supported directly in setting data types for fields. The Variant data type automatically adapts to the type of information it contains. If you store a number in a variant, it behaves like a numeric data type. If you store characters in it, it behaves like a string. In addition, you can store special values in a variant, such as dates and times. There are nine types of data you can store in variants:

• • • • • • •

Empty Null Integer Long Single Double Currency

• •

Date String

Most of these types of data should look familiar by now. The first two, however, may not. A variant is empty if your program has never assigned it a value. Typically, a variant is empty at the beginning of your program, before you ever use it. A variant is Null if your program has initialized it but it currently contains no data.

Declaring Variables
Before you can use a variable within your program, you need to declare it. This simply means that you need to let VBA know what name you are going to use for the variable and what data type it will store. There are two ways you can declare a variable--either implicitly or explicitly. Implicit declaration means you simply start using the variable. For example, if VBA encounters the following line in your program, and you have never used the variable LittleCount before, then VBA assumes you want to define a variable by the name of LittleCount. LittleCount = 123.456 Because you are assigning a single precision value to the variable, you have implied the variable's type as single. Explicit declaration means you declare the variable before actually using it. You explicitly declare using one of the following declaration statements

• • •

Dim. Dimensions (sets aside space for) a variable. You can access the variable within the current procedure only. Each time your program uses the procedure, VBA reinitializes the variable. Global. Same as Dim, except the variable is accessible in any procedure. Static. Same as Dim, except VBA does not reinitialize the variable every time the procedure is used.

In general, you should use explicit declarations in your programs. In this way, VBA can check to make sure you are using your variables correctly. To enforce explicit variable declaration, you can put the following statement in the declarations section of your program (before you define any variables): Option Explicit

Variable Arrays
In order to keep related information together, VBA lets you define variable arrays. Arrays use subscripts to differentiate between different elements of the same overall variable. For instance, you can declare a variable as a string and give it 15 subscripts, as shown here: Dim Employee(15) As String In this case, there are 15 occurrences of Employee, numbered 1 through 15, as in Employee(1), Employee(2), Employee(3), and so on through Employee(15). Each element of the array is a different variable. Because these elements share the same base name (Employee), your program can process the information in the array easier. Each element in the Employee array can store a character string. In this example, Employee has 15 elements, but

only one dimension. You can also define arrays to have more dimensions. Thus, you could declare the following type of variable: Dim Employee(15,20) As String In this case, Employee has 300 elements (15 × 20 = 300), each one a separate variable, and two dimensions. You can use as many dimensions as you desire, but most programmers stick with one- or two-dimensional arrays simply because their needs never extend past that.

Assigning Values to Variables
To inform VBA that a particular variable should contain a particular value, you need to assign the value to the variable. To assign a value to a variable, use the assignment operator (the equal sign) in an equation, as shown here: Dim SpeedLimit As Integer SpeedLimit = 65 After you program this assignment, you can use the variable SpeedLimit anywhere else in your program and VBA will understand you actually mean the number 65. To assign a group of characters to a string variable, just surround the characters with quotation marks. As shown in the following, the quotation marks indicate to VBA where the string begins and ends: Dim BookTitle As String BookTitle = "Learn Access Now"

Naming Variables
When you choose names for your variables, you must follow VBA guidelines. If you disregard the guidelines, VBA will not behave in the way you want. The guidelines are: Names must begin with a letter of the alphabet Names cannot contain spaces Names must be no longer than 40 characters Names cannot use words reserved for VBA use Reserved words are those which VBA or Access uses for other purposes--typically as statements, functions, or keywords. If you want to see the list of reserved words, search for the term reserved words in the online Help system within the VBA Editor.

Understanding Operators
An operator is a symbol that informs VBA about the operation you want to perform. For instance, the expression 2 + 2 uses an operator--the plus sign. There are 4 general categories of VBA operators:

• • •

Math operators. Used to put together mathematical equations. Examples include addition, subtraction, multiplication, and division. Comparison operators. Used to determine how two operands relate to each other. The result of a comparison operation is always either True or False. String operators. Used to combine the characters in two strings.

Logical operators. Used to test conditions. Examples include AND, OR, and NOT. The result of a logical operation is always either True or False.

There are 20 different operators spread among these 4 categories, as shown in Table 20-2.

Operato r + * / ^ \ Mod = < > <= >= <> & AND EQV IMP NOT OR XOR

Category Math Math Math Math Math Math Math Comparis on Comparis on Comparis on Comparis on Comparis on Comparis on String Logical Logical Logical Logical Logical Logical

Meaning Addition Subtraction Multiplication Division Exponentation Integer division Modulus Equal Less than Greater than Less than or equal Greater than or equal Not equal Concatenation And Equivalent Implication Not (or the logical opposite of) Or Exclusive Or

Table 20-2 Operators in VBA.

Understanding Functions
Technically, a function is a procedure that returns a value. VBA has a wide variety of built-in functions which you can use in your programs. These functions are important because they keep you from having to "reinvent the wheel" for common tasks. There are many, many functions built into VBA. These functions are categorized by the type of work they do. The categories are:

• • • •

Math Functions Financial Functions Data Conversion Functions String Functions

• • •

Date and Time Functions Transcendental Functions Miscellaneous Functions

Note: If you want to see information about a particular function, you can search for that function in the on-line Help system within the VBA Editor. When you use a function, you generally use it in an equation or some other place where a value is needed. This is because functions, by definition, return a value. For instance, if you want to find the length of a string, you can use the Len function as follows: Dim HowLong As Integer, MyString As String HowLong = Len(MyString) These statements set the variable HowLong equal to the length of the value in MyString. Thus, if there were 25 characters in MyString, the Len function will assign the value 25 to the variable HowLong. Notice, also, that functions require the use of arguments (sometimes called parameters). In this example, the variable MyString is an argument for the Len function. Some functions require only a single argument; others can require quite a few. These arguments are similar to the arguments you use with macro actions. The number, type, and specifications for arguments will vary based on the function.

Referring to Database Objects
Throughout this book you have learned ways you can manually work with database objects-things like tables, forms, reports, macros, and the like. From a programming perspective, these objects fit into a technology known as DAO, which means data access objects. Once you understand the fundamentals of DAO, you can refer to any portion of any VBA object you desire, all under program control. The DAO framework recognizes two types of database components: objects and collections. An object is typically a single database component (many of which you have already learned about), although it can also encompass other objects or collections. A collection is a group of related objects; it provides a way for you to reference an entire group of objects at one time. Table 20-3 lists the various collections and objects that belong to the DAO framework.

Collectio n Containers Databases Document s Fields Groups Indexes Parameter s

Object Container Database DBEngine Documen t Field Group Index Paramete r

Purpose Hold information about other objects An open Access database The Jet database engine Information about other objects in the database A column that is part of a table, query, recordset, index, or relation A group of user accounts in the current workgroup A table index A query parameter

Properties QueryDefs Recordset s Relations TableDefs Users Workspac es

Property QueryDef Recordse t Relation TableDef User Workspac e

A property of an object A saved query definition The records defined by a table or query A relationship between fields in tables or queries A saved table in a database A user account in the current workgroup An active session of the Jet database engine

Table 20-3 DAO collections and objects recognized by VBA. The collections and objects that make up DAO are defined by the Jet database engine, which is the heart of Access. In addition to DAO objects and collections, Access also recognizes the group of objects and collections (referred to as Access objects) shown in Table 20-4. (Note that not all objects have corresponding collections.)

Collectio n

Object Applicatio n Control Debug

Purpose The current instance of Access A control on a form or report The VBA immediate window A form or subform A report or subreport The video display

Forms Reports

Form Report Screen

Table 20-4 Microsoft Access objects

Properties and Methods
Earlier in this book, you learned about properties and how you use them to define objects in tables, forms, reports, and other database objects. Likewise, you use properties to define DAO objects and collections and Microsoft Access objects. Most of the object and collection properties are the same properties you can manipulate in a Properties window. Objects and collections also have methods, which are special functions that act on an object or collection. For instance, a Workspace object has a CreateDatabase method; when you use the Workspace object, its method creates a new database. As with properties, the available methods will vary based on the object or collection.

Addressing Objects and Collections
Access provides a method that lets you address anything within your database. Since objects and collections form a hierarchical system, it may be helpful to compare the Access address system to a similar system you should already be familiar with--the directory structure on your disk drive. As you move through different levels in a directory structure, you indicate a change in level using a backslash. For example, consider the following pathname:

C:\WINDOWS\SYSTEM\SYSEDIT.EXE As you read the pathname from right to left, you learn that the SYSEDIT.EXE file is in the SYSTEM directory, which is within the WINDOWS directory, which is on the C: drive. Each backslash indicates you are moving down a level in the directory structure, when reading from left to right. In Access, you use exclamation points (!) and periods (.) to denote changes in the level. The exclamation points are called bangs, and the periods are called dots. The general rules which govern the use of bangs and dots are as follows:

• • •

You use a bang before objects you have named. If a name contains spaces, you surround it with brackets, as in [Zip Code]. You use a dot before objects, properties, or methods named by Access.

How do you apply these rules? Simple. Suppose you want to refer to the Business Customers table you created earlier in this book. This table is part of the Customer database, so you can address it as follows: Customer![Business Customers] Notice the brackets around the table name and the bang between levels of the hierarchy. If you want to refer to a property within the table, you can use a dot and the property name, as in the following: Customer![Business Customers].RecordCount

Object Variables
Earlier in this chapter, you learned that within your programs you use variables to represent actual values. These variables can be a specific data type, which indicates the type of information they contain. For instance, consider the following code: Dim SalesTax As Single SalesTax = .05 The purpose of the first line is to specify a new variable named SalesTax, which will have the characteristics of a Single data type. This line does not indicate what value the variable will contain; the second line assigns the variable's value. After Access executes the second line, VBA uses the value .05 whenever you refer to the SalesTax variable. Notice there is a distinct difference between an actual value (.05) and the label used to refer to that value (the variable SalesTax). You refer to objects within VBA in the same manner. You can assign a label--a variable name-to the object. Access refers any references you make to the label to the actual object that the label represents. For instance, take a look at the following: Dim WorkDB As Database Set WorkDB = DBEngine.Workspaces(0).Databases(0) In the first line, the Dim statement defines the characteristics of the variable WorkDB. This variable has the characteristics of a database object. The second line performs the actual assignment, using the Set statement. The (0) within parentheses--it appears twice--is an offset index into the collection being referenced. Since VBA starts counting at zero (not one),

Workspaces(0) refers to the first Workspace object in the Workspaces collection. Likewise, Databases(0) refers to the first Database object in the Databases collection. After Access executes the second line, VBA knows that WorkDB refers to a physical entity, using the full name you specified (the name is constructed according to the rules defined in the previous section). Again, notice there is a distinct difference between the actual object (the part to the right side of the equal sign in the Set statement) and the object variable subsequently used to reference the object (WorkDB).

Chapter 20: Using Visual Basic for Applications
(This is section 3 of 6 in this chapter)

Creating a VBA Module
Now that you know how to put together a program, you are ready to give it a try. To create a VBA procedure, you follow many of the same steps you follow when you created macros. The general steps in VBA programming are as follows:
1. 2. 3. 4. 5. 6. Identify the task you want to accomplish. Plan the steps needed to accomplish that task. Create the programming code necessary to implement the steps. Test the program. Refine the program. Repeat steps 4 and 5 until the program works correctly.

Does this sound familiar? It should; with only minor variations it is the same process you learned in Chapter 18, "Creating and Using Macros." This is because the principles of programming apply whether you are working with the macro language or with VBA. You create VBA programming code by using the VBA Editor, which is described in the following section.

What is the VBA Editor?
You create VBA programs using the VBA Editor. To start the VBA Editor, first click your mouse on the Modules button in the Database window. Then, click your mouse on the New button. Access, in turn, displays the VBA Editor, as shown in Figure 20-1.

Figure 20-1 The VBA Editor. Notice that there are several different parts to the VBA Editor. In the upper left corner is what the editor refers to as the Project window. This is where you can see the different elements of your project and any modules that have been defined in the workbook. Just below the Project window is the Properties window. Here you can specify different attributes of whatever you have selected in the Project window. For most simple development needs, you will never do much with the Properties window. To the right of the Properties window, and at the very bottom of the screen, is the Immediate window. This is where you can either test parts of your procedures during development or you can find the immediate results of various commands. The Immediate window comes in very handy during testing and debugging, when they are necessary. Note: If a window is not displayed within the VBA Editor, you can display it by choosing one of the options from the View menu. For instance, if you want to display the Project window, choose Project Explorer from the View menu. The Module window, which is the largest window on the screen, is where you do your programming. At the top of the Module window are two drop-down lists. The one on the left is called the Object box. The one on the right is the Procedure box. You use the Object box to

select which object you want to work with. When you first create a module, the object is set to the word General, meaning you are working on a general module, not on one associated with a particular object in a form or report. The Procedure box is where you indicate the name of the procedure on which you want to work. If you choose or specify a different procedure in this box, the information Access shows in the Module window changes to reflect the VBA statements you have assigned to that procedure. The top level of a module is the Declarations section; It begins with the procedure name you indicate in the Procedure box when you first create a module. Take a look at the Module window (Figure 20-1). It contains the programming code already defined for the declarations section. In this instance, there is only one line of code defined--a statement that indicates the database's default sort order. To enter programming statements into a procedure, you type them in the Module window. As you enter information, Access checks to make sure it can understand what you type. In other words, Access checks the syntax of what you enter. You use the correct syntax when you follow the VBA rules of grammar. You can cut, copy, and paste sections of code using standard Windows mouse or keyboard techniques. You can perform these operations either in the same procedure or between different procedures.

Creating a New Procedure
Note: This chapter uses a version of the Customers.mdb database you developed in earlier chapters of the book. You should copy the Customer 8.mdb database from the companion CD-ROM to a directory on your hard drive and rename it Customer.mdb. You should do this rather than using any older versions of Customers.mdb, since the CD-ROM version has a new form in it that you will need. Assume for a moment that you want to create a procedure that will step through the records in the Business Customers table, which you developed earlier in this book. The procedure will check the ZIP Code of every record. If the ZIP Code equals a specific value, the procedure will change the record's salesperson. Your first step is to create a new procedure in the Module window. To do this, you must first decide if your procedure will be a subroutine or a function. Your choice will depend on the task you want the procedure to perform. Remember, if you want your procedure to return a value, you need to make it a function. You also need to determine a name for your procedure. Unlike the names you use for fields, tables, and other database objects, this procedure name cannot contain spaces. In this case, let's say you decide to use the name ChangeSalesByZip, which is quite descriptive. If you want to create a function by this name, you simply enter the following on a blank line in the Module window: Function ChangeSalesByZip() After you press the Enter key, Access changes the way the Module window looks, as shown in Figure 20-2.

Figure 20-2 The Module window for the new procedure. Notice that VBA provides two program lines. These statements define your function's starting point and ending point. You must place the programming lines that make up your function between these points. Notice, also, that Access lists your new function in the Procedures box at the top of the Module window. If you had many procedures in your module, you could display other procedures by clicking your mouse on the pull-down arrow at the right of the Procedures box and selecting another procedure name.

Specifying Parameters
Take a look at the parentheses after the function name (on the first line of the procedure). Inside these parenthesis, you specify arguments (parameters) for your function. Position the cursor between the parentheses, and change the line so it looks like this: Function ChangeSalesByZip(ZipWanted As String, NewSales As Long) This statement tells VBA that your function requires two arguments: ZipWanted and NewSales. The first parameter is a string, and the second is a long integer. Your function will use these arguments to determine which records to change and what to change them to. There is one other thing you need to do to this function's first line. You already know that you use functions when you want to return information to whoever called it. In this case, you want to return the number of records the function changes. You need to specify, on this first line, what type of value your function will return. Add As Long to the end of the line, so it looks like this:

Function ChangeSalesByZip(ZipWanted As String, NewSales As Long) As Long The As Long tells VBA that this function returns a long integer to the calling routine. With these declarations out of the way, you are ready to enter the body of your routine in between the first and last lines on your screen.

Specifying Variables
Your next step is to specify the variables Access will use in your routine. You already know you will be using the variables specified as parameters for the function, but there are other variables necessary, as well. For instance, you will need to use variables for counters, table names, and for objects within the database. Add the following lines to your function: Dim TableName As String Dim ChangeCount As Long TableName = "Business Customers" ChangeCount = 0 The first two lines declare two variables. The first variable (Tablename) will store the table name; the second variable (ChangeCount) will count the number of changed records. The third and fourth lines set the values for the variables. Next, add the following lines to your function: Dim CustDB As Database, CustTable As Recordset Set CustDB = DBEngine.Workspaces(0).Databases(0) Set CustTable = CustDB.OpenRecordset(TableName, dbOpenTable) These lines set up the object variables necessary to access your data. The first line declares two variables, one for the database and the other for the records in the database. The second line defines the database object. This Set statement uses an object address composed of the highest-level object (the Access database engine), the Workspaces collection, and the Databases collection. This verbiage will always return a reference to the currently open database. The third line builds on the object you defined in the second line. Using the CustDB object, the OpenRecordset method opens the table needed. At the end of these two lines, you can use CustDB to refer to the Customer.mdb database, and use CustTable to refer to the Business Customers table within that database. You have now finished the preliminaries of your function--the necessary part that sets up your data. Your Module window should look like what you see in Figure 20-3, and you are now ready to develop the main body of your function.

Figure 20-3 Part way through your function.

Stepping Through the Records
The next part of the function will step through the records in your database to determine if a change is necessary. If a change is necessary, the function makes the change and saves the update. Enter the following into your function: Do Until CustTable.EOF If CustTable![Zip Code] = ZipWanted Then CustTable.Edit CustTable!SalesPerson = NewSales CustTable.Update ChangeCount = ChangeCount + 1 End If CustTable.MoveNext Loop This code section uses a Do Loop structure. On the first line of the loop, you define a condition you want VBA to check. VBA repeats each of the statements between the Do Until and Loop statements until that condition is met. In this case, you want the function to check each record in your table, one record at a time. In other words, you want your function to begin with the first record and to end with the last. The last record, therefore, becomes your Do Loop's ending condition. Your function can use the EOF (end of file) condition to determine when it has used the last record. In this case, the code will repeat the statements until the EOF condition returns true.

The If statement checks to see if a record needs to be changed. On the If line, you define a condition you want to test: in this case, whether the ZIP Code of the table record is equal to the ZIP Code you want to change. If it is, VBA executes the lines between that point and the End If statement. If the ZIP Codes are not equal, VBA skips those lines. The statements between the If and End If statements update and save the record. First, the Edit method lets the function edit the data in the table. The next line changes the Salesperson field. Then, the Update method writes the changed information back to the table. The line after the Update method incrementally counts the number of records changed. VBA always executes the next line (CustTable.MoveNext), whether there was a change in the record or not. (Access executes this line because it is physically outside of the If/End If structure.) Using the MoveNext method, the code moves the table pointer to the next record in the table. When these lines of code complete, the function will have checked all of the records in the table. You are now ready to finish up your function.

Wrapping It Up
You are now at the end of your function, and you are ready to finish it up. To do so, enter the following lines: CustTable.Close CustDB.Close ChangeSalesByZip = ChangeCount The first two lines close the table and the database, respectively. The third line assigns the count of changed records to the function name. This line causes the function to return a value to wherever it was called from; without the statement, your function would always return a zero. The entire function is shown in Figure 20-4.

Figure 20-4 The finished function.

Saving Your Module
After you have finished your first procedure, you can create other procedures in this same module, if you desire. Otherwise, your next step is to save your module and close it. You save the module by clicking your mouse on the Save tool on the toolbar, or by choosing Save from the File menu. Access then asks you for the name you want to use for your module. Enter a name, such as MyModule, and then press ENTER or click your mouse on OK. Access will save the module as a part of the database. Now you can close your module by clicking your mouse on the Close icon (the X) in the upperright corner of the VBA Editor program window. (You can also choose Close and Return to Microsoft Access from the File menu, if desired.) Access closes the VBA Editor and again displays the Database window, as shown in Figure 20-5.

Figure 20-5 The updated Database window.

Chapter 20: Using Visual Basic for Applications
(This is section 4 of 6 in this chapter)

Testing Your Procedure
Once you have created a procedure, you need to test it out. VBA provides several different tools you can use to do this. These tools include:

• • •

The Immediate window Debug.Print Breakpoints

Using the Immediate Window
Perhaps the most important tool you can use to test your procedures is the Immediate window. This window lets you see the results of your procedures right away. Using the window, you can uncover problems before they get buried among dozens of different procedures you may develop. For example, you can test your functions to see what values they are returning. Remember that the Immediate window appears at the bottom of the VBA Editor workspace. (If it is not visible for some reason, you can display it by choosing Immediate Window from the View menu.) You can select the window by clicking your mouse in the window. Access displays

a cursor and then waits for your command. You can switch to another VBA Editor window by simply clicking the mouse in the desired window. Later, you can hop back to the Immediate window by clicking your mouse within it. (This is the same process you use to select different windows anywhere in the Windows environment.) To use the Immediate window to test out a procedure, simply type a question mark followed by the name of the procedure. For example, if you want to test out the function developed in the previous section, enter the following: ?ChangeSalesByZip("43772", 3) As soon as you press ENTER, Access executes the procedure. Remember--this function will change the information in your database, so be careful with how you use it. When the function returns, it prints the number of records changed, as shown in Figure 20-6.

Figure 20-6 Using the Immediate window. To display a variable's value, type in the name of the variable preceded by a question mark. For example, if you want to display the contents of the MyVar variable (assuming you have such a variable defined), type in the following: ?MyVar You should note that before a procedure is executed, the value of all variables is undefined, meaning you cannot print them. In addition, after a procedure is executed, VBA wipes out the contents of all variables. This means that the only time you can meaningfully print the value assigned to a variable is while a procedure is executing. Typically, this is done after a breakpoint is reached, as discussed shortly.

Using Debug.Print
Another way you can use the Immediate window is with Debug.Print. VBA lets you use Debug.Print to display the contents of any variable, as your program runs. Using Debug.Print, you can watch closely a variable's value to see how your code changes the variable. By tracking a variable's value in this way, you may detect errors in your code. To display a variable value in this way, you include Debug.Print in a line of code. For example, if you want to determine the value of the TempStore variable, you can use the following line of code: Debug.Print "Value of TempStore is " & TempStore If TempStore contains a value of 4, when VBA executes this line, Access will display the following in the Immediate window:

Value of TempStore is 4 Debug.Print has no other effect on your program; everything else remains the same. Debug.Print is particularly useful when you're debugging, since you don't have to bother with stopping and restarting your program.

Setting Breakpoints
Another testing tool which VBA provides for you is a breakpoint--a place you specify within your program where you want the program's execution to stop. Once your program stops, you can use the Immediate window to display information about variables or to test out other procedures, if you desire. VBA lets you set breakpoints on any line of code in your program or within any procedure of your program. To set a breakpoint, position the cursor on the program line where you want VBA to stop. Then, select the Toggle Breakpoint option from the Debug menu or click your mouse on the toolbar Breakpoint tool. VBA highlights the program line to indicate it has set the breakpoint. (Figure 20-7 shows an example of a breakpoint in a program.)

Figure 20-7 A breakpoint within a program.

When you later run the program and VBA encounters the line at which you have set the breakpoint, VBA will do three things:

• • •

Pause the program. Display your program (with the line containing the breakpoint) in the program window. Wait for your command.

Normally, you set breakpoints so you can inspect the status of the program while it is executing. This is the time when you can print out the contents of variables, as discussed earlier in this chapter. You can also use other VBA menu commands (those under the the Debug menu) to step through your procedure one instruction at a time. If you are done inspecting the program, you can also choose the Run command from the Run menu. This causes the procedure to continue executing as if no breakpoint had been encountered. To later remove a breakpoint, stop your program and position the cursor on the line that contains the breakpoint. Then, perform any of the actions you used to first set the breakpoint: select the Toggle Breakpoint option from the Debug menu or click your mouse on the toolbar Breakpoint tool. This removes the breakpoint, which you can see because VBA removes the highlight from the program line. Note: It is not unusual when you are testing your program to have several different breakpoints set. If you want to remove all the breakpoints in your program at the same time, you can do so by choosing the Clear All Breakpoints option from the Debug menu.

Chapter 20: Using Visual Basic for Applications
(This is section 5 of 6 in this chapter)

Attaching Your Procedure to a Form
You now know how to create procedures you can use in your database. You will now want to attach your procedure to a form so a user can click their mouse on a command button to execute the procedure. The steps you must perform to attach your procedure to a form are similar to those you used in Chapter 18 where you attached a macro to a form. The first step is to load the form from which you want to use a subroutine. For example, display the form Design window for the Review Accounts form, as shown in Figure 20-8.

Figure 20-8 The form you want to use. Select the Command button and display the Event tab of the Properties dialog box. Scroll through the properties until you see the event you want to trigger the VBA code. In this case, use the On Click event. If you use the pull-down arrow to the right of this property box, you will notice that you have two choices:

• •

Event procedure. This is a VBA procedure which you develop to run in response to this event. Display Ancillary. This is a macro stored in the Customers database. If you had other macros defined in the database, they would be listed, as well.

You want to run an event procedure, so make that selection and click your mouse on the Builder icon to the right of the property box. Access will then display the VBA Editor with a Module window specific to this Command button, as shown in Figure 20-9.

Figure 20-9 The Module window for the Command button. If you click your mouse on the pull-down arrow to the right of the toolbar Object box, you will find that you can pick any other object in the form. If you click your mouse on the pull-down arrow to the right of the toolbar Procedures box, you will find you can pick a number of predefined procedures. Each of these procedures represents a specific event that can occur in relation to the object in the Object box. With the current settings in the Procedure and Objects boxes, you will develop a procedure that VBA executes when someone clicks their mouse on the Change Reps object (which is the button on the form). At this point, you need to develop a procedure that will ask the user for a ZIP Code and a new salesperson number. To do so, enter the subroutine shown in Figure 20-10.

Figure 20-10 The subroutine to call the ChangeSalesByZip function. This procedure uses the InputBox function to display two questions to the user. The function first asks for a ZIP Code and then for a replacement salesperson ID. With this information available, the code invokes the ChangeSalesByZip function. When the function is complete, the code displays a message box to indicate the outcome of the change. After you type in this subroutine, click your mouse on the Save tool and then close the VBA Editor. When you subsequently use the Review Accounts form, the Change Reps button will be active. If you click your mouse on it, you will see the first of the two questions, as shown in Figure 20-11.

Figure 20-11 Being prompted for a ZIP Code. After you answer this question, the code will ask you for a new salesperson number. After you type in your response, your program will update the records in the table.

Chapter 20: Using Visual Basic for Applications
(This is section 6 of 6 in this chapter)

Summary
VBA provides a complete programming language you can use to create event-driven programs for your databases. Using VBA is similar to using other versions of Basic for the Windows environment, such as Visual Basic. In this chapter, you learned the bare essentials of programming in VBA. Before you move on to Chapter 21, make sure you understand the following key concepts:

• • • • • • • •

VBA is a programming language based on older versions of Basic. A VBA program is made up of procedures; a procedure is a series of programming lines designed to accomplish a particular task. There are two types of procedures: functions and subroutines. The difference between the two is that functions are designed to return a value, and subroutines are not. A VBA program can contain many different components, including statements, variables, operators, and functions. When programming, you combine these elements to accomplish a task. In VBA, you can use a hierarchical addressing system to address database objects and collections of objects. The VBA Editor is where you develop VBA programs. To display the VBA Editor, you first click your mouse on the Modules button in the Database window and then click on New. VBA provides several tools you can use to help test your programs. These include the Immediate window, Debug.Print, and breakpoints. You can develop event procedures as part of your forms and reports. You develop an event procedure in much the same way as a regular procedure.

If you are interested in programming in VBA, the best thing to do is to simply try things out. You will also probably want to get a hold of a good book on programming either in VBA or Visual Basic. Chapter 21 is the last chapter in Learn Access Now. In it, you will learn how to put together an application which utilizes everything you have learned in this book to this point.

Chapter 21: Putting It All Together
(This is section 1 of 7 in this chapter)

Congratulations. You now know everything you need to know to put Access to work for you. The previous chapters in this book have taught you everything from starting Access to working with OLE to using VBA. In this chapter, all that information will be brought together, in one place, as you review the design and implementation of a full-fledged database system. This chapter is different from previous chapters in that it is not necessarily designed to teach you anything, other than what you may learn by reviewing someone else's work. Note: The database described in this section is include on the companion CD-ROM as CD Collection.mdb. The sound snippets included in the database are also stored in the Sounds folder on the CD-ROM.

Database Design
The database developed for this chapter provides a way to catalog and manage an audio CD collection. When I first looked at developing this database, I determined that I needed to maintain the following information:

• • • • • •

Song title CD name Track number Length Group Notes

In addition, I wanted the capability to keep a ten-second sound clip for each song in the collection. With this as my starting point, my next task was to determine how I could minimize repetitious data in the database. If the database kept a record for each song in a CD collection, two fields (Group and CD Name) immediately presented themselves as potentially repetitious. To get rid of the repetition, I made these fields the basis for other tables in the database. Thus, I decided upon three tables:

• • •

The CD Table (CD Collection). Holds the title and related information for each CD. The Song Table (Individual Tracks). Holds the individual songs from each CD. The Groups Table. Holds information about musical groups.

My next step was to examine each table and determine the information stored in each.

The CD Table
I intended the CD table to hold information related to an entire CD: the name, group, date of release, and so on. This information would constitute the main table in the database. I decided to name this table CD Collection. The table ended up having the layout shown in Table 21-1.

Field Disc ID Group ID Title

Type AutoNumb er Number Text

Lengt h

Long 40

Recording Label Year Released Number of Tracks Date Purchased Purchase Price Note

Text Number Number Date/Time Currency Memo

35 Integer Byte

Table 21-1 Layout for the CD Collection table. The Disc ID field, which is a AutoNumber, determines a unique identification number for each CD in the collection and serves as the primary key for the table. In addition, the Individual Tracks table uses the Disc ID field to track records to each individual CD record. If you desire, you could label each physical CD in your collection with the Disc ID number, which makes cataloging your CDs easier. I picked the other fields in the table in a brainstorming session. None of these fields are mandatory; I included many of them simply because I felt this was information which would be "nice to maintain." Note: I don't claim that my database design is the best possible design, or the only design that will work. Determining what information to keep in a database is a very subjective matter, and you may decide to keep entirely different information in your database. The Group ID field ties into the Groups table, described in a moment. I used this field to indicate what group was responsible for which CD.

The Song Table
The Song table holds information about individual tracks on each of the CDs. This information includes the song title, group, length, and a sample sound clip. I named this table Individual Tracks, and it has the layout shown in Table 21-2.

Field Disc ID Track Number Song Title Group ID Length Notes Sample Sound Clip

Type Number Number Text Number Date/Tim e Memo OLE Object

Lengt h Long Byte 50 Long

Table 21-2 Layout for the Individual Tracks table. The Disc ID field ties the Individual Tracks table to the CD Collection table. I used this field to indicate the CD that an individual song belongs to. Likewise, the Group ID field ties into the field of the same name in the Groups table (described in the next section). You will use this field when you want to search songs by group.

The Groups Table
The Groups table keeps track of information related to a musical group or individual singer. Of the three tables, the Groups table contains the least information. It does, however, contain the group name, when the group was formed, and where the group is from. The layout for this table is shown in Table 21-3.

Field Group ID Group Name Date Formed Where From Notes

Type AutoNumb er Text Date/Time Text Memo

Lengt h

50

40

Table 21-3 Layout for the Groups table. The Group ID field is a AutoNumber that uniquely identifies every group in the table. This field ties into both of the other tables in the database.

Establishing Relationships
With each of these tables in place, you are ready to establish relationships between the tables. You establish table relationships as described in Chapter 12, "Understanding Data Relationships." In this case, the linking fields and their purposes are:

• •

Disc ID. Both the CD Collection table and the Individual Tracks table use this field. It is a one-tomany relationship, with the CD Collection table being the "one" side. Group ID. This field forms two relationships between three tables. In both instances, the field forms a one-to-many relationship, with the Groups table being the "one" side. Relationships exist between the Groups table and the CD Collection table and between the the Groups table and the Individual Tracks table.

After you establish these relationships, the Relationships window should look similar to what is shown in Figure 21-1.

Figure 21-1 The Relationships window.

Chapter 21: Putting It All Together
(This is section 2 of 7 in this chapter)

Forms in the Database
In designing forms for the CD database, my goal was to make using the system as simple as possible. Therefore, I decided to use a two-pronged approach. First, I developed a switchboard menu form; I would later start this form with an AutoExec macro. (An AutoExec macro is one that automatically executes when a databases is first opened.) This form has four simple choices on it, as follows:

• • • •

Add Music Review Music Print CD List Exit

I implemented these four choices using command buttons, tied to macros which run the appropriate form or report. Figure 21-2 shows how my Startup form appears; this is the form that implements the switchboard menu.

Figure 21-2 The Startup form. On my Startup form, I placed four command buttons to run macros that would allow me to use other database objects. (The next four sections describe the actions that were tied to each of the command buttons.) My form also includes an unbound OLE object: a picture of a guitar. This picture is a GIF graphic file that I inserted in an OLE frame. Note: The term "unbound OLE object" may sound foreign. When something is described as "unbound" in Access, it simply means it has not been associated with a data source. In this case, the OLE object is unbound because it is not meant to be associated with data, but with a non-changing picture.

Add Music
When I click my mouse on the Add Music command button, the Add CDs macro runs. This macro has only two actions, OpenForm followed by GoToRecord. The OpenForm action opens the Music Input form in Edit mode. The GoToRecord action then jumps to a brand new record for the form. The Add Music option and the Review Music option both use the Music Input form. I built the form around the Sorted Collection query, described later in this chapter. The form consists of two major parts: information from the CD Collection table (through the Sorted Collection query) and information from the Individual Tracks table. I implemented the form using a form and subform arrangement, as shown in Figure 21-3.

Figure 21-3 The Music Input form.

The Subform
The subform is a direct window into the Individual Tracks table. The form displays only those tracks which correspond to the Disc ID. The definition of the subform is in the Track List form. This form displays four fields from the Individual Tracks table, even though another field is defined on the form as a hidden field. Figure 21-4 shows what this form looks like. (This form is shown in its Design window so that you can see the hidden field.)

Figure 21-4 The Design window for the Track List form. If you try to use this form by itself, the form does not work as you would want it to--instead, it simply displays all the tracks in the Individual Tracks table. I designed the form for use with the Music Input form where it is set up to display only those tracks for a particular CD. The Track List form is based on the Sorted Tracks query, described later in this chapter.

Adding Groups
Notice that the form uses a Combo Box object for the musical group. If you click your mouse on the pull-down arrow, you will see a list of groups which have been defined in the database. These are, of course, saved in the Groups table. To select an existing group, you simply select it from the list. To add a group (or groups), you click your mouse on the Add Group command button. When you do, Access executes a macro called Click on Add Group. This macro is very similar to the Add CDs macro. It contains two actions: OpenForm and GoToRecord. However, Access opens the Add Groups form this time. This form allows you to add records to the Groups table. Figure 21-5 shows what this form looks like.

Figure 21-5 The Add Groups form. When you close this form, Access again displays the Music Input form. Access makes any groups you added with the Add Groups form immediately available in the Combo Box object.

Review Music
When you click your mouse on the Review Music command button, Access executes the Review CDs macro. This single-line macro opens the Music Input form, described earlier in the chapter. The only difference is that the macro now opens the form with the Data Mode argument set to Read Only. This means that by using this menu option, you can only review the information in the database. Figure 21-6 shows how the Music Input form appears when opened read only.

Figure 21-6 The Music Input form with a record displayed. Notice that the subform, which displays information from the Individual Tracks table, is filled in at the bottom of the screen. If you double-click your mouse on the OLE icon in the Clip column, a music clip plays. When you double-click your mouse on an OLE icon, the macro called Play OLE Clip executes. This macro uses the RunCommand action to play the sound clip.

Print CD List
The Print CD List option does not display a form, but rather prints a report that lists your CDs. The macro itself, called Print Report, is very similar to the conditional macro you developed in Chapter 18, "Creating and Using Macros." The macro first checks to see if there are any records in the Individual Tracks table. If there are not, then the macro ends. Otherwise, the macro proceeds to the next step. The first two macro actions turn screen echoing off and display the mouse pointer as an hourglass. Then the OpenReport action prints the report named Collection Report (the Collection Report is described fully later in this chapter). After the report is printed, the screen updating turns back on and the mouse pointer returns to normal.

Exit
The Exit command button ties to a macro called Exit, which uses the Close action to close the open form. When you select the Exit button, the current form is the Startup form, which contains the switchboard menu. As such, Access closes your access to the database.

Sign up to vote on this title
UsefulNot useful