Queries That Select Records

Một phần của tài liệu access 2010 the missing manual (Trang 43 - 71)

1 Creating Your First

Database

Although Microsoft won’t admit it, Access can be intimidating—intimidating enough to trigger a cold sweat in the most confident office worker. Even though Microsoft has spent millions of dollars making Access easier to use, most people still see it as the most complicated Office program on the block. They’re probably right.

Access seems more daunting than any other Office program because of the way that databases work. Quite simply, databases need strict rules. Other programs aren’t as obsessive. For example, you can fire up Word, and start typing a letter straightaway.

Or you can start Excel, and launch right into a financial report. But Access isn’t nearly as freewheeling. Before you can enter a stitch of information into an Access database, you need to create that database’s structure. And even after you’ve defined that structure, you’ll probably want to spend more time creating other useful tools, like handy search routines and friendly forms that you can use to simplify data look- up and data entry. All of this setup takes effort and a good understanding of how databases work.

In this chapter, you’ll conquer any Access resistance you have, and learn to create a simple but functional database. Along the way, you’ll get acquainted with the slick Access user interface, and you’ll learn exactly what you can store in a database. You’ll then be ready to tackle the fine art of database design, which is covered in detail throughout this book.

Understanding Access Databases

FREQUENTLY ASKED QUESTION

Using Someone Else’s Database

Can I use an Access database I didn’t design?

Although every database follows the same two-step pro- cess (first somebody creates it, and then people fill it with information), the same person doesn’t need to perform both jobs. In fact, in the business world, different people often work separately on these two tasks.

