Access is a database program. It is used primarily for storing data and giving easy access to those data. A basic Access database stores information about like items with common attributes in tables. Each item in an Access table is represented by a single record with a number of data fields containing descriptive information about that item. For example, if you were to create a database of books ordered for a library, you might end up with a table like the example below Note that:
How to Get Started on an Access Database
Note that when any data type is selected, it opens up a table at the bottom of the Access window, in the “Field Properties” box (see example, below). This table gives a variety of options for customizing how your field works with the data input (for example, you can choose a date/time format or a currency type in the properties for fields of those types.) If you wish any of your fields to require a value, find the “Required” row on its field properties, and select “Yes” from the drop-down menu. This is often helpful in preventing users from forgetting to enter essential data.
Entering Data in an Access table
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 straight away. 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 lookup 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. As you already know, a database is a collection of information. In Access, every database is stored in a single file. That file contains database objects, which are 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 create 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 equipment, 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 involves 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 provide 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 sorted, 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, 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. 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. When you start Access, you begin at the welcome page. 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. This example is designed to store a list of prized bobblehead dolls. (For those not in the know, a bobblehead 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.) Here’s how to create a blank new database:
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 (Figure 1-4). Figure 1-4. The navigation pane on the left lets you see different 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. If you haven’t used the ribbon before (either in Access or in another Office program), the Introduction covers the basics of how the ribbon works. Otherwise, carry on to the next section, where you’ll learn how to add a table to your brand-new, empty database. 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, you need more than one table. In the database BigBudgetWedding.accdb, you may 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-5 shows a sample table. Figure 1-5. In a table, each record occupies a separate row. Each field is represented by a separate column. In this table, it’s clear that you’ve added five bobblehead dolls. You’re storing information for each doll in five fields (ID, Character, Manufacturer, PurchasePrice, and DateAcquired). Before you start designing this table, you need to know some very basic rules:
When you first create a database, it’s almost empty. But 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 in the previous section to create a new database, you’re already at the Datasheet view (Figure 1-5), which is where you enter data into a table. All you need to do is customize this table so that it meets your needs. You can customize a table in two ways:
The following steps show you how to turn a blank new table (like Table1) into the Dolls table by using the Datasheet view:
Technically, you don’t need to save your table right away. Access prompts you to save it when you close the datasheet (by clicking the X at the document window’s top-right corner), or when you close Access. As you can see, creating a simple table in Access is almost as easy as laying out information in Excel or Word. If you’re itching to try again, you can create another table in your database by choosing Create→Tables→Table from the ribbon. But before you get to that stage, it makes sense to take a closer look at how you edit your table. You now have a fully functioning (albeit simple) database, complete with one table, which in turn contains one record. Your next step is filling your table with useful information. This often-tedious process is data entry. To fill the Dolls table, you use the same datasheet you used to define the table. You can perform three basic tasks:
You’ll probably spend a lot of time working with the datasheet. So settle in. To make your life easier, it helps to understand a few details. As you already know, you can use the arrow keys to move from field to field or row to row. However, you may have a bit of trouble editing a value. When you start typing, Access erases any existing content. To change this behavior, you need to switch into Edit mode by pressing F2; in Edit mode, your typing doesn’t delete the stuff that’s already in that field. Instead, you get to change or add to it. To switch out of Edit mode, you press F2 again. Figure 1-10 shows a close-up look at the difference. Figure 1-10. Top: Normal mode. If you start typing now, you’ll immediately erase the existing text (“Hobergarten”). The fact that all the text in the field is selected is a big clue that you’re about to wipe it out. Bottom: Edit mode. The cursor shows where you’re currently positioned in the current field. If you start typing now, you’ll insert text in between “Hober” and “garten.” Edit mode also affects how the arrow keys work. In Edit mode, the arrow keys move through the current field. For example, to move to the next cell, you need to move all the way to the end of the current text, and then press the right arrow key again. But in Normal mode, pressing the arrow keys always moves you from cell to cell. Power users know the fastest way to get work done is to use tricky keyboard combinations like Ctrl+Alt+Shift+*. Although you can’t always easily remember these combinations, a couple of tables can help you out. Table 1-1 lists some useful keys that can help you whiz around the datasheet. Table 1-1. Keys for Moving Around the Datasheet
Table 1-2 lists some convenient keys for editing records. Table 1-2. Keys for Editing Records
Figure 1-11. An Access user has been on an eBay buying binge and needs to add several doll records. With a quick Ctrl+” keystroke, you can copy the date from the previous record into the DateAcquired field of the new record. Access, like virtually every Windows program, lets you cut and paste bits of information from one spot to another. This trick is easy using just three shortcut keys: Ctrl+C to copy, Ctrl+X to cut (similar to copy, but the original content is deleted), and Ctrl+V to paste. When you’re in Edit mode, you can use these keys to copy whatever you’ve selected. If you’re not in Edit mode, the copying or cutting operation grabs all the content in the field. Unlike other programs, Access doesn’t require that you save your data. It automatically saves any edits you make to the records in a table. This automatic-saving process takes place every time you change a record, and it happens almost instantaneously. It also takes place behind the scenes, and you probably won’t notice anything. But don’t be alarmed when you exit Access and it doesn’t prompt you to save changes, as any change to your data is saved the moment you make it. The rules are a bit different for database objects (Understanding Access Databases). When you add or edit a database object, Access waits until you finish and close the object, at which point it prompts you to save or discard your changes. If you’re a bit paranoid and you can’t stand the wait, just click the tiny Save icon in the Quick Access toolbar in the top-left corner of the window (it looks like a floppy disk) to save the current database object immediately. Remember, when you click File, you enter Backstage view, which provides a narrow strip of commands (on the left) and a page with options for the currently selected command (on the right). You use Backstage view to open, save, and convert database files—see The Quick Access Toolbar if you need a quick review about how it works. The automatic save feature can pose a problem if you make a change mistakenly. If you’re fast enough, you can use the Undo feature to reverse your last change (Figure 1-12). However, the Undo feature reverses only your most recent edit, so it’s no help if you edit a series of records and then discover the problem. It also doesn’t help if you close your table and then reopen it. Figure 1-12. The Undo command appears in the Quick Access toolbar at the top left of the Access window, so it’s always available. For these reasons, it’s a good idea to make frequent database backups. To make a database backup, you simply need to copy your database file to another folder, or make a copy with another name (like Bobblehead_Backup1.accdb). You can perform these tasks with Windows Explorer, but Access gives you an even easier option. First, choose File→Save As. Then, under the “File Types” heading, choose Save Database As. Finally, under the Save Database As heading, double-click Back Up Database. This opens a Save As window that offers to create a copy of your database, in the location you choose (Figure 1-13). Figure 1-13. When you choose to create a backup, Access fills in a suggested file name that incorporates the current date. That way, if you have several backup files, you can pick out the one you want. Of course, it’s still up to you to remember to copy your database backup to another location (like a network server) or to a different type of storage (like a DVD or a USB memory stick), so you’re ready when disaster hits. Access makes this job easy. Just choose File→Save As and click the big Save As button. Access opens a Save As window, where you can browse to a different folder on your hard drive and type a new file name. When you’re finished, click Save to seal the deal and create the newly named copy of your database. Keep in mind that once Access creates the new database file, that file is the one it keeps using. In other words, if you create another table or edit some of your data, Access updates the new file. If you want to go back to the old file, you need to open it in Access again. (Alternatively, you can use the backup feature described in the previous section. Like the File→Save As command, the backup feature creates a copy of your database with a new name, but after it makes the backup it carries on using the original version.) When you create a new database, Access uses its modern .accdb format (which is short for “Access database”). Microsoft introduced the .accdb format with Access 2007, and it still works in Access 2010 and Access 2013. That makes it the go-to choice for new databases. However, there may be times when you need to share your data with people who are using truly ancient copies of Access. Versions before Access 2007 use a different database format, called .mdb (which stands for “Microsoft database”). And, as you can see in Figure 1-14, the .mdb format actually comes in two versions: a really, really old version that supports Access 2000, and an improved that Microsoft introduced with Access 2002 and reused for Access 2003. Figure 1-14. To change the format of your database, choose File→Save As (1), click Save Database As (2), and then pick the format you want from the “Database File Types” section (3). Use “Access 2002-2003 Database” or “Access 2000 Database” to save a .mdb file that works with very old versions of Access. The standard .accdb format is the best choice if you don’t need to worry about compatibility, because it has the best performance and a few extra features. But if you need to share databases with people running much older versions of Access, the .mdb format is your only choice. Older database formats are less reliable and may not support all of the Access features you want to use. The best approach is to stick with the .accdb format and save a copy of your data in an older format for the people who need it. However, if possible, keep using the modern .accdb format as the master copy of your database—the one you’ll use to enter new data and to create your Access queries, reports, and forms. You can also use the old-style .mdb format when you first create a database. Choose File→New and then click the folder icon next to the File Name box. Access opens the File New Database window (which you saw back in Figure 1-3). It includes a “Save as type” box where you can choose the Access 2002-2003 file format or the even older Access 2000 format. (If you’re set on going back any further, say the Access 95 format, your best bet is a time machine.) When you add information to a database, Access doesn’t always pack the data as compactly as possible. Instead, Access is more concerned with getting information in and out of the database as quickly as it can. After you’ve been working with a database for a while, you might notice that its size bloats up like a week-old fish in the sun. If you want to trim your database back to size, you can use a feature called compacting. To do so, just choose File→Info and click the big Compact & Repair Database button. Access then closes your database, compacts it, and opens it again. If it’s a small database, these three steps unfold in seconds. The amount of space you reclaim varies widely, but it’s not uncommon to have a 20 MB database shrink down to a quarter of its size. If you compact a brand-new database, Access shows a harmless security warning when the database is reopened. You’ll learn about this message, and how to avoid it, in the next section. The only problem with the database-compacting feature is that you need to remember to use it. If you want to keep your databases as small as possible at all times, you can switch on a setting that tells Access to compact the current database every time you close it. Here’s how:
You can set the “Compact on Close” setting on as few or as many databases as you want. Just remember, it’s not switched on when you first create a new database. Once you’ve created a database, it’s easy to open it later. The first step is go backstage; choose File→Open. There you’ll see a list of all the databases you’ve viewed most recently (Figure 1-15). To open one, just click it. Incidentally, you see the same list of recent databases when you first start Access (Figure 1-1). Figure 1-15. The Recent Databases’s biggest advantage is the way it lets you keep important files at your fingertips by using pinning. To try it, click the thumbtack next to the database file you want to keep. Access moves your database to the top of the list and keeps it there. From this point on, that database won’t leave the list, no matter how many databases you open. If you decide to stop working with the database later on, just click the thumbtack again to release it. Do you want to hide your recent work? You can remove any file from the Recent Databases list by right-clicking it and choosing “Remove from list.” And if the clutter is keeping you from finding the databases you want, just pin the important files, right-click any file, and choose “Clear unpinned items.” This action removes every file that isn’t pinned down. Ordinarily, Access tracks the previous 25 databases in the File→Recent list, but you can tell it to keep a shorter or longer list. To change this setting, choose File→Options, choose Client Settings, scroll down to the Display section, and change the number for “Show this number of Recent Documents.” You can pick any number from 0 to 50. If you want to open a database that’s on your computer but not on the list of recent databases, you can browse your way to the file. Start by choosing File→Open, and, in the Places list, click Computer (Figure 1-16). Click one of the folders you’ve recently used, and Access shows an Open window listing the files in that location. Or, just click the big Browse button underneath to hunt around in the current folder. When you find the file you want, double-click it. Figure 1-16. When you click Places, Access shows you a list of folders in which you’ve recently opened or saved databases. Click one, and Access shows you the familiar Open window for that folder. You can also grab files from your SkyDrive file-sharing account, if you’ve configured it in Access. However, this is strictly a one-way street: you can download databases from your SkyDrive folder, but you can’t upload new ones from Access. In other words, you’ll use SkyDrive as a way to transfer databases from one computer to another, not as a permanent home for your databases. If you want to keep your database on the Web, you need the web database feature described in Chapter 20. (And to learn more about Microsoft’s SkyDrive service, visit http://tinyurl.com/skydr.) Finally, as always, you can open a database file from outside Access by simply double-clicking it in Windows Explorer or on your desktop. When you open a database for the first time, you’ll notice something a little bizarre. Access pops up a message bar with a scary-sounding security warning (Figure 1-17). Figure 1-17. This security warning tells you that Access doesn’t trust your database—in other words, it’s opened your file in a special safe mode that prevents your database from performing any risky operations. If you’re opening your own recently created database, this security warning is a bit confusing, because right now your database doesn’t even attempt to do anything risky. However, once you start building databases with code routines (as described in Part 5), or when you start using action queries (Chapter 8), it’s a different story. In those situations, you need to know if Access trusts your database and will allow it to run code and action queries. In the meantime, you’re probably wondering what you should do about the message bar. You have two options:
Every time you use the File→Open command, Access closes the current database and then opens the one you chose. If you want to see more than one database at a time, you need to fire up more than one copy of Access at the same time. (Computer geeks refer to this action as starting more than one instance of a program.) It’s almost embarrassingly easy. If you double-click another database file while Access is already open, a second Access window appears in the taskbar for that database. You can also launch a second (or third, or fourth…) instance of Access from the Start menu, and then use File→Open to load up a different database in each one. You can use the File→Open command to open an Access database created with a previous version of Access. Access handles old database files differently, depending on just how old they are. Here’s how it works:
You can tell the current database’s format by looking at the text in parentheses in the Access window’s title bar. For example, if you open an Access 2002-2003 file, the title bar will include the text “(Access 2002-2003 file format).” When you open an old-school Access database, you’ll notice something else has changed. When you open a table, it doesn’t appear in a tabbed window like the ones shown in Figure 1-19. Instead, the table opens in an ordinary window that can float wherever it wants inside the main Access window. This seems fine at first, until you open several tables at once. Then, you’re stuck with some real clutter, as shown in Figure 1-18. Figure 1-18. In an old-style Access database, different windows can overlap each other. It’s not long before the table you want is buried at the bottom of a stack of windows. This somewhat unfriendly behavior is designed to mimic old versions of Access, like Access 2003. But don’t worry—you can get back to the slick tabs even if you don’t convert your database to the new format. All you need to do is set a single configuration option:
For a retro touch, you can use the same setting to make a brand-new Access database use overlapping windows instead of tabs. It’s time to step back and take a look at what you’ve accomplished so far. You’ve created the Bobblehead database and added a single database object: a table named Dolls. You’ve filled the Dolls table with several records. You don’t have the fancy windows, reports, and search routines that make a database work smoothly, but you do have the most important ingredient—organized data. One issue you haven’t tackled yet is how you manage the objects in your database. For example, if you have more than one table, you need a way to move back and forth between the two. That tool is the navigation pane, shown in Figure 1-19. Figure 1-19. Unhappy with the space consumed by the navigation pane? Click the Open/Close button in the top-right corner (circled at the top), and the navigation bar slides out of the way to give more room for the datasheet (bottom). Click the button again to expand it back into view. The navigation pane shows the objects (Understanding Access Databases) that are part of your database, and it lets you manipulate them. However, you don’t necessarily see all your database objects at all times. The navigation pane has several different viewing modes, so you can home in on exactly what interests you. When you first create a database, the navigation pane shows only the tables in your database. That’s good enough for now—after all, your database doesn’t contain anything but the tables you’ve created. (You’ll learn how to customize the navigation pane in Chapter 14.) To try out the navigation pane, you need a database with more than one table. To give it a whirl, choose Create→Tables→Table from the ribbon to add a new blank table. Follow the steps starting on Creating a Simple Table to define the table and insert a record or two. Not sure what table to create? Try creating a Collectors table that tracks all the friends you know who share the same bobbleheaded obsession. Now try to come up with a few useful fields for this table (while remembering that there’s no need to go crazy with the details yet), and then compare your version to the example in Figure 1-20. Once you’ve added the new table, you see both the new table and the old in the navigation pane at the same time. If you want to open a table, then, in the navigation pane, just double-click it. If you have more than one datasheet open at once, Access organizes them into tabs (see Figure 1-20). Figure 1-20. Using the navigation pane, you can open as many tables at once as you want. Access gives each datasheet a separate tabbed window. To move from one window to another, you just click the corresponding tab. If you’re feeling a bit crowded, just click the X at the far right of the tab strip to close the current datasheet. If you open enough tables, eventually all the tabs won’t fit. In this situation, Access adds tiny scroll buttons to the left and right of the tab strip. You can use these buttons to move through all the tabs, but it takes longer. Figure 1-21. Do you want to use every square inch of screen space for your data? You can collapse the ribbon (as shown here) by double-clicking any tab. Click a tab to pop it open temporarily, or double-click a tab to bring the ribbon back for good. And if you want to perform the same trick without raising your fingers from the keyboard, you can use the shortcut key Ctrl+F1. So far, you know how to open a table using the navigation pane. However, opening tables isn’t all you can do with the navigation pane. You can actually perform three more simple tasks with any database object that shows up in the navigation pane:
Access gives you a few more options for transferring database objects and tucking them out of sight. You’ll consider these features later in the book. Get Access 2013: The Missing Manual now with the O’Reilly learning platform. O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers. |