15 database basics with ms access

Upload: emina-daff

Post on 07-Jan-2016

66 views

Category:

Documents


0 download

DESCRIPTION

book

TRANSCRIPT

  • M O D U L A R S Y S T E M

    DATABASE BASICS WITHMS ACCESS

    Seluk CANKURTMkremin ZKUL

    h t t p : / / b o o k . z a m b a k . c o m

  • Copyright Zambak Yaynclk veEitim Gereleri A..

    All rights reserved. No part of this book may be reproduced,

    stored in a retrieval system or transmittedin any form without the prior written

    permission of the publisher.

    Digital AssemblyZambak Typesetting & Design

    EditorOsman AY

    Language ProofreaderNanette DAY

    Page DesignHasan ZDEMR

    PublisherZambak Yaynclk veEitim Gereleri A..

    Printed byalayan A..

    Gaziemir / zmir, May 2012Tel: +90-232 252 22 85+90-232 522 20 96 / 97 ISBN: 978-605-112-382-0

    Printed in Turkey

    DISTRIBUTIONZambak Yaynclk ve Eitim Gereleri A..

    Mahmutbey Merkez Mah.Souksu Cad. No. 31 Tek-er Merkezi

    Baclar / STANBUL

    Tel.: +90-212 604 21 00Fax: +90-212 604 21 12

    http://book.zambak.com

    Microsoft, MSN, Microsoft Access 2010, Microsoft Office 2010, and Windows are either registered trademarks or trademarks of MicrosoftCorporation in the United States and/or other countries.

  • CHAPTER 1: INTRODUCTION TO DATABASE .........6

    What is a Database? ...................................................... 6

    What can be done using a DatabaseManagement System? ...................................................6

    Applications Area for Databases .....................................6Class List ..........................................................................7

    Book Catalogue .................................................................7

    Medical Record or Health Record.......................................8

    Components of a Microsoft Access database .................9Tables ...............................................................................9

    Forms ..............................................................................10

    Queries ............................................................................10

    Reports ...........................................................................11

    Macros and Modules .......................................................12

    MS Access Interface, View, or Outline .......................... 12

    Database File Operations in MS Access ........................ 13Opening a Blank (New) Database .....................................13

    Open & New ....................................................................14

    Saving a Database ...........................................................14

    Save As versus Save .......................................................15

    Closing a Database .........................................................15

    Quitting Access ...............................................................15

    MS Access Templates .....................................................16

    The Navigation Pane ........................................................16

    Editing and Deleting Records ...........................................17

    The Quick Access Toolbar ...............................................18

    Practice : Database Fundamentals .......................... 19

    CHAPTER 2: TABLES ...........................................24

    Introduction to Tables .................................................. 24

    Field Data Types .......................................................... 25

    Choosing the Optimal Data Types ....................................26

    Planning a Table from Scratch ..................................... 26

    Pratice I: Student Registration ................................. 26

    Pratice II: Computerized Book Catalog ................... 26

    Pratice III: Computer Lab Inventory ........................ 27

    Rules and Steps in Designing a Table ........................... 27Identify the fields names. ...............................................27

    Identify the data types that will be stored in the fields. ......27

    Identify the indexed fields. ................................................27

    Identify the primary key field. ...........................................29

    Use of the primary key field .............................................29

    Creating a Table .............................................................31

    Creating a Table in the Design View ................................31

    Design a table by entering Data in Datasheet view ...........35

    Designing a Table Using a Template ................................36

    Controlling Data Entry .................................................. 36Using Input Mask .............................................................36

    Validating Data Entry ........................................................38

    Working with Data in the Tables ................................... 39Sorting Records in the Tables ..........................................39

    Filtering Records in the Tables .........................................40

    Finding and Replacing Records in Tables .........................42

    Using Look up Wizard for Data Entry ............................ 42

    Using the Lookup Wizard .................................................43

    Make a Calculated Field ................................................ 46

    Project I: Registration Table ................................... 47

    Project II: Inventory Table ....................................... 47

    CHAPTER 3: FORMS ...........................................52

    Introduction to Forms ................................................... 52

    Making a Form in MS Access ...................................... 54Creating a Form Using the Form, Split Form, and

    Multiple Form ..................................................................55

    Options ............................................................................55

    Creating a Form Using the Datasheet Option ....................55

    Create a Form Using the Form Wizard ..............................55

    Creating a Form Using the Blank Form Option ..................57

    Creating a Form from Scratch Using the

    Form Design Option .........................................................58

    Using Calculated Controls ............................................ 60How to Use Expressions for Calculated Controls ..............61

  • Using Expressions for Default Value Property ...................62

    Using Expressions for the Validation Rule Property ...........62

    Project I: Student Database Form .......................... 64

    Project II: Inventory Database Form ........................ 64

    CHAPTER 4: QUERIES .........................................68

    Use of Queries ............................................................. 68

    Query Types ................................................................ 68

    Creating a Query .......................................................... 69Using the Query wizard ....................................................69

    Filters and Sort Commands vs. Queries............................71

    Create a Query from Scratch ............................................71

    Customizing Queries .................................................... 73Sorting Records using a Query ........................................73

    Grouping and Summarizing Records using a Query ..........73

    Using the Show Option ....................................................74

    Using Criteria ...................................................................74

    Using Wildcards in Criteria ...............................................76

    Using a Calculated Field ...................................................78

    Creating a Parameter Query.......................................... 78Entering a Parameter .......................................................79

    Using Multiple Parameters ...............................................80

    Combining Parameters with Wildcards .............................81

    Creating Action Query (Optional) .................................. 82

    Creating a Make Table Query (Optional) ....................... 82

    Creating an Append Query (Optional)............................ 83

    Making an Update Query (Optional) .............................. 84

    Making a Delete Query (Optional) ................................. 85

    Making a Crosstab Query (Optional) ............................. 87

    Project I: Student Database Queries ........................ 89

    Project II: Inventory Database Queries ..................... 89

    CHAPTER 5: REPORTS ........................................94

    What is a Report? ........................................................ 94

    Reports vs. Forms ...................................................... 94

    The Anatomy of a Report ............................................ 95

    Making Reports .......................................................... 96

    Making a Report Using Auto Report ............................. 97

    Making a Report Using Wizard ..................................... 97

    Making a Report by Using Blank Form Option ............... 98

    Making a Report from Scratch...................................... 99

    Using Calculated Controls in Reports ......................... 101

    Project I: Student Database Reports ..................... 104

    Project II: Inventory Database Reports .................. 104

    CHAPTER 6: RELATIONSHIPS ...........................108

    Introduction ............................................................... 108

    Types of Relationships ............................................... 109

    Making a Relationship ................................................ 109Making One-to-Many Relationships ................................110

    Making Many-to-Many Relationships .............................112

    Establishing Relationships .......................................... 118Data integrity rules .........................................................120

    Join types ......................................................................120

    Editing and Deleting Relationships ..................................121

    Using Subform and Subreport .................................... 121About Subforms ............................................................122

    Make a Form that Contains a Subform using the

    Form Wizard ..................................................................122

    Make a Subform from the Beginning ..............................124

    Project I: From Flat-File to Relational Database ..... 125

    Project II: Designing a Relational Database ........... 125

    Project III: School Library ..................................... 126

    ANSWER KEY ....................................................129

    INDEX ...............................................................130

  • Database Basics with MS Access6

    INTRODUCTION TO DATABASE

    What is a Database?

    A database is a large, organized, and structured collection of data. Any data that can be divided (categorized) into the groups (columns) are suitable for use in a database. For example, data in a class list can be organized by names, surnames, school numbers, and classes as a database. Data are a collection of facts, measurements, and statistics used as a basis for reasoning.

    A database management system (DBMS) is the software that controls database operations for storing, retrieving, adding, deleting, and modifying data. Microsoft Access, DB2, MySQL, Microsoft SQL Server, Oracle, and FileMaker Pro are all examples of database management systems.

    A database is much like a table or a group of listed items. Just like a spreadsheet ( MS Excel) tables, database tables consist of columns and rows. You can also use any word-processing program, such as Notepad and WordPad to list items. However, databases are actually much more powerful than spreadsheets or word processors in terms of manipulating, sorting, searching, and reporting data as well as some relational design concepts.

    What can be done using a Database Management System?

    Storing and manipulating data: you can enter, delete, and edit data.

    Retrieving, sorting, and searching data: you can view, order, and find specific data or filter data according to the specific criteria.

    Reporting data: you can prepare and print out reports.

    Sharing data: you can share data on networks with other users.

    Protecting data: you can protect data from unauthorized users.

    Applications Area for Databases

    It is impossible to give a complete listing of applications that can use database programs, but some common ones include:

    Student records (name, surname, address, parents name) in a school;

    User records (user names and passwords) in an e-mail server;

    Computerized library systems in a school library;

    Flight reservation systems for airlines companies; and

    Computerized inventory systems.

  • Chapter 1 / Introduction to Database 7

    Some examples of databases encountered in daily life include:

    Class List

    That is an example of the simplest database illustrated in Figure 1-1, prepared using Excel.

    Figure 1-1: A class list in MS Excel

    Book Catalogue

    That provides a simple interface for browsing and accessing any book in a library.

    Figure 1-3: Modern method of accessing a book

    Figure 1-2: Classical method for looking for a book

  • Database Basics with MS Access8

    Medical Record or Health Record

    That is systematic documentation of a patients medical history and healthcare in a database in an electronic environment. Traditionally, medicals records have been written on paper and kept in folders.

    Figure 1-4: Traditional medical filing system

    Figure 1-5: Modern medical electronicdatabase system

  • Chapter 1 / Introduction to Database 9

    GETTING STARTED WITH ACCESSMicrosoft Access is an application program for designing relational databases. It is a member of the Microsoft Office suite. MS Access might be used for personal information management and small business organizations to keep track of data in an organized manner.

    Components of a Microsoft Access database

    When you create a database in MS Access, you build a database file that includes different objects (parts) that are also referred to as database objects:

    Tables are a collection of data, arranged in rows and columns.

    Queries make a request to the database, asking it to find, select, or filter data stored in the table.

    Forms provide a quick and easy way to modify and enter records into the table.

    Reports are used to present the selected data from the table on the screen or to get a print out in a datasheet format or in different layouts.

    Macros are a set of instructions and commands to automate specific tasks.

    Modules are a block of programming codes used to perform specific tasks.

    Tables

    A table is a place to store and keep data in a database. A table is the central point of a database file. It is made of rows and columns. Rows are considered to be records, and columns are considered to be fields in which details about one specific record of the table are recorded.

    Figure 1-6: An example of an MS Access table

  • Database Basics with MS Access10

    Forms

    A form is a window object that gives the user a more convenient and interactive way of editing, deleting, and looking at the data in a table or entering data into a table. Whereas a table allows the user to view many records at a time in only the datasheet fashion, a form can display the contents of a record one at time in a form view or more records in a datasheet view or in different layouts. Forms offer a graphical environment that is easily navigated by anyone familiar with standard computer techniques.

    Figure 1-7: An example of an MS Access form

    Queries

    A query is like a request from a database table to make a selection and then modify (change) the data. When you open your a table, you see all the data in the table at one time; a query will help you select specific data that meet specific criteria and modify the selected data.

    When you run a query, it displays only fields that meet the specific criteria. For example, Figure 1-8 shows a select query that is used to select and display only Name, Surname, Form (Class), and Birth Date columns from the table in Figure 1-6.

  • Chapter 1 / Introduction to Database 11

    Figure 1-8: An example of an MS Access query

    Reports

    Reports are used to preview data on the screen in an easy-to-read format or to get out a printout for distribution in a usable view. For example, Figure 1-9 shows a report listing the students birth date with an attractive view and design rather than a plain list.

    Reports do not allow users to make changes to data within a database whereas tables, forms, and queries can allow users to interact with the database to work with data.

    Figure 1-9: An example of an MS Access report

  • Database Basics with MS Access12

    Macros and Modules

    Macros are collections of stored actions that enable you to perform some repeating database tasks automatically and easily. Modules are collections of Visual Basic Access (VBA) codes used to perform some custom database actions automatically.

    Figure 1-10: An MS Access module with VBA code

    MS Access Interface, View, or Outline

    Figure 1-11: Initial view of MS Access

    After you launch MS Access, you see a window like the one shown in Figure 1-11 Initial view of MS Access, with a title bar, a menu bar, and a toolbar. The starting screen provides access to some professionally designed database templates for tracking assets, contacts, events, tasks, etc.

  • Chapter 1 / Introduction to Database 13

    When a database is open, you encounter the interface for the workplace, which is the control point of the different parts of your database.

    Figure 1-12: MS Access interface for the workplace

    MS Access has a contextual interface. It changes according to what you are presently doing.

    The Navigation pane provides easy access to all of the database objects. You can also use the Navigation pane to organize the database objects by object type, date created, date modified, or custom groups that you define.

    The File tab provides commands such as Open, Save, and Save As; other menu tabs display the commands with graphical menu called the ribbon.

    To close open MS Access objects, you can click the close button on the far right side of its tab or press CTRL+F4.

    Database File Operations in MS Access

    Opening a Blank (New) Database

    After you launch MS Access, you can do one of the following:

    Click the File menu and select the New option (Figure 1-11); click the Blank Database button.

    Type a name for your new database in the File Name textbox and then click the Create button.

    6

    72

    3

    1

    5

    4

    1. MS Office ribbon menu tabs.

    2. Quick access toolbar.

    3. New style ribbon toolbar.

    4. Navigation pane.

    5. Tabbed objects instead of stacked.

    6. View buttons.

    7. MS Access Help.

  • Database Basics with MS Access14

    Press CTRL+N and select the Create button.

    MS Access opens a new blank database.

    Figure 1-13: New blank table in the Datasheet view

    Now you have a blank database. In a blank database, the first thing you should do is create a table to keep your data which is why MS Access automatically starts a new table in the Datasheet view.

    Open & New

    You can use the Open command to work with an existing database whereas the New command creates a new blank database, designing it from the beginning.

    To open or retrieve an existing file:

    Click the File menu and select the Open command; select your database file and then click the Open command.

    Or:

    Press CTRL+O and select your database file; then click the Open command.

    Saving a Database

    If you want to use your Access database for later use, you should save it. This can be done by:

    clicking on the Save command on the Quick Access toolbar or

    clicking the File tab and selecting Save command or

    pressing CTRL + S.

  • Chapter 1 / Introduction to Database 15

    When a database is saved for the first time, you are prompted to type a file name. The name should be descriptive in helping to identify your database file later. MS Access automatically adds .accdb the extension name to database file.

    Save your database file form time to time during your work. All newly created or modified database objects need to be saved. However, unlike other MS Office applications, data entered in database objects are saved automatically.

    Save As versus Save

    The Save as command always opens a Save As dialog box whereas - the Save command only opens Save As dialog box during the first use. The Save command saves the changes you make over the existing file without showing the Save as dialog box.

    In the Save As dialog box, you can:

    save your file with another name;

    change the file type;

    choose the place you want to save your file.

    Closing a Database

    To close a database file:

    Click on the File tab; then click the Close Database command.

    Click the close command on the title bar.

    Quitting Access

    Save all database objects that you create or modify before quitting MS Access. If there are any unsaved MS Access objects open, you will be prompted whether you want to save the open object(s).

    It is a good practice to first close hte database. Some of the ways to quit MS Access are listed below.

    Double-clicking on the MS Access control menu button on the far left side of the title bar,

    Figure 1-14: MS Access control menu button

  • Database Basics with MS Access16

    Clicking the close button on the far right side of the title bar,

    Figure 1-15: MS Access close button

    Clicking on the File menu and selecting the Exit menu option

    Pressing Alt + F4.

    MS Access Templates

    Access includes several database templates. If you do not have much experience with databases or if you just do not want to spend time designing a database from scratch, Access provides various templates that you can use to quickly and easily create a database that is ready for use:

    Events: to keep track of important events, such as meeting and appointments.

    Faculty: to manage information about faculty members, such as telephone numbers and addresses.

    Marketing Projects: to manage information about a marketing project.

    Northwind: to manage sales for a fictitious company called Northwind Traders.

    Sales Pipeline: to monitor the progress of sales.

    Students: to manage information about students.

    Tasks: to track a group of work items.

    After creating a database using a template, you can modify and customize the database to better suit your needs.

    The Northwind database (Figure 1-16) is one of the great database template using sample data. It contains some tables, queries, reports and other database features. You can open it as previously explained.

    The Navigation Pane

    When you open any template database or existing database by clicking the Shutter Bar Open / Close Button you can expand or collapse the navigation pane to

    Figure 1-16: Northwind databasetemplate icon

  • Chapter 1 / Introduction to Database 17

    list the existing database objects such as tables, forms, queries, and reports, in a currently open database file (Figure 1-17).

    You can also use the F11 key to open and hide the navigation pane as an alternative to shutter bar.

    When you expand the the navigation pane, by clicking the All Access Objects bar, you can customize how your objects are displayed in the navigation pane. According to these selected options, all the database objects will be listed by their type (Figure 1-18).

    Figure 1-18: Customizing the navigation pane

    Figure 1-17: Navigation pane shutter bar open/close button

    To open a database object listed in the navigation pane:

    Right-click on the name of the object and select the Open option or double-click the name of the object (Figure 1-19).

    To close a database object:

    Right-click on the respective objects tab (an object tab is illustrated in Figure 1-20/1) and select the Close option or click the Close button on the bar displayed on the objects tab (an object close button is illustrated in Figure1-20/2) or click its tab to activate a database object and then press CTRL+F4.

    Editing and Deleting Records

    To edit data in a record in a table or in a form:

    Click the corresponding place and edit it (editing a record in a table is illustrated in Figure 1-20/3).

  • Database Basics with MS Access18

    To delete a record in a Table;

    Click the row select or empty box in front of any record (line) and then press

    Delete key on the keyboard or just right-click on the row selector in front of the

    record (line) and then select Delete Record option clicking the row selector in

    front of any record to select a record is illustrated in Figure 1-21).

    1 2

    3

    Figure 1-20: An open table

    Figure 1-21: Selecting a record in a table

    The Quick Access Toolbar

    MS Access has a new toolbar called the Quick Access toolbar located on the far

    left side of the title bar (illustrated in Figure 1-12/2).

    Figure 1-19: Right-clicking a database object in the navigationpane

  • Chapter 1 / Introduction to Database 19

    To Customize Quick Access Toolbar:

    By adding and removing buttons, you can customize the Quick Access Toolbar.

    For example you can add the frequently used options to the Quick Access Toolbar.

    To add or remove buttons to the Quick Access Toolbar:

    Click the up-down arrow on the far right side of the Quick Access Toolbar

    (Figure 1-22) to open the short-cut menu.

    Select the option you want to add or uncheck the option you want to remove.

    If the menu option you want to add is not listed in the short-cut menu, click the

    More Commands option to see all menu options.

    Figure 1-22: Customizing the Quick Access Toolbar

    Practice : Database Fundamentals

    Making a Database by Using a Template

    1. Start MS Access 2010. You will see the screen illustrated in Figure 1-11 - Initial

    View of MS Access. If the New menu option is not selected, select it.

    2. Click Sample Templates, and then click the template that you want to use.

    3. In the File Name box, type a file name.

    4. Click Create.

  • Database Basics with MS Access20

    Opening Northwind Database

    If it is not available on your MS Office installation, download and install the latest version from the Internet:

    1. Go to http://office.microsoft.com web site

    2. Click the Templates tab

    2. Under the Templates Categories, click see all categories.

    2. Under Databases, Access 2010, click Samples.

    2. Click Northwind web database Access 2010.

    Deleting Records

    While the Northwind web database is open, from the navigation pane, open the Products table:

    1. Add a new record after the last record in the table.

    2. Change the data in the first record.

    3. Delete the third record from the table.

    Leave the table open and go on the next practice.

    Editing Records

    1. From the navigation pane, open the Product Detail form.

    2. Add data for two new products using the form.

    3. Change the data in the fifth record.

    4. Switch back to the Products table and look for the data you added and changed in the second step through the Product Detail form.

    Tables / Queries / Forms / Reports

    1. From the navigation pane, open the ProductList query and look at the result. What is the difference between the contents of the Product table and the ProductList query?

    2. From the navigation pane, open the CustomerList report and look at the result.

    3. From the navigation pane, open the CustomerExtended query and the CustomerList report. What is the difference between the query and report views?

    Customizing the Quick Access Toolbar

    1. Customize the Quick Access Toolbar by adding the New, Open, and Copy buttons.

  • Chapter 1 / Introduction to Database 21

    Test Questions

    1. Which one of the following collections of data is not suitable for use in a database system?

    a. Student class list

    b. Dictionary

    c. This textbook

    d. Address book

    e. Inventory list

    2. Which one of the following tasks might be easily done with the help of a database management system?

    a. Formatting a diskette

    b. Copying files

    c. Searching and sorting data

    d. Healing computer viruses

    e. Compressing data

    3. Which one of the following program does not require a database system?

    a. Hotel booking program

    b. Library catalogue program

    c. Grade book program

    d. Calculator program

    e. Flight reservation system

    4. .. is used to manage organized data for personal and small business purposes.

    a. MS Access

    b. Oracle

    c. MS Word

    d. MS SQL

    e. File Maker Pro

    5. Which one of the following statements is not true about the MS Access program?

    a. It is a relational database management program

    b. It is a program used to access the Internet

    c. It is a member of the MS Office program suite

    d. It is an application program

    e. It is a small business and personal DBMS

    6. . is not a component of MS Access.

    a. Form

    b. Report

    c. Macro

    d. Menu

    e. Table

    7. Assume that you have a database to keep track of college students records. Using this database you only want to get an attractive printout of the list of the boarding students. To achieve this task, which of the following database component(s) is /are not required?

    I. Table, II. Form, III. Query, IV. Report

    a. Only I

    b. Only II

    c. Only III

    d. II and III

    e. I and III

  • Database Basics with MS Access22

    Test Questions

    8. Which one of the following option(s) will not work to design a new database from the beginning?

    I. Launch the MS Access program and click the Create button

    II. Open the File menu and select the New option, then click the Create button

    III. Press CTRL+N and select the Create button

    IV. Click the File menu and select the Open option

    a. Only IV

    b. Only II

    c. I and II

    d. I and III

    e. Only I

    9. .. is the extension name automatically given to the database files in MS Access.

    a. accdb

    b. docx

    c. dbx

    d. accx

    e. xlsx

    10. Which one of the following option is not a way to quit MS Access program?

    a. Double-clicking on the MS Access control menu button on the far left side of title bar

    b. Clicking on the File menu and selecting the Close menu option

    c. Pressing ALT+F4

    d. Clicking close button on the far right side of the title bar

    e. Clicking the control menu box, then selecting Close option

  • Database Basics with MS Access24

    TABLES

    Introduction to Tables

    A table is a place where you keep your data in your database. It is made up of rows and columns. A database can include one or more tables.

    Column

    Row

    Figure 2-1: Table components

    A table is made up of the similar data sets, groups, and columns, which are called fields as shown in Figure 2-2. The column headings are called field names or attributes, which actually describe the data groups in the table.

    Figure 2-2: Fields names

    In the column of a table, the data change for each row, but the type of data stays the same. For example, in Figure 2-1, the Name column displays different names, but the data are all the same text data type. In a database, each column can be set to contain a different data type. Thus, database operations done on your table can be executed more specifically and efficiently.

  • Chapter 2 / Tables 25

    Field Data Types

    We can use different kinds of data in a table in an Access database.

    Text represents a group of characters such as names but it is limited to 255 characters. Names, surnames, and addresses are examples of the text data type.

    Memo is used to write larger text (maximum of 64,000 characters). Long text entries can be kept in memo data type fields.

    Number is for numeric data. Age, amount, and weight are examples of the number type. The number data type can be chosen in one of the following formats from the data field properties:

    Byte is used for small numbers (integers) that range from 0 to 255. The Integer (2-byte) is larger than a Byte; it ranges from -32768 to 32767. The Long Integer (4-byte), Single (4-byte), and Double (8-byte) represent

    larger numbers.

    The Replication ID is a 16-byte Globally Unique Identifier (GUID). If AutoNumber option is used, it assigns a globally random unique numbers to each record. If the Number type is used, a globally unique number can be assigned manually.

    A Decimal (16-byte) is used when your number has a decimal value. Date/Time is used to store a date or time. Purchase date and birth date are examples of this data type.

    Currency is used to represent a currency (money) value. Selling price and school fee are examples of this type.

    AutoNumber sets the numeric values automatically. It is the easiest method for assigning unique numbers to each record in a table.

    The OLE Object allows you to include other kinds of files in your database, such as graphics, and other applications files (e.g. MS Word documents, MS Excel worksheets).

    Attachment allows you to include pictures, images, and MS Office files.

    With the Hyperlink, you can provide a link from your document to a file in your computer or to a web page on the Internet, or to an email address.

    The Lookup Wizard is used to enter some pre-entered data or data from another table in a particular column.

    Yes/No is used for Boolean values. You can use one of three formats: Yes/No, True/False, or On/Off. Any value requiring one of two options can be kept in yes/no fields.

  • Database Basics with MS Access26

    Choosing the Optimal Data Types

    Choose the most suitable data types for your fields. By choosing the optimal data type, you can decrease both:

    Disk space used to store data,

    Processing time to retrieve, manipulate, and write data in the database.

    Planning a Table from Scratch

    Tables are the central point of a database, which is why you should spend some time planning and organizing your tables.

    Before starting to design a table you should decide:

    What is your aspect, and events to be tracked and processed? Answering this question identifies the content of the table and name of the table.

    Which fields do you need to organize similar values and data? Answering this question identifies the name of the fields.

    Which type of data will you store in these fields? Answering this question identifies the field types.

    Pratice I: Student Registration

    On a piece of paper, create a rough copy of a single database table used to track the registration information of students in your college. As you design the table, consider the application form students fill out when they apply to the college.

    Solution:

    1. What is your aspect? Our aspect is designing a table to record the registration data of the students. We can name this table with a descriptive name such as StudentRecordTbl.

    2. Which fields do you need (i.e. which data will be collected from the students)?Name, Surname, Fathers Name, Mothers Name, Address, Phone Number , Email Address, Birth date, Gender, etc. More fields according to the required data.

    3. Which type of data will you store in these fields? Look at the Fields Data Type table. Name Text, Surname Text, Fathers Name Text, Mothers Name Text, Address Text, Phone Number - Text or Number, Email Address Hyperlink, Birth date - Date and Time, Gender lookup.

    Pratice II: Computerized Book Catalog

    On a piece of paper, create a rough copy of a single database table to track the books in your colleges library.

  • Chapter 2 / Tables 27

    Pratice III: Computer Lab Inventory

    On a piece of paper, create a rough copy of a single database table to track assets or inventory items in the computer room.

    Rules and Steps in Designing a Table

    Identify the fields names.

    Identify the data types that will be stored in the fields.

    Identify the indexed fields.

    Identify the primary key field.

    Step I - Identify the fields names.

    A Table with fields for a school database for recording student information is shown in Table 2-1.

    Step II - Identify the data types that will be stored in the fields.

    For the given fields in step I, the following data types can be used.

    Student ID AutoNumber

    Name Text

    Surname Text

    FathersName Text

    MothersName Text

    Address Text

    City Text

    Boarding Yes/No

    PhoeNumber Text

    WebAddress Hyperlink

    BirthDate Date/Time

    SchoolFree Currency

    Photo OLE Object

    Table 2-2: Fields and corresponding types

    Step III - Identify the indexed fields.

    Using of the indexed fields: An index speeds up data searching and accessing time in a database. Using an index is optional in small or single table databases. In a large and professional database, an index provides the following advantages:

    Student ID

    Name

    Surname

    FathersName

    MothersName

    Address

    City

    Boarding

    PhoeNumber

    WebAddress

    BirthDate

    SchoolFree

    Photo

    Table 2-1: Field names

  • Database Basics with MS Access28

    Speeds up searches in a database. In a database with more than one table, links the tables to each other.

    Despite these advantages, indexes take up disk space and slow down the adding, deleting, and updating of records. In most situations, the speed advantages of indexes greatly outweigh these disadvantages. However, if a database updates data very frequently or if you face some disk space constraints, it is better to limit the number of indexes.

    Indexes are not used in fields that contain much the same data or only contain two or three different values. For example, indexing a Yes/No field will not speed up the data search. To check the number of unique entries in an index, use the Access DistinctCount property. Compare this value to the number of records in the table to determine if the index will do any good.

    Unique index: If a unique index is selected, duplicate entries are not allowed. To set a unique index, from the Field properties pane, in the Indexed combo box select the Yes (No Duplicate) option. In this example, the unique index type can be used for the school number, GSM number, and Email address fields. Before setting an index field, determine which fields will be used for searching, filtering, sorting, listing etc., information. For example, the following fields can be set as indexes in a student database:

    List the students from a particular city (e.g. from Istanbul).

    List the students whose father or mother is doing a particular job (e.g. whose fathers are doctors)?

    List the students whose birth dates are in a specific this month, then set the index property in those fields.

    Figure 2-3: Indexed field

  • Chapter 2 / Tables 29

    Step IV - Identify the primary key field.

    A primary key is the field(s) in a table that uniquely defines the records in the table. Actually, the primary key field is a unique indexed (no duplicate) field. When you set a primary key property for a particular field, MS Access will automatically set the Index Yes (No Duplicate) property which means unique index for it.

    To set a primary key property for a particular field:

    Right-click on the field name in the design view and select the Primary key option.

    Figure 2-4: Setting primary key Figure 2-5: Primary key

    In design view, MS Access indicates the key field with a Key symbol.

    Use of the primary key field

    Primary keys are essential for database for many reasons:

    They ensure that each record in a table is uniquely identified. A primary key avoids entering the same record more than once into a table, even if you have done so mistakenly.

    They help establish and enforce various types of integrity. This concept will be further explained in Chapter VI Relationships.

    They serve to establish table relationships. This concept also will be further explained in Chapter VI Relationships.

    The main purpose of a primary key is to uniquely identify the records in the tables. A primary key is also used to relate the records in a particular way between different tables.

    For a single table database using a primary key field is optional. For a large relational database (containing two or more tables linked to one another), you must define key fields for your tables.

  • Database Basics with MS Access30

    Three vital rules for selecting key field(s):

    To decide which fields in your table can be used as key field(s) you can consider a few rules for each table:

    1. All data in the field which will be used as the key field must be unique. In other words, the key field should be chosen among those fields that do not have repeating values.

    2. A records primary key value cannot be null. In other words, the key field should be chosen among those fields that do not have any empty values. The primary key value must exist when the record is entered.

    For example, citizenship number, book ISDN number, and social security number are examples of unique fields and can be used as primary fields.

    Ways to make key field(s)

    After the design of your table is complete, to set the key field, close the database design table window:

    Access displays the following message box.

    Click the Yes button, Access creates a key field. You can have several fields that meet criteria for all the key field rules. For example:

    a. In a table about books, book ISDN

    b. In an employee table, social security number or citizenship number

    c. In a school table, field for students registration numbers

    When there is not a field in a table that meets all key field rules, you can use combination of several fields as key fields. This will create a unique value for each record, such as a persons first and last name, together with the birth date.

    To make a key field of your own, do the following steps:

    a. Open your table in design view and go to the beginning of the table

    b. Make a field and name that field with ID in its name (e.g., StudentsID, BooksID, OrdersID).

    Figure 2-6: Primary key warning dialog box

  • Chapter 2 / Tables 31

    c. Set the Autonumber type for that field.

    d. Right-click on it and select the primary key option.

    Creating a Table

    There are three methods for designing a table in MS Access:

    1. Creating a table from scratch in the Design view

    2. Creating a table in the Datasheet view

    3. Creating a table by using a template

    Creating a Table in the Design View

    You can design your table from scratch. Lets design the following table.

    Figure 2-7: A table

    Open a blank database.

    2

    1

    Design view button

    Figure 2-8: Blank database in Datasheet view

    Figure 2-9: Opening a table in Design view

  • Database Basics with MS Access32

    Access opens a table in the Datasheet view; to switch your view to the Design view:

    Click the Design view button (Figure 2-9/1) or from the Table Tools/Fields on the ribbon in the Views group.

    Select the Design View button (Figure 2-10) or in the Database navigation pane right-click the table name and then select the Design View option (Figure 2-8)

    Access switches Table 1 in the Datasheet view to the Design view, as seen in the following figure.

    Figure 2-10: Table design view buttons

    When prompted Do you want to save?, give a proper name to your table and select the OK button (Figure 2-11).

    Type the field names and set the proper Data Types.

    Figure 2-12: A table in Design view

    Figure 2-11: Table save as dialog box

  • Chapter 2 / Tables 33

    Figure 2-13: Entering field names and selecting their types

    Click the Name field to activate it as shown in Figure2- 14/1 and from the Field Properties pane (Figure 2-14/2), then change the following settings:

    2

    1

    Set Field size to 50

    Set the Required option to Yes

    Set the Indexed option to Yes(Duplicate OK)

    Figure 2-14: Field properties

    For the other fields:

    Surname field: set Field size to 50, Required to Yes, Indexed to Yes (Duplicate OK)

    FathersName, MothersName, City fields: set Field size to 50, Required to No, Indexed to Yes (Duplicate OK)

  • Database Basics with MS Access34

    Address field: set Field size to 255, Required to No, Indexed No

    Currency field: set Format to Euro, Decimal Place to 2.

    For further information on Field Properties, review the following table.

    Field Property Table

    Field Property Description

    Field Size

    You can specify the size of a field. For Text fields, The maximum number of characters (0 to 255) that can be entered in the field; the default setting is 255. For Number / Currency fields, stores the number as a Byte, Integer, Long Integer, Single, Double, or Replication ID. The default setting is Long Integer.

    Format How the data in the field will be displayed on the screen.

    Input Mask Make a pattern in which data must be entered.

    Decimal Places The number of decimal places in Number and Currency fields.

    CaptionA label for the field that will appear on forms. If you do not enter a caption, Access will use the field name as the caption.

    Default Value A value that Access enters automatically in the field for new records.

    Validation Rule An expression that restricts or limits the values that can be entered in the field.

    Validation TextThe error message that appears when an incorrect or restricted value is entered in a field with a validation rule.

    Required Specify whether or not a value must be entered in the field. The default is No.

    Allow Zero Length

    Determines whether or not the field allows zero-length text strings (a string containing no characters). Zero-length text strings are useful if you must enter data in a field, but no data exists. For example, if an ISBN number field requires data, but you do not know the ISBN number of a book, you can enter a zero-length text string in the field. To enter a zero-length text string type in the cell. The cell will appear empty. The default is No.

    IndexedDetermines whether or not you want to index the field to speed up searches and sorts time. The default is No.

    StudentID field is selected as the primary key field and the data type of the

    field is set to AutoNumber type. Name, Surname, FathersName, MothersName,

    and City fields are identified as index fields.

    To index a field:

    Select the field in the Design view; and from the Field Properties pane, set

    the index value.

  • Chapter 2 / Tables 35

    Design a table by entering Data in Datasheet view

    Now lets design the same table using the Datasheet view method.

    Open a blank database. A blank table in the Datasheet view is displayed.

    Choose the Click to Add text box and enter your data. As you enter data, Access automatically names the fields Field1, Field2, etc. and selects the field types according to the data you enter (Figure 2-15).

    Figure 2-15: Designing a table by entering data

    To rename the default Field names:

    Double click on the Field1 or click Field1 and click the Name & Caption button, and then type a new field name (Figure 2-16).

    Figure 2-16: Renaming fields

    To change the field type:

    Click the field, and then select the type of the field from the Data Type drop-down box on the ribbon.

    Access automatically sets the Primary Key. In order to change the Primary Key field, switch the Design View. You can also change the name, type, and properties of the fields in the Design view.

    To index the fields:

    Click the field and then click the Unique check box to set No duplicate type index or click the Indexed check box to set Duplicate Ok index type. Figure 2-17: Setting the type of the fields

  • Database Basics with MS Access36

    Designing a Table Using a Template

    Access also lets you use some predesigned tables based on templates such as Events, Students, and Task.

    To design your tables:

    1. Start Access. Click the Sample Templates buttons (Figure 2-18). Select one of the templates and click the Create button.

    Figure 2-18: Templates

    Controlling Data Entry

    Using Input Mask

    Setting an input mask for a field helps you enter data in a particular format. For example, you can add an input mask for the telephone field in a table. As you enter the numbers into the field, Access automatically adds characters (e.g., parentheses or hyphens).

    An input mask can also be used in queries, text boxes, and combo boxes in forms. You can set the InputMask property in the Design view of a table, query, or form. Do the following steps to set an input mask for a field:

    1. Select your table from the navigation pane.

    2. Open your table in the Design view. Click in the field that you want an input mask for.

    3. In the Field Property window at the bottom of the screen, click the Input Mask box.

  • Chapter 2 / Tables 37

    4. Click the Build button, which is (). A wizard appears.

    5. Follow the instructions in the wizard dialog box. Save your table.

    Figure 2-19: Setting Input Mask Figure 2-20: Input Mask Wizard

    2

    13

    5

    6

    4

    Some input mask Characters:

    Character Description

    0 Digit (0 through 9, entry required; plus [+] and minus [-] signs not allowed).

    # Digit or space (entry not required; blank positions converted to spaces, plus and minus signs allowed).

    ? Letter (A through Z, entry optional).

    C Any character or a space (entry optional).

    < Causes all characters that follow to be converted to lowercase.

    > Causes all characters that follow to be converted to uppercase.

    !Causes the input mask to display from right to left, rather than from left to right. Characters typed into the mask always fill it from left to right. You can include the exclamation point anywhere in the input mask.

    PasswordPassword creates a password entry text box. Any character typed in the text box is stored as the character but is displayed as an asterisk (*).

  • Database Basics with MS Access38

    Input Mask Examples:

    Input mask definition Examples of values Input mask definition Examples of values

    (000) 000-0000 (308) 444-0247 (000) AAA-AAAA (206) 555-TELE

    (999) 999-9999! (308) 444-0247( ) 444-0247

    #999 -101000

    >L0L 0L0 K3M 8N3 00000-9999 67223 - 67223-4009

    >LL00000-0000 KB71351-0037 ISBN 0-&&&&&&&&&-0 ISBN 1-55514-607-9ISBN 0-13-765261-3

    Validating Data Entry

    In a table, you can restrict the data allowed in a field. Incorrect data entry is controlled by setting rules. If the data is breaks the rule, Access prompts a message showing the user what kind of entries are allowed. There are two kinds of validation rules: field validation rules and record validation rules.

    A field validation rule is used to check the value entered into a field.

    For example, If you set >=1 And

  • Chapter 2 / Tables 39

    1. Select your table from the navigation pane.

    2. Open your table in Design View using View button.

    3. Click on the field name you want to limit. This will open the Field Properties section at the bottom left of the screen.

    4. In this section, next to the Validation Rule option, you will see the small button with the ellipsis symbol (three dots).

    5. Click that button, which opens the Expression Builder command box.

    6. Then click the Functions option.

    7. In the white box above, enter values or key words to define the range of data that may be entered. You may use commands like AND, OR and NOT.

    8. When the validation rule finishes, click OK. You may enter a validation text in the Field Properties box to display a message when users enter an incorrect value.

    Some Validation Rule Examples:

    Validation Rule Description

    0 Please enter a nonzero value.

    1 or >5 Value must be either 1 or over 5.

    =#1/1/2000# and

  • Database Basics with MS Access40

    Using Criteria

    You can sort MS Access records using more than one field. Access sorts criteria by first sorting the column to the left; the column immediately to the right of that column is sorted next.

    1. Open the table that you want to sort according to the criteria.

    2. Click on Home ribbon menu; in the Sort & Filter group, click Advanced, then click the Advanced Filter / Sort... option. A window appears.

    3. Select your sort criteria. By clicking in the first field of the first column, a small menu marked by an arrow will appear.

    4. Click on the arrow to access a list of all column (field) titles. Select the first column you want to sort by. In the text box directly below, select the sort order that you want (ascending or descending).

    Figure 2-23: Sorting Data Entry

    5. Repeat these steps for the second and any additional fields that you want to sort by.

    6. When your criteria are selected, click the Apply Filter button. You will see the records sorted according to the criteria you have just established.

    Filtering Records in the Tables

    Filtering allows you to retrieve specific information from a table. For example, in your College Database, you can run a filter to select the students whose names start with the letter S.

  • Chapter 2 / Tables 41

    Here are the steps:;

    Open the table containing the records that you want to filter.

    Click the arrow next to the Filed name and then select the any filter option.

    Figure 2-24: Filtering records in tables

    Or click the Filter button on the Home tab ribbon in Sort & Filter group.

    Access displays a menu at the location of the selected field. Select your filter type.

    For our example: select the Begins With... option and type S to filter all of the students whose name starts with the letter S. The result will list the filtered students.

    To further filter your search, select a second field from the filtered table and continue.

    Remove the Filter

    If you want to see all your records again without filtering them, you can remove the filter:

    1. Select the filtered table, select the filtered field, then click the Filter button

    again.

    2. From the menu, click the Clear filter from .... option or from the Advanced button select Clear All Filters option to remove all filters.

    3. To delete filter(s) temporarily, click the Toggle Filter button. Figure 2-25: Removing a filterfrom a field

  • Database Basics with MS Access42

    Finding and Replacing Records in Tables

    To use the Find and Replace dialog box, open your table in the Datasheet view

    and, from the Home tab, in the Find group click the Find command.

    The Replace tab is used to change the data found by the the Find command to the data in the Replace text box.

    Figure 2-26: Find and Replace dialog box

    Using Look up Wizard for Data Entry

    A lookup column (or field) is a field in a table whose value is retrieved from another table or from a value list. You can use a lookup column to display a list of choices in a combo box or list box. The choices can come from a table or query, or they can be a list of values that you supply. You can create a lookup column manually by setting a fields Lookup field properties; or automatically by completing the Lookup Wizard. Whenever possible, you should use the Lookup Wizard to create a lookup column. You can also create a lookup column in either the Datasheet View or the Design View.

    Figure 2-27: Using Multiple Criteria

  • Chapter 2 / Tables 43

    Using the Lookup Wizard

    The Lookup Wizard starts in the following cases:

    When you create a lookup column in the Datasheet view, click the arrow key next to the Click to Add field notification, then select theLookup & Relationship option.

    When you drag a field from the Field List pane to a table that is opened in the Datasheet view,

    Figure 2-29: Using Lookup with a Field list

    In the Design view when you select Lookup Wizard in the Data Type column.

    Figure 2-30: Using Lookup in Design view

    The wizard guides you through the steps needed to create a lookup column and automatically sets the appropriate field properties to match your choices.

    Figure 2-28: Using Lookup in Datasheet view

  • Database Basics with MS Access44

    The wizard provides two type sources for data: lookup column on a table or query or on a list of values that you enter.

    Figure 2-31: Lookup Wizard dialog box

    Choose the option I want the lookup column to look up the values in a table or query.

    Click the Next button (Figure 2-31); the Lookup Wizard displays a list of tables and queries.

    Select a table or query, click the Next button (Figure 2-32).

    Figure 2-32: Lookup Wizard dialog box

    The Lookup Wizard lists the available fields from the selected table or query. Select the field(s) you want to add and click the Next button (Figure 2-33).

  • Chapter 2 / Tables 45

    Figure 2-33: Lookup Wizard dialog box

    The Lookup Wizard displays a page that allows you to specify a field(s) to sort

    the lookup column. This sorting is optional. When you are finished, click the Next

    button (Figure 34).

    Figure 2-34: Lookup Wizard dialog box

    The next page of the wizard displays the fields that supply lookup values. You

    should choose a field that uniquely identifies the row. Usually, the primary key

    field of the source table makes a good choice. When you are finished, click the

    Next button.

  • Database Basics with MS Access46

    On the final page of the Lookup Wizard, you should type a name for your lookup

    column, which will become the name of the field in your table

    If you want to allow for the selection of more than one value and store the

    multiple values when the lookup column appears, select the Allow Multiple

    Values check box.

    Make a Calculated Field

    You can make a calculated field in the Design view.

    1. Open the table in which you want to make a calculated field in the Design view.

    2. In the first empty row of the table design grid, under Field Name, type a name

    for the calculated field.

    3. Under Data Type, choose Calculated.

    The Expression Builder opens.

    Figure 2-36: Expression Builder

    4. Enter the calculation that you want to perform in the Expression Builder. For

    example, you can multiply Field1 and Field2 and display the result in a calculated

    Field3.

    For example to calculate the selling price: [UnitPrice]-[UnitPrice]*[Discount]

  • Chapter 2 / Tables 47

    5. When you are finished typing the calculation expression, click OK.

    Figure 2-3: Table with calculated field

    After you enter values for the UnitPrice and Discount fields, SellingPrice field will be calculated and displayed automatically.

    Project I: Registration Table

    Design a table to keep track of students registration information, such as name, surname, fathers name, address, birth date, and photo, and save your database as studentdb.

    Project II: Inventory Table

    Design a table to keep track of the inventory in a school; save your database as inventorydb.

  • Database Basics with MS Access48

    Test Questions

    1. Which one of the following is different from the others?

    a. Field

    b. Category

    c. Attribute

    d. Record

    e. Column

    2. To keep the pictures in a field in a table which data type do you need?

    a. OLE

    b. Memo

    c. Bounded Picture

    d. List

    e. Text

    3. To maintain the order number of the computers in a computer room, which data type will be the best selection?

    a. Byte

    b. Integer

    c. Decimal

    d. Long Integer

    e. Single

    4. Which data type will be the best selection for the key field in the main table?

    a. AutoNumber

    b. Long Integer

    c. Text

    d. OLE

    e. Date/Time

    5. Which data type will be the best selection for the book summary field in a table?

    a. List b. Memo

    c. Text d. Long text

    e. Number

    6. To design a table, you should first decide ______________.

    a. Data types b. Indexed field

    c. Field names d. Field properties

    e. Key fields

    7. What do you use to achieve the following goals?

    I. Speed up your data search and access time

    II. Link tables to one another

    a. Indexed fields b. Queries

    c. Macros d. Relationships

    e. Lookup fields

    8. Which one of the following fields is not suitable for adding an index?

    I. Boarding status (yes / no field)

    II. School fee field

    III. Student picture fields

    IV. Notes (with memo data type)

    V. Birthdate

    VI. Fathers Name

    a. I, II, III and IV b. III, V and VI

    c. III and IV d. I, II and III

    e. V and VI

  • Chapter 2 / Tables 49

    Test Questions

    9. The main purpose of a _____________ is to uniquely identify the records in a table and implement a relationship between two tables.

    a. Primary key

    b. Indexed field

    c. Query

    d. Macros

    e. Calculated field

    10. If you are designing a flat-file database (which contains only one table), you do not need to specify:

    a. Indexed fields

    b. Field names

    c. Field types

    d. Key field

    e. Table name

    11. Which of the following option is not important to select a field to use as a primary key field?

    a. Primary key must uniquely identify each record.

    b. The primary-key value cannot be null.

    c. The primary-key value cannot be changed after entered

    d. The primary key field must be first field in a table.

    e. The primary-key value cannot be repeated.

    12. Primary key fields are set automatically by MS Access to

    a. Non-indexed field

    b. Indexed Duplicate OK field

    c. Indexed No Duplicate field

    d. First field

    e. AutoNumber field

    13. Which of the following fields would be the wrong selection for use as a key field in a table?

    a. Citizenship number

    b. Social security number

    c. International Standard Book Number (ISBN) number

    d. AutoNumber field

    e. Birth date

  • Database Basics with MS Access52

    FORMS

    Introduction to Forms

    Forms are used to view, enter, edit, and modify data quickly and easily in tables directly or through a query.

    Figure 3-1: Form

    A form contains a Form Header, Detail, Form Footer, Title bar, and Status bar.

    Figure 3-2: Form in Design view

  • Chapter 3 / Forms 53

    To design a form you should use form controls located in the Form Header, Detail, and Form Footer sections.

    Table 3-1 Some form controls

    Control Description

    Label Displays text. Good for naming objects on a form, titles, and fixed text.

    Text Box Displays the value of a field from a record or is used for data entry into the fields.

    Check BoxDisplays the two state values in a graphical representation: yes/true/on state is represented with checked mark and no/false/off state is displayed with clear box.

    List BoxDisplays a list of values. This list can be a list of values you have pre-entered statically during the form design stage or it can be drawn from another table or query, which also can be updated at any time in the running stage.

    Combo Box This is a combination of a Text Box and a List Box. When you click the drop-down arrow of the Combo Box, it displays a list of choices.

    AttachmentAllows adding several types of files, such as bitmap files, jpg files, sound files, and Office documents, to the fields without increasing the size of the database.

    OptionButton and

    OptionGroup

    Like check box, displays Yes/No (two states) values in a graphical fashion; useful when one option should be selected at any time.

    HyperlinkDisplays a blue underlined hyperlink text that, when clicked, navigates to the pointed object. It is usually a web page, but it can also be a picture, an e-mail address, any type of file, or a program.

    ImageDisplays a fixed picture on the form. It is good for displaying logos and decorating the form.

    UnboundObjectFrame

    Displays fixed old-fashioned OLE (Object Linking and Embedding) objects such as an embedded bitmapped picture, Excel worksheet, audio file, or Word document within your form.

    Figure 3-3: Record navigation buttons

  • Database Basics with MS Access54

    Label is used to displaythe descriptive names.

    Check box is used to make a choice. It enables the users to select more than one option at a time.

    Combo box is used to select an option from the drop down list.

    Commend button is used to perform action.

    Text box is used to enteror display the data.

    List box is used to selectdata from the list.

    Tab is used to categorize a group of controls.

    Option box is used to make a choice. It enables the users to select only one option at a time.

    Figure 4: Illustration of some Form Controls

    Making a Form in MS Access

    There are several ways to create a form in MS Access:

    Table 3-2: Creating forms

    Method, Option Icon Description

    Form Automatically creates a form from the currently selected table or query.

    Form Wizard Creates a form using the Wizard.

    DatasheetAutomatically creates a form to display multiple records at once in datasheet fashion.

    Form Design Creates a blank form in Design view to design a form from the beginning.

    Blank FormCreates a blank form in Layout view. In Layout view, while form is running, it can be edited. When you locate a bound form control, It dynamically displays current data.

  • Chapter 3 / Forms 55

    Creating a Form Using the Form, Split Form, and Multiple FormOptions

    You can use the Form tool to quickly create a form that displays information about one record at a time (Figure 3-1):

    1. In the Navigation Pane, click the table or query that contains the data that you want to use on your form.

    2. On the Create ribbon menu, in the Forms section, click More Form button,

    then click the Datasheet option.

    3. Access creates the form automatically and displays it in the Datasheet view.

    Creating a Form Using the Datasheet Option

    You can use the Datasheet tool to quickly create a form in the Datasheet format, which displays all records at a time.

    In the Navigation Pane, click the table or query that contains the data that you want to use on your form.

    On the Create the ribbon, in the Forms section, click the Form , Split

    Form or Multiple Items option.

    Access creates the form automatically and displays it in Layout view.

    To start your new form, switch to Form view. On the Home ribbon menu, in the

    Views group, click View, and then click Form View .

    Create a Form Using the Form Wizard

    When you use Form, Split Form, Datasheet, and some other options to make a form, Access automatically adds all fields and uses its own default styles.

    The form wizard provides options about how fields appear on the form, what layout and style will be used, and how the data are grouped and sorted.

    To use the Form Wizard option:

    1. In the Create tab on the ribbon, in the Forms section, click the More Forms

    command, and then click Form Wizard .

  • Database Basics with MS Access56

    2. Follow the directions on the dialog boxes of the Form Wizard.

    Figure 3-5: Form wizard

    3. In the first dialog box, you can select the table or query that contains the data you want to work on and then select the fields to locate the data in your Form; then click the Next button.

    Figure 3-6: Form wizard

    4. In the second dialog box, chose the form layout and then click the Next button.

    5. In the last dialog box, you can name your form and decide to open your form or continue developing your form design yourself.

    6. On the last page of the wizard, click Finish.

    The Form Wizard produces a variety of forms depending on the options that you select.Figure 3-7: Form wizard

  • Chapter 3 / Forms 57

    Creating a Form Using the Blank Form Option

    Using the Blank Form option creates a blank form and allows you design a form in Form view. In the Form view display, you can design and modify the form. Each control displays current data as you double-click or drag them from the Field list onto the Form.

    To modify your form you can use:

    Property sheet to modify the properties for the form and controls located in it and sections. To display the property sheet, press F4 or click the Property Sheet button on the Design tab in the Tools group.

    Field List pane to add fields from the associated table or query. To display the Field List pane, click the Add Existing Fields button on the Design tab in the Tools group or Press ALT+F8. Then drag fields directly from the Field List pane onto the form.

    In the Create tab on ribbon, in the Forms group, click Blank Form.

    Figure 3-8: Blank form

    Access opens a blank form in Layout view, and displays the Field List task pane on the right side as shown Figure 3-8.

    In the Field List task pane, expand the table or tables that contain the fields by clicking the plus symbols.

    To add a field to the form:

    Double-click or drag it onto the form.

    When you drag or double-click any field it will be locate on the form and also be automatically bounded to the table and its corresponding field. Figure 3-9: Form tools

  • Database Basics with MS Access58

    From the Design tab on the ribbon, use the tools in the Controls group to add a logo, title, page numbers, or the date and time.

    You can also switch to the Design view by right-clicking on the form, clicking the Design View , and then using the tools in the Controls group on the Design tab.

    To add a control;

    Click the tool for the control you want; then click on the form where you want to position it.

    Figure 3-10: Form controls

    Creating a Form from Scratch Using the Form Design Option

    If you want to design your form completely from the beginning, you can use the Form Design Option, which opens a blank form in the Design view.

    To design a form from the beginning, you should follow four steps:

    On the Create tab, in the Forms group, click the Form Design command. Access opens a blank form in Layout view.

    Figure 3-11: Form in Design view

  • Chapter 3 / Forms 59

    Next, bound your form to a table. To define the forms source, you should bound (link) the form and table to one another.

    To bound a blank form to a table or query;

    Right-click on the form control box and select Form properties or click the form and press F4 to display the property sheet on the right side. Click the Data tab; then select the Record Source box and select the source.

    Figure 3-12: Form properties

    Locate your controls using the tools in the Control group on the Design tab (Figure 3-9).

    You can rename the labels of the controls to the name of the data fields. In order to rename a control, double-click on the label and type a new name.

    Figure 3-13: Locating form controls

    After you finish laying out the controls on your form, you should also define sources for unbound controls.

  • Database Basics with MS Access60

    To bound a form control to a table field:

    Right-click on an unbound form control and select the properties. You will see the Property Sheet pane to the right as shown in Figure 3-14b. On the pane click the Data tab, then click Control Source box and select the corresponding field name.

    Repeat this for every unbound form control.

    After you finish your form, you can switch to the Form view to use your form. On the Home ribbon menu, in the Views group, click View and then click Form View.

    Figure 3-14a: Form control properties

    Figure 3-14b: Control source

    Using Calculated Controls

    Instead of bounding a form control to a field, you can type an expression as a data source for a form control. Text box controls are usually used as calculated control.

    EminaHighlight

  • Chapter 3 / Forms 61

    An expression is a combination of:

    Identifiers the names of table fields or controls on forms or reports, or the properties of those fields or controls, such as [Name] and [OrderDate]

    Operators mathematical or logical operators such as + (addition), / (division), and < (smaller)

    Functions built-in functions such as SUM or AVG

    Constants values that do not change such as 0.6, 303, and College

    You can use expressions to:

    calculate values

    set a default value for a field or control

    validate data

    How to Use Expressions for Calculated Controls

    1. In the Navigation Pane, right-click on the form or report that you want to change

    and click Design View on the shortcut menu.

    2. On the form or report, right-click the text box control that you want to change, then click Properties on the shortcut menu.

    3. If necessary, click the Data tab.

    4. Click the box next to the Control Source property and type your expression. For example, you can copy and paste an expression from Table 3-3 Some Examples for Expressions

    5. Close the property sheet.

    Table 3-3: Some Examples for Expressions

    Expression Description Example result

    =[Page] Displays the page number. 1

    =Date() Displays the current date.

    =[Price]*1.1 The product of the value of the Price field or control and 1.1 (adds 10 percent to the Price value).

    If value in Price field 100 produce 110

    =Forms![Student]![Name] The value of the Name control on the Student form. Mesut

    =Avg([Score]) Uses the Avg built-in function to display the average of the scores of a field named Score

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaInsert TextTo input image

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

  • Database Basics with MS Access62

    Using Expressions for Default Value Property

    If you want to assign a default value to a field or control, you can use the Default value property.

    To add a default value for a control in a form:

    In the Navigation Pane, right-click the form that you want to change and click Design View on the shortcut menu.

    Click the field that you want to change; and on the General tab, click the Default Value property box.

    Type the expression, or click the Build button to the right of the property

    box to type an expression using the Expression Builder or by yourself.

    Table 3-4: Expressions for Default Field Values

    Expression Default Field Value

    1 1

    XXL XXL

    Istanbul Istanbul

    Date() + 7 one week later from today

    Using Expressions for the Validation Rule Property

    To add a Validation Rule for a control in a form:

    In the Navigation Pane, right-click the form that you want to change and click Design View on the shortcut menu.

    Click the field that you want to change; on the General tab, click the Validation Rule property box and then type your expression.

    Table 3-5: Example expressions for Validation Rule

    Validation Rule property Validation Text property

    0 Enter a value other than zero.

    0 Or > 5 Enter a value that is either 0 or more than 5

    Like S???? Enter a five-character long text, beginning with the letter S.

    < #1/1/2010# Enter a date prior to 1/1/2010.

    >= #1/1/2010# And < #1/1/2011# Date must be in the year 2010.

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

  • Chapter 3 / Forms 63

    The following example in the following form demonstrates how to use the Validation Rule property and Validation Text property.

    Open a form in the Design view (Figure 3-15); select the Zip/Postal Code field and then press F4 to open the Property Sheet.

    Set the Rule property to 0 and Validation Text to Please enter a value other than 0.

    Figure 3-15: Setting Validation Rule and Text properties

    Open your form in the Form view or run it (Figure 3-16) and enter the zero value for the Zip/Postal Code field.

    Press enter to confirm your entry.

    A warning message shows that you are not allowed to enter a zero value in this field.

  • Database Basics with MS Access64

    Figure 16: Validation Text Warning

    Project I: Student Database Form

    Open the studentdb database that you have designed in Chapter II and design a form for that table.

    a. Open the form and create records for five students.

    b. Open the table and find the records that you have just created with the help of the form.

    c. Open the table and create three new records at the end of the table.

    d. Open the form and find the records that you have just created directly into the table.

    Project II: Inventory Database Form

    Open the inventorydb database that you designed in Chapter II and design a form for that table.

  • Chapter 3 / Forms 65

    Test Questions

    1. Which one of the following is not a form control?

    a. List box

    b. Option box

    c. Check box

    d. Menu

    e. Textbox

    2. You can link a form to _____________

    a. a macro

    b. a table field

    c. a table

    d. a form control

    e. a report

    3. You can link a form control to _____________

    a. a table

    b. a table field

    c. a form

    d. a query

    e. a report

    4. To link a form, use the _____________ option in

    design view.

    a. Table source

    b. Record set

    c. Record source

    d. Data source

    e. Query

    5. Which form control can be used for a Yes/No field in a form?

    a. Label

    b. Navigation

    c. Input box

    d. Check box

    e. Bound Object Frame

    6. Which form controls cannot be used for field us ed to display students class in a form?

    I. Text Box

    II. List Box

    III. Combo Box

    IV. Check box

    V. Option Box

    a. I, II and III

    b. III and IV

    c. I and II

    d. III, IV and V

    e. IV and V

  • Database Basics with MS Access68

    QUERIE S

    Use of Queries

    You can consider a query as a request used primarily to extract, gather, and select required data from a database. It is also used in certain cases to manipulate data, such as updating and deleting records or calculations in data fields. There are also some sophisticated queries to automate these data operations.

    A query can select different data fields from one or more related tables to create a list, such as a list of names, surnames, fathers names, and fathers phone numbers of the students who attended Computer Science course.

    You can use the queries to perform calculations on data to produce the sum, average, count, and some other mathematical operations. For example, you can calculate the averages of all students as well as the class averages.

    Figure 4-1: A query

    Query Types

    Select query enables you to select specific fields out of the tables instead of displaying all fields in the tables. A select query can do three basic query operations easily: selection, filtering, and sorting.

    Find Duplicate Query finds the repeated records in one or more fields that you specify.

    Find Unmatched Query finds records that do not have related records in another table.

    Crosstab query calculates outcomes, such as sum, average, and count.

    Action Queries perform an action on the results of the selected data. There are four types of action queries:

    1. Delete query deletes records from the table(s).

    2. Append query adds records from one or more tables to the end of one or more other tables.

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

    EminaHighlight

  • Chapter 4 / Queries 69

    3. Update query changes present records with the updated version in the table(s).

    4. Make Table query makes a new table from all or part of the data in one or more tables.

    Parameter queries prompt the user for criteria that restricts or filters the results.

    Creating a Query

    Using the Query wizard

    Using the Query wizard is a quickly, effective method to design a query easily. You can use the Wizard to create:

    Simple Query

    Crosstab Query

    Find Duplicate Query

    Find Unmatched Query

    To make a simple select query:

    On the Create tab, click the Query Wizard button (Figure 4-2).

    The New Query dialog box opens. (Figure 4-3).

    Figure 4-2: Query Wizard Figure Figure 4-2: Query Wizard button

    Select the Simple Query Wizard in the list, and click OK. (Figure 4-3).

    In the Tables/Queries list, click Table: Table1. Then double-click the required fields from the Available Fields list to the Selected Fields list.

  • Database Basics with MS Access70

    For example, Name, Surname, Birthdate, and EmailAddress (Figure 4-4).

    Figure 4-3: Query Wizard

    In the next dialog box, name your query (Figure 4-5) and click the Finish button.

    Figure 4-5: Query Wizard

    Access runs the query and displays the results in the Datasheet view

    (Figure 4-6). You can scroll through the results and see that information is

    displayed in order.

  • Chapter 4 / Queries 71

    Figure 4-6: A select query

    Switch to Design view if you want to customize the query.

    Filters and Sort Commands vs. Queries

    The Filter and Sort commands are usually faster to implement than queries.

    The Filter and Sort commands can be used temporarily; queries can be permanently saved and run again in the future.

    The Filter and Sort commands can be used with only the currently open table or form. A query can use tables or queries.

    Create a Query from Scratch

    On the Create tab, in the Query group, click the Query Design command.

    Figure 4 -7: A query in Design view

    The query design window and Show Table dialog box are displayed (Figure 4-7).

  • Database Basics with MS Access72

    In the Show Table dialog box:

    Double-click a table to add it to the query window. Close the dialog box.The upper part of the window displays a field list for each table or query that you add.

    The selected table is represented in the top portion of the window with a field list. An asterisk at the top of each list represents all fields in the list.

    The lower part of the window, known as the design grid, is used to define the querys criteria and accept expressions as criteria.

    Figure 4 -8: A query in Design view

    Drag or double-click on the fields Name, Surname, Birthdate, and EmailAddress to add them to the columns in the Design grid.

    On the Design tab, in the Results group, click the Run command.

    Access runs the query and displays the results in the Datasheet view.(Figure 4-9).

    Figure 4-9: A select query

  • Chapter 4 / Queries 73

    Customizing Queries

    With a custom a query you can sort, filter, restrict records, group, summarize records in the selected fields, and create calculations in the records in the selected fields.

    Open your query in the Design view to customize it.

    Sorting Records using a Query

    A query displays records in the order in which they are entered. You can also sort records alphabetically, numerically, or by date in ascending (A to Z) or descending (Z to A) order.

    To sort the records in a query:

    Open the query in the Design view.

    The third row in the design grid is labeled as Sort (Figure 4-10). You can set sorting criteria for one or multiple fields.

    Figure 4-10: Sorting in a query

    Grouping and Summarizing Records using a Query

    You can also group records in a query, usually for the purpose of summarizing data. A group is a collection of related records; it can be useful when you are analyzing data.

    To group the records in a query:

    Open the query in the Design view. On the Design tab, click the Totals command.

    In the design grid, the Total row appears as shown in Figure 4-11.

    EminaHighlight

    EminaCalloutFalse

  • Database Basics with MS Access74

    Select one of the functions to group your records.

    Figure 4-11: Us ing the Total option in a query

    An Aggregate function returns information about a set of records and some important aggregate functions, as follows:

    Sum: Totals the values for each group.

    Avg: Averages the values for each group.

    Count: Returns the number of items in each group (excluding nulls and blanks).

    The other three options are listed here:

    Group By: Defines the group by reducing the data to unique entries.

    Expression: Returns a calculation based on an aggregate function.

    Where: Specifies search criteria.

    Using the Show Option

    Open your query in the Design view. In the Show row, use the check boxes to hide or display the fields.

    Using Criteria

    You can limit the results of your query by specifying criteria expressions. For example, you might want to see only students from a specific state (city).

    Figure 4-12: Using the Show option in a query

    EminaHighlight

    EminaCalloutTrue

  • Chapter 4 / Queries 75

    How to Use Criteria

    Open your query in the Design view.

    Type your criteria in the corresponding criteria place, as shown in Figure 4-13.

    Query will return only those records in which the UnitPrice entry equals to $10.

    Figure 4-13: Using criteria

    Use the logical operations and, or, and not to combine your criteria.

    Figure 4-14: Two different methods of using the AND condition

    The query returns those records in which the Quantity value is bigger