For example, a summer student whiz-kid at a beer store may build a database for tracking orders (task #1). The sales department can then use the database to enter new orders (task #2), while other employees look up orders and fill them (also task #2). Warehouse staff can make sure

stock levels are OK (again, task #2), and the resident ac- countant can keep an eye on total sales (task #2).

If task #1 (creating the database) is done well, task #2 (using the database) can be extremely easy. In fact, if the database is well designed, people who have little under- standing of Access can still use it to enter, update, and look up information. Amazingly, they don’t even need to know they’re running Access at all!

You’ll learn more about sharing Access with groups of people in Chapter 19.

Understanding Access Databases

As you already know, a database is a collection of information. In Access, every da- tabase is stored in a single file. That file contains database objects, which are simply the components of a database.

Database objects are the main players in an Access database. Altogether, you have six different types of database objects:

Tables store information. Tables are the heart of any database, and you can cre- ate as many tables as you need to store different types of information. A fitness database could track your daily running log, your inventory of exercise equip- ment, and the number of high-protein whey milkshakes you down each day, as three separate tables.

Queries let you quickly perform an action on a table. Usually, this action in- volves retrieving a choice bit of information (like the 10 top-selling food items at Ed’s Roadside Diner or all the purchases you made in a single day). However, you can also use queries to apply changes.

Forms are attractive windows that you create, arrange, and colorize. Forms pro- vide an easy way to view or change the information in a table.

Reports help you print some or all of the information in a table. You can choose where the information appears on the printed page, how it’s grouped and sort- ed, and how it’s formatted.

Macros are mini-programs that automate custom tasks. Macros are a simple way to get custom results without becoming a programmer.

Modules are files that contain Visual Basic code. You can use this code to do just about anything—from updating 10,000 records to firing off an email.

Access gurus refer to all these database ingredients as objects because you manage them all in essentially the same way. If you want to use a particular object, then you add it to your database, give it a name, and then fine-tune it. Later on, you can view your objects, rename them, or delete ones you don’t want anymore.

Note: Designing a database is the process of adding and configuring database objects. For those keeping score, an Access database can hold up to 32,768 separate objects.

In this chapter, you’ll consider only the most fundamental type of database object:

tables. But first, you need to create a blank database you can work with.

Getting Started with Your First Database

When you start Access, you begin in backstage view, with the New command already selected. From there, you’re just a few clicks away from generating a database of your very own.

In this chapter, you’ll slap together a fairly straightforward database. The example is designed to store a list of prized bobblehead dolls. (For those not in the know, a bob- blehead doll is a toy figure with an oversized head on a spring, hence the signature

“bobbling” motion. Bobblehead dolls usually resemble a famous celebrity, politician, athlete, or fictional character.)

Tip: You can get the Bobblehead database, and all the databases in this book, on the Missing CD page at www.missingmanuals.com/cds.

Here’s how to create a blank new database:

1. If you’re currently working with a database, choose FileNew. If you’ve just started Access, you’re there already.

The Access window now has three columns (Figure 1-1). At left is a narrow strip of backstage commands. In the middle section, you choose the type of database you want. At far right is an optional picture that previews your choice, a text box for you to pick the file name, and the all-important Create button.

2. At the far right, in the File Name box, type a file name for the database you’re about to create.

Access stores all the information for a database in a single file with the exten- sion .accdb (which stands for “Access database”). Don’t stick with the name Ac- cess picks automatically (like “Database1.accdb”). Instead, pick something more suitable. In this example, Bobblehead.accdb does the trick.

As with any other file, Access files can contain a combination of letters, spaces, numbers, parentheses, hyphens (-), and the underscore (_). It’s generally safest to stay away from other special characters, some of which aren’t allowed.

Getting Started with Your First Database

Figure 1-1:

This database will be named Bobblehead.accdb.

As you can see by the file path below the File Name box, it will be saved in the folder C:\Users\

matthew\Documents.

You can edit the file name by typing in the File Name box, and you can browse to a different folder by clicking the folder icon.

Note: Depending on your computer settings, Windows may hide file extensions. Instead of seeing the Access database file MyScandalousWedding.accdb in file-browsing tools like Windows Explorer, you may just see the name MyScandalousWedding (without the .accdb part on the end). In this case, you can still tell the file type by looking at the icon. If you see a small Access icon (which looks like a key) next to the file name, that’s your signal that you’re looking at an Access database.

3. Choose a folder.

Like all Office programs, Access assumes you want to store every file you create in your personal Documents folder. If this isn’t what you want, click the folder icon to show the File New Database dialog box, browse to the folder you want (Figure 1-2), and then click OK.

4. In the middle of the window, choose “Blank database”.

When you choose to create a blank database, that’s exactly what you get—a new, empty database file with no tables or other database objects. Starting from scratch is the best way to learn about Access. It’s also the favorite choice of data- base experts, who prefer to create everything themselves so it’s exactly the way they like it. That said, you can click one of the other pictures under the Available Templates heading to build a database based on a template. The box on page 28 has more about this approach.

Figure 1-2:

The File New Data- base dialog box lets you choose where you’ll store a new Access database file.

It also gives you the option to create your database in the format used by older versions of Access (.mdb), instead of the more modern format used by Access 2010 and Access 2007 (.accdb).

To do so, you need to choose either the 2000 or 2002-2003 format options from the “Save as type” list, as shown here.

POWER USERS’ CLINIC

Telling Access Where to Store Your Databases

Access always assumes you want to store databases in your Documents folder. And though you can choose a different location every time you save or open a database, if there’s another folder you need to visit frequently, then it makes sense to make that your standard database storage loca- tion. You can configure Access to use this folder with just a few steps:

1. Choose File➝Options. The Access Options window appears.

2. In the list on the left, choose General.

3. In the page on the right, look for the “Creating da- tabases” heading. Underneath, you’ll find a “Default database folder” text box. Type the path to the folder you want to use (like C:\MyDatabases), or click Browse to navigate to it.

When you’re finished, click OK to save your changes.

5. Click the big Create button (under the File Name box).

Access creates your database file and then shows a datasheet where you can get to work creating your first table.

Once you create or open a database, the Access window changes quite a bit. An impressive-looking toolbar (the ribbon) appears at the top of your screen, and a navigation pane shows up on the left. You’re now in the control center where you’ll perform all your database tasks (as shown in Figure 1-3).

Getting Started with Your First Database

Figure 1-3:

The navigation pane on the left lets you see dif- ferent items (or objects) in your database. You can use the navigation pane to jump from a list of products to a list of customers and back again. The ribbon along the top groups together every Access command. This ribbon is the mission control that lets you perform various tasks with your database. The document window in the middle takes up the rest of the window. This window is where you’ll do your work, such as designing tables and entering data.

The ribbon

The navigation pane

The document window

UP TO SPEED

Templates: One Size Fits Some

The example in this section shows you how to create a blank database. However, you’ve probably noticed a num- ber of other options in the Available Templates section in the middle of the File➝New page. These options let you browse dozens of prebuilt databases, which are known as templates. Templates aim to save you the work of creating a new database and let you jump straight to the fine-tuning and data-entry stage.

As you might expect, there’s a price to be paid for this con- venience. Even if you find a template that stores the type of information you want to track, you might find that the predefined structure isn’t quite right. For example, if you choose to use the Home Inventory template to track all

the stuff in your basement, you might find that it’s miss- ing some information you want to use (like the projected resale value of your stuff on eBay) and includes other de- tails you don’t care about (like the date you acquired each item). To make this template work, you’ll need to change the design of your table, which involves the same Access know-how as creating one.

In this book, you’ll learn how to build your own databases from the ground up and customize every square inch of them. Once you’re an Access master, you can spend many fun hours playing with the prebuilt templates and adapting them to suit your needs.

The Introduction covers the basics of how the ribbon works. (Jump to page 8 for the full details.) Now, it’s time to consider how you can make use of your brand-new, empty database by adding a table.

Note: Keen eyes will notice that the title bar of the Access window has the text “Access 2007” in it. (For example, if you created a database named Bobblehead, you’ll see the text “Bobblehead: Database (Access 2007).” Don’t worry, you don’t have a bum copy of Access. The 2007 simply denotes that you’re using the modern format of database, which was was introduced in Access 2007 and is still used in Access 2010. As you’ll discover in later chapters, there are a few new Access 2010 features that can break this compatibility, so your database won’t work completely in Access 2007. But even in this situation, the title bar still says “Access 2007”.

Understanding Tables

Tables are information containers. Every database needs at least one table—without it, you can’t store any data. In a simple database, like the Bobblehead database, a single table (which we’ll call Dolls) is enough. But if you find yourself wanting to store several lists of related information, then you need more than one table. In the database Big- BudgetWedding.accdb, you might want to keep track of the guests that you invited to your wedding, the gifts that you requested, and the loot that you actually received. In Chapter 5, you’ll see plenty of examples of databases that use multiple tables.

Figure 1-4 shows a sample table.

Figure 1-4:

In a table, each record occupies a separate row. Each field is rep- resented by a separate column. In this table, it’s clear that you’ve added six bobblehead dolls. You’re storing information for each doll in five fields (ID, Character, Manufac- turer, PurchasePrice, and DateAcquired).

A record

The name of

the table A field named Character

Before you start designing this table, you need to know some very basic rules:

A table is a group of records. A record is a collection of information about a single thing. In the Dolls table, for example, each record represents a single bobblehead doll. In a Family table, each record would represent a single relative. In a Products table, each record would represent an item that’s for sale. You get the idea. When you create a new database, Access starts you out with a new table named Table1, although you can choose a more distinctive name when you decide to save it.

Each record is subdivided into fields. Each field stores a distinct piece of in- formation. For example, in the Dolls table, one field stores the person on whom the doll is based, another field stores the price, another field stores the date you bought it, and so on.

Getting Started with Your First Database

Tables have a rigid structure. In other words, you can’t bend the rules. If you create four fields, every record must have four fields (although it’s acceptable to leave some fields blank if they don’t apply).

Newly created tables get an ID field for free. The ID field stores a unique number for each record. (Think of it as a reference number that will let you find a specific record later on.) The best part about the ID field is that you can ignore it when you’re entering a new record. Access chooses a new ID number for you and inserts it in the record automatically. You’ll learn much more about ID fields on page 77.

UP TO SPEED

Database Planning for Beginners

Many database gurus suggest that before you fire up Access, you should decide exactly what information you want to store by brainstorming. Here’s how it works. First, determine the type of list you want by finishing this sen- tence “I need a list of….” (One example: “I need a list of all the bobblehead dolls in my basement.”)

Next, jot down all your must-have pieces of information on a piece of paper. Some details are obvious. For example, for the bobblehead doll collection, you’ll probably want to keep track of the doll’s name, price, and date you bought

it. Other details, like the year it was produced, the company that created it, and a short description of its appearance or condition may require more thought.

Once you’ve completed this process and identified all the important bits of data you need, you’re ready to create the corresponding table in Access. The bobblehead doll exam- ple demonstrates an important theme of database design:

First you plan the database, and then you create it using Access. In Chapter 5, you’ll learn a lot more about planning more complex databases.

Creating a Simple Table

When you first create a database, it’s almost empty. But in order to get you started, Access creates your first database object—a table named Table1. The problem is, this table begins life completely blank, with no defined fields (and no data).

If you followed the steps to create a new database (page 25), you’re already at the Datasheet view (Figure 1-4), which is where you enter data into a table. All you need to do is customize this table so that it meets your needs.

There are two ways to customize a table:

Design view lets you precisely define all aspects of a table before you start us- ing it. Almost all database pros prefer Design view, and you’ll start using it in Chapter 2.

Datasheet view is where you enter data into a table. Datasheet view also lets you build a table on the fly as you insert new information. You’ll use this approach in this chapter.

The following steps show you how to turn a blank new table (like Table1) into the Dolls table using the Datasheet view:

1. To define your table, you need to add your first record.

In this case, that means choosing a bobblehead doll to add to the list. For this example, you’ll use a nifty Homer Simpson replica.

Note: It doesn’t matter which doll you enter first. Access tables are unsorted, which means they have no underlying order. However, you can sort them any way you want when you need to retrieve information later on.

2. In the datasheet’s rightmost column, under the “Click to Add” heading, type the first piece of information for the record (see Figure 1-5).

Based on the simple analysis you performed earlier, you know that you need to enter four fields of information for every doll. For the Homer Simpson doll, this information is “Homer Simpson” (the name), “Fictional Industries” (the manu- facturer), “$7.99” (the price), and today’s date (the purchase date). Although you could start with any field, it makes sense to begin with the name, which is clearly an identifying detail.

Figure 1-5:

To fill in your first record, start by entering something in the first field of information (like the doll name “Homer Simpson”). Then, hit Tab to jump to the second column, and then enter the second piece of information.

Ignore the ID column for now—Access adds that to every table to identify your records.

Start entering info here. . . . . .then enter the next piece of info here

3. Press Tab to move to the next field, and return to step 2.

Repeat steps 2 and 3 until you’ve added every field you need, being careful to put each separate piece of information into a different column.

Một phần của tài liệu access 2010 the missing manual (Trang 43 - 71)

Tải bản đầy đủ (PDF)

(832 trang)