Microsoft Office Access, previously known as Microsoft Access is a relational database management system that lets you manage your database efficiently by offering a variety of features. It facilitates creation of database in several ways, manage database structures, export and import databases to/from various data sources and file formats, backup, restore, and perform other operations in easy-to-use graphical user interface. Access can use data stored in Microsoft SQL Server, Oracle, or any ODBC-compliant container including MySQL and PostgreSQL. It supports some object-oriented features but cannot be considered as a fully object-oriented development tool. Access also integrates well with other office packages, and data transfer between Access and other Office components is relatively easy. Almost all users can use it. Beginners can learn to use the wizards and the easy-to-understand interface while developers can push it to its limits and do some extraordinary things with it. This appendix discusses the basics of designing and creating an efficient and productive database using Access.
Using an Access database, one can store and manage large quantity of data for a wide variety of business and personal activities. Before learning to work with Access, let us first discuss the components that make up the Access.
Database objects are the basic building blocks of Access database. Access provides many types of objects. The most common database objects are tables, forms, queries, and reports. Learning how to work with these data objects is the main aim of this appendix.
To start Access, click on Start, select Programs, and then select Microsoft Access. The Microsoft Access dialog box appears with the following options:
Choose the Blank Access database option to create a new database, and click OK. Access will prompt you to specify a location and a name for the database. Enter the desired name of the database in the Filename textbox. For instance, we can give the database name as Online Book. Then click the Create button. This displays the Database window as a sub window within the Microsoft Access Application window.
NOTE In Microsoft Access, it is essential to first save the database and then create it. By default, Microsoft Access saves a database with .mdb extension.
In the Database window, the name of the database is displayed on the title bar (see Figure A.1). In addition, it contains an Object pane that displays different objects that can be created in Microsoft Access.
Fig. A.1 Database window
A table is the basic unit for storing and organizing data in an Access database. A table consists of fields, each of which stores a distinct data for a single record. Data within a table is arranged in a basic grid, with each row containing a single record and each column representing a field. A database can contain any number of tables as well as links to tables stored in other location and other formats.
The fields that make up a table can be defined in several ways. However, each field definition includes a data type. You can choose from the data types listed in Table A.1 for defining a field.
Table A.1 Access data types
Data Type | Description |
---|---|
Text | It is used for field values that contain letters, digits, spaces, and special characters. The default size of text field is 50 and it can contain up to 255 characters. |
Memo | It is used for long text comments and can contain up to 64,000 characters. |
Number | It is used for numeric values. |
Date/Time | It is used for date and time. |
Currency | It is used for currency values. Currency fields are similar to the number data type, except that the decimal places and field size are pre-determined, and calculations performed using the currency data type is not subject to round-off error. |
AutoNumber | It is used when you want integers or a value automatically inserted in the field as each new record is created. |
Yes/No | It is used for yes/no, true/false, or on/off values. |
OLE Object | It is used to embed or provide links to objects like graphics, spreadsheets, sound files, etc. |
Hyperlink | It is used to enter clickable links to web addresses, folders, files, and other objects. Each item in a field of this data type can contain up to 64,000 characters. |
Lookup Wizard | This is not a data type, but this option will create a field that lets you to select a value from another table or from a pre-defined list of values. Once the list is created, Access will then set the data type for you. |
NOTE By default, every new field you create uses the Text data type, with a maximum length of 50 characters.
Tables form the basis for all objects within an Access database including queries, forms, and reports. In this section, we will discuss how to create tables and establish relationships among them, and to add, modify, and delete records from tables.
Access allows creating tables in three different ways: using Design view, using Table Wizard, and using Datasheet view. The easiest approach to create a table is to use the Table Wizard. This wizard assists in creating tables by suggesting one of the pre-defined tables appropriate for business or personal use. However, it is preferred to create table in Design view. Since, it gives you greater flexibility to design a table that meets your exact requirements.
To create a table in Design view, follow these steps.
ISBN
is set as the primary key of the table. To create a primary key, place the cursor in the field row and right-click it. Now select the Primary Key option from the pop-up menu. Similarly, multiple fields can be defined as primary key.NOTE If no primary key is specified by the user, Access creates a primary key of AutoNumber data type automatically under some circumstances.
The BOOK
table in Design view is shown in Figure A.2(a). After designing the table, we can save the table with a desired name. For instance, we have saved our example table as BOOK
. Once, it is saved, it will be displayed in Database window as shown in Figure A.2(b). Similarly, other tables of Online Book database can be created.
NOTE Creating a new table by entering records directly in Datasheet view is a least used approach.
Once a table is created, one can change its structure at any time, even if data is added to the table. New fields can be added, and existing fields can be deleted, reordered, or renamed in the Design as well as Datasheet view. However, to change field’s data type and properties that validated data entries, one must switch to Design view.
Access creates some indexes automatically. If naming conventions are followed carefully, it is guaranteed that right fields are indexed. By default, any field name that begins or ends with ID, key, code or num will be indexed. Use field names such as AID and RollNum as the primary key when possible. When same field name is used in related tables where another field is the primary key, Access automatically indexes these fields, making queries that use these fields as fast as possible.
Fig. A.2 Table BOOK
in MS Access
As we know, a well-designed database contains multiple interrelated tables to ensure referential integrity. To work with multiple tables, one has to define relationship between the tables. Establishing a relationship between two tables requires that each table have a field in common with the other.
To create relationships between two or more tables of a database, follow these steps.
Fig. A.3 Showing tables in Relationships window
Fig. A.4 After adding tables in the Relationships window
PUBLISHER
and BOOK
table, the P_ID
attribute in PUBLISHER
table is dragged to P_ID
attribute in the BOOK
table. An Edit Relationships dialog box appears as shown in Figure A.5.
Fig. A.5 Edit Relationships dialog box
Figure A.6 shows the relationships between all the tables of Online Book database.
Fig. A.6 Relationships among various tables of Online Book database
After the structural design of the table is completed, it is required to enter, view, or edit data in the table. Datasheet view offers a quick and easy way to work directly with data. One can add or delete records at any position within the table. One can also change the look of a particular table in the Datasheet view by changing fonts, hiding columns, and freezing columns so that they remain visible on scroll.
To enter or edit records, open the desired table in the Datasheet view by double-clicking the table from the table list in the Database window.
The BOOK
table of Online Book database with a sample data is shown in Figure A.7.
Fig. A.7 Datasheet view of BOOK
table with some sample data
Queries are the database objects that enable you to retrieve information from the database. It allows retrieving a subset of data from a single table, from a group of related tables, or from other queries using criteria you define. In addition to fields drawn from the table, a query may also contain calculated fields, which display the results based on the contents of the other fields. The query is saved as a database object and can, therefore, be run at any time. The query will be updated whenever the original tables are updated.
Filters provide a quick way to create simple queries. Like queries, filters also allow you to work with a subset of records in a database. Using filters, you can limit the display of records in Datasheet or form views. To create a filter in a table, open the table in the Datasheet view, and then follow these steps.
1. Select one instance value (say, Textbook in the Category field) you want records to contain in order to be included in the filter’s result.
2. Click Records menu, then click Filters and choose Filter By Selection option. This will display the records with the selected value in the particular field. For example, Figure A.8 shows only those records that have Textbook value in the Category field.
Fig. A.8 Applying filter to the BOOK
table
To remove filter from the table, click the Records menu, and then click Remove Filter/Sort option.
Query By Example (QBE) is a feature that provides a user-friendly interface for running database queries. QBE is included with various database applications. Access also has a QBE interface known as Query Design View. A query can be created from scratch in Design view to build one of several specific types of queries. When design view is used to create a new query from the scratch, select query is created by default.
To create a select query in Design view, follow these steps.
Figure A.9(b) shows a select query that displays the detail of a book along with the name of its publisher. You can save this query by any name say, BOOK query
, so that it can be executed any time.
Fig. A.9 Creating query in design view
Access also allows you to create the queries that can change the data in the tables based on the criteria. These types of queries are known as action queries. Access allows creating four types of action queries, namely, make-table, update, append, and delete query.
To create make-table query in Design view, follow these steps.
Fig. A.10 Creating a make-table query
To create an update query in Design view, follow these steps.
Figure A.11 shows an update query to increase the price of the book by 10. Similarly, you can create, append, and delete query.
Fig. A.11 Creating an Update Query
Running a query means viewing the result of the query. Access displays the result of the query in a datasheet view. To run a query, follow these steps.
Book query
. MS Access runs the query and displays the results in Datasheet view as shown in Figure A.12. Alternatively, query can be run by opening it in design view and clicking the Run button on Query Design toolbar.Fig. A.12 Result of Book
query
By default, Access creates an inner join between tables that are added to a query regardless of whether a relationship has been established between the tables. It happens automatically if the two tables contain a common field of the same name and data type, and if this field is the primary key in one of the tables. This autojoin feature can be turned off. To turn off this feature, follow these steps.
1. Click the Tools menu on the Database toolbar and then click Options. The Options dialog box appears.
2. Select Tables/Queries tab, remove the check mark from the Enable Autojoin option, and click OK.
As stated earlier, a query can be created using Wizards or Query Designer. In general, there is no need to deal with SQL. However, each query created in Microsoft Access (created using a wizard or designed in Design view) has an associated SQL statement. You can view the SQL statement behind the existing queries as well as create a new query using SQL. SQL is useful when you want to create a complex query.
To view or modify existing queries in SQL, follow these steps.
Book query
) in the query list and click the Design button from the Database Window toolbar to open the query in Design view.Fig. A.13 SQL view of the query
To create an SQL query, follow these steps.
BOOK
, AUTHOR
, and AUTHOR_BOOK
tables, select these tables.AUTHOR query
) in the Query Name textbox of the Save As dialog box and press OK.The result of this query is shown in Figure A.14(b).
Forms provide a quick way to insert, update, and delete records in the table. They offer an intuitive, graphical environment easily navigated by anyone. They are generally used to display one record at a time in an onscreen window. A form can be customized by having all or only some of the fields from a table.
Fig. A.14 Creating query in SQL view
The easiest way to build a form is by using the Form Wizard. The wizard allows the user to choose the layout of records in the form and the background color, and format of the display. The wizard also allows preview of the layout and style options when a form is created. To create a form using the wizard, follow these steps.
Rating Query
). Then, select the fields that are to be included in the form by highlighting each one in the Available Fields list and clicking the single right arrow (>) button to move the field to the Selected Fields list [see Figure A.15(a)]. To move all of the fields to Selected Fields list, click the double right arrow (>>) button. If you make a mistake and would like to remove a field or all of the fields from the Selected Fields list, click the left arrow (<) or left double arrow (<<) button. After the proper fields have been selected, click the Next > button to move on to the next screen.REVIEW form
). Select Open the form to view or enter information to open the form in Form View or Modify the form’s design to open it in Design view.Fig. A.15 Creating form using Form Wizard
An Access report organizes data in a format ideally suited for printing. Reports can be made by combining data, images, charts, and even audio/video elements. You can add headers, footers, and page numbers, group the information, and change the background colors, among other things. A report provides total control of the presentation of the data. One can add colors, put in summaries and calculations, add in appropriate titles, only show certain fields, and perform many other formatting details.
An Access report can be created from the scratch in Design view. However, it is better to use report Wizard to create the basic structure of a report. This is the quickest way of creating a report. The Report Wizard asks you a series of questions to help you design the data exactly as you need. Once the basic structure of a report is created, it can be opened in Design view to make detailed changes to its contents and appearance.
To create a report using the wizard, follow these steps.
Rating Query
). Then, select the fields that will be included on the form by highlighting each one in the Available Fields list and clicking the single right arrow (>) button to move the field to the Selected Fields list [see Figure A.16(a)]. After the proper fields have been selected, click the Next > button to move on to the next screen.Rating report
) and select to open it in either print preview or design view mode. Click the Finish button to create the report. The report will be displayed as shown in Figure A.16(d).Fig. A.16 Creating report using Report Wizard