This document gives an introduction to Microsoft Access – the ...

4 downloads 25 Views 1MB Size Report
Microsoft Access is a Windows Relational Database Management System ... Modules: A module is a collection of programs: subroutines or functions. It.
INFORMATION SYSTEMS SERVICES

Getting Started With Microsoft Access

This document gives an introduction to Microsoft Access – the relational database management system.

AUTHOR: Information Systems Services, University of

DATE: June 2004

EDITION: 3.0

UG 8

UNIVERSITY OF LEEDS

Contents Introduction

3

Fundamentals System Requirements Data Types Supported by Access Restrictions Imposed by Access Running Access

3 4 4 5 6

Help Creating a New Database Opening an Existing Database Creating Tables within a Database Viewing Data Insert, Spell Check, Delete and Modify Data Using Existing Data Exporting to Other Systems Queries Relationships Forms Reports Pages Macros Modules Concurrent Access to Data Deleting Database Objects Renaming Database Objects Options - Changing Access's Behaviour Different Versions of Access Example Database

7 8 8 8 12 14 14 15 15 23 24 31 34 34 35 36 37 37 37 38 39

1

Format Conventions In this document the following format conventions are used: Commands that you must type in are shown in bold Courier font. Menu items are given in a Bold, Arial font. Keys that you press are enclosed in angle brackets.

WIN31 Windows Applications

Feedback If you notice any mistakes in this document please contact the Information Officer. Email should be sent to the address [email protected] Copyright This document is copyright University of Leeds. Permission to use material in this document should be obtained from the Information Officer (email should be sent to the address [email protected]) Print Record This document was printed on 8-Dec-04.

2

Introduction Microsoft Access is a Windows Relational Database Management System (RDBMS). It is a recommended product in this application area and is available on cluster PCs at the University of Leeds. Since its launch in 1992, several different versions of Microsoft Access have become available. In compiling this document, Access 2003 has been used. In the main, the documented features are available in all versions of Access from version 2000 onwards. However, if you are using version XP or 2000, there may be some minor differences between what is shown herein and what you see or where a particular item appears in the interface. The user interfaces in versions 97 and before vary more – but the principals remain the same. This document assumes a basic familiarity with relational database management systems and with Microsoft Windows. It describes, in broad terms, the facilities offered by Access and shows how to carry out the most common tasks. A database based on the operation of a bank is described in the Example section of this document and will be used in examples throughout.

Fundamentals An Access database is made up of a collection of: Tables: The basic component of a relational database. Tables can be defined and data can be inserted, deleted or modified in a straightforward way. Queries: Act as a store for (SQL) commands. When run, a query will result in specific data being displayed or modified. Forms: A medium through which information can be displayed and modified. A form can display related information that is stored in several tables. Reports: Lay out information – often for inspection in paper form. A report can display related information that is stored in several tables. Reports can also be used to produce mailing labels and simple graphics. Pages: Introduced in Access version 2000 pages aim to provide integration with the World Wide Web. Pages are not an integral part of a database. A page is either a reference to an existing HTML file or it may be generated by Microsoft Access to enable Web based access to a database. Its working depends on certain infrastructural components being available. Macros: Used to automate specific, usually high level, tasks. Modules: A module is a collection of programs: subroutines or functions. It is expressed in Visual Basic - a high level language - and allows a programmer to specify almost any task. All of the information (except pages) connected with a database can be stored in a single file. The file will have a name of your choice and extension MDB.

3

Access enables its users to: •

Create a relational database: that being a set of related tables



Create and utilise queries, forms, reports, macros and modules



Insert, delete, modify, sort and validate data



Manage concurrent access to data



Import data from, and export data to, other database systems or ordinary files



Interact with other applications e.g. Microsoft Word



Obtain on-line help

System Requirements •

Before purchasing any program you are advised to check that the system you have is capable of running it. The following information is for your guidance only and gives practical minimum requirements to run different versions of Microsoft Access.



Access 2003: An Intel Pentium 233 MHz processor (a Pentium III is recommended), a monitor with resolution of at least 800x600 with 256 colours, 128Mb RAM, Microsoft Windows 2000 with SP3 or XP, or Windows 98, Me or NT.



Access XP or 2000: An Intel Pentium 233 MHz processor, a monitor with resolution of at least 800x600 with 256 colours, 64Mb RAM, Windows 98, NT with SP 6a, 2000 with SP2, Me or XP.



Access 97: An Intel 80486 compatible PC, a VGA monitor, 32Mb RAM, Windows 95, 98, NT version 3.51 with SP5, Windows 2000, Me or XP.



Access for Windows 95: An Intel 486DX 22MHz, VGA monitor, 8Mb RAM, Windows 95 or later.



Access 1 or 2: An Intel 80386, EGA or VGA monitor, 6Mb RAM with Windows 3.1 or later.

Data Types Supported by Access The following data types are supported by Access: •

Text: up to 255 alphanumeric characters (Unicode is supported in Access 2000 and later)



Memo: up to 65535 characters



Number •

Byte: integers in the range 0-255



Integer: range -32768 - +32767



Long Integer: range -2147483648 - +2147483647



Decimal: +-1028-1 (28 decimal places) Single precision floating point: range ± 1.4*10-45 to ±3.4 * 1038 (7 significant figures)



Double precision floating point: range ±4.9*10-324 to ±1.79 * 10308 (15 s.f.)



Currency



Date/Time (from years 100 to 9999) 4



AutoNumber (An automatically incremented long integer)



Yes/No



OLE Object (binary data)



Hyperlink

Restrictions Imposed by Access Many of the restrictions imposed are not restrictions in any practical sense. The only ones likely to cause problems are: •

Number of fields in a table or query: 255



Number of bytes entered into a record (excluding Memo and OLE fields) 2000



Form/Report - Width: 22", Section Height: 22", Overall height: 200"



Number of levels of nested Forms/Reports: 7 (3 in version 2000 and earlier)

5

Running Access To run Access, double-click the Access Icon. From the start-up window you can choose to open an existing Access database or create a new one. The figure below shows a typical Access window after an existing database has been opened.

Menu Bar Tool Bars

Action Object

Database Window

Status Bar

Figure 1: The Access Window



The Menu Bar is a collection of items. Selecting one will reveal a menu. The content of the Menu Bar, and the menus that display, vary according to which window is active (that most recently clicked).



A Tool Bar is a collection of buttons that provide shortcuts to performing certain common tasks1. When the mouse is moved over a toolbar button, an indication of what will happen when it is clicked appears near the mouse pointer.



The Status Bar is used to display messages.



The Database Window displays a list of objects: Tables, Queries, Forms, Reports, Pages, Macros or Modules depending on which one of the Database Object Buttons has been most recently clicked2.

1 More than one toolbar may be displayed at one time. Each toolbar has a name and can be customised. 2 The Object Buttons will be referred to individually as the Tables, Queries, Forms, Reports, Pages, Macros and Modules Button.

6



The Action Buttons enable a new object to be created, or an existing one to be used or modified3. The icons to the right of the Action Buttons control the format and what is displayed in the Database Window. Other windows can be displayed in the Access Window. For example, they might show information in tables, forms or the output from queries.

Help The Help menu gives access to a comprehensive help system

Figure 2: The Help menu



Table of Contents gives access to a hierarchical sequence of headings.



Search For - Searches for help on a designated word or phrase.

3 Three Action Buttons always appear but are labelled according to the most recently clicked Database Object button — their functions are always clear. Note: when an object is being specified or having its specification altered it will be said to be in design view.

7

Creating a New Database To create a new database, select New... from the File menu displayed in the Access start-up window. A form will display – it is illustrated on the right of the figure below.

Figure 3: The File/New Window and the New Database Dialog Box

To create an empty database, choose Blank Database: you will be prompted to specify a Drive, Directory and File Name for the new database. Access also has a set of templates for creating databases for different sorts of application. These are available through General Templates… In electing to use a template you are effectively asking Access to create an outline database which you can then modify.

Opening an Existing Database To open an existing database, select Open... from the File menu in the Access startup window. A dialog box will display in which the Drive, Directory and File Name can be specified. Alternatively a list of recently opened databases appears towards the bottom of the File menu – click on one to open it.

Creating Tables within a Database To create a new table, click the Tables button in the Database Window. Then click the New button.

8

Figure 4: The Table Window(Design View)

Selecting Design View and clicking the OK button causes an empty grid to display in which structure of the new table can be specified as illustrated above. Each field must be given a name and a data type. Entering a description is optional. Selecting Table Wizard enables the creation of a table starting from one of a number of templates.

Field Properties When a field has been given a name and data type, further properties can be specified in the Field Properties section which displays in the bottom left hand corner of the table window. That displayed in the figure above shows the General properties for a field with data type Number. The properties vary according to the data type. The role of each property is outlined in the Hint box when that property is clicked, and context dependent help is available4. •

Field Size determines the maximum amount of text for Text fields or the type of number (Byte, Integer, Long Integer, Decimal, Single or Double) for Number fields.



Format determines how the data is formatted for display. A number of options are provided together with the ability to specify custom formats for some data types.



Decimal Places applies to Number or Currency fields, and determines the number of digits displayed after the decimal point. Its setting does not affect the underlying data.



Input Mask defines a pattern to which input data must conform.

4 Press the F1 key for context dependent help

9



Caption determines the label used with the field on forms and reports, and what appears at the top of a column in queries. If caption is not set, the field name is used.



Default Value determines a default value for this field when new records are created.



Validation Rule5 can be used to specify a condition or conditions that data must satisfy.



Validation Text displays on the status bar when data breaks the validation rule.



Required means that the field must contain valid, non-null, data before a record can be saved.



Indexed determines whether an index is built for the field - see also Indexes below.



IME Mode and IME Sentence Mode. Introduced in Office XP IMEs (Input Method Editors) allow users to input Asian text in Office applications, regardless of the language version of their operating system. IME behaviour is modified through these properties.



Smart Tags: Introduced in Office XP, smart tags offer means of integration with other programs. E.g. Microsoft Outlook could be activated through a smart tag in such a way that the Mail-To… field was pre-filled with an email address stored in a database.



The Lookup tab can be used to control how data is entered. E.g. For textual data, it can be used to define a set of data values that display as a drop-down list similar to that shown in figure 28

Indexes Indexes are used by Access when tables are queried to speed that process. They can result in dramatic performance gains on large tables. The indexes that are currently associated with a table can be viewed and modified through a dialog box which displays when the Index button on the toolbar is clicked. Indexes can be based upon a single field or a combination of fields. The Primary Key6 of a table can be defined by selecting a field, or a set of fields and pressing the key button which appears on the tool bar. Alternatively, the primary key can be specified or modified through the Index dialog box. There is an overhead in indexing - disk space is required and the index has to be updated whenever the data in that field changes. However, it is generally advisable to index if the table contains a large number of records and if the field will be used frequently in queries.

5 A Validation Rule is absolute. It cannot be overridden by the specification of alternative validation rules on a Form which serve only to further restrict the data that can be entered through it. 6 A field, or a combination of fields, the values of which can be used to uniquely determine a record.

10

Table Properties The Table Properties dialog box enables a Description to be associated with a table and table wide (as opposed to field specific) validation rules to be specified.

Saving a Table To save a table definition, select the File menu and choose Save7 - you will be prompted for a table name.

Figure 5: Prompt for Table Name

Overtype the suggestion with a name of your choice.

Modifying the Structure of a Table The structure of a table can be modified after it original definition8. Adding extra fields9 or increasing the size of a text or number field is straightforward. Changing the name of a field may require you to alter any references to it in Queries, Forms, Reports, Macros and Modules. Name change tracking was introduced in Access 200010 and an option can be set that cascades changes in table and field names in many cases (e.g. to Forms featuring a field): however reference to a field/table in code will not be altered. Try to get names right first time. Deleting a field will require you to delete all references to it. Decreasing the size of a field, or converting from one format to another may result in data loss: you will be warned and given the option of quitting, if you attempt it.

7 Follow this procedure to save any object you create (Table, Query, Form, Report, Macro or Module). 8 Tip: take a copy of the table that you are going to change and keep it until you are happy that the modifications you have made have worked. To do so, highlight the table name in the Database Window, press Ctrl+C (copy) followed by Ctrl+V (paste), and indicate in the dialog box that displays that you wish to copy both structure and data. 9 In Table design view: if you want to position the field between existing fields, click Row on the Insert menu, or click on the Insert Rows button on the Table Design toolbar. 10 See Tools | Options + General Tab

11

Viewing Data To display the contents of a table in a table window, click the Tables tab, highlight the table and click the Open button. A window, like that in the figure below, will appear displaying several records. This is referred to as Datasheet View. Resize Colum Record Scroll Bar

Navigation Figure 6: Viewing data in a table

The Resize Handles on each corner of the window can be used to resize it. The Scroll Bar on the left, which appears when there are too many records to fit into the window, can be used to control which block of records currently displays, as can the Navigation Controls at the bottom11. The grey area to the left of each record is known as the Record Selector. The Record Selector corresponding to the current record (that most recently clicked) contains a symbol. A triangle denotes an unchanged record. A pen indicates a record currently being changed. A no-go sign (a crossed circle) may display to indicate that you are attempting to change a record which is currently locked (and possibly being changed) by another. An asterisk appears in the record selector of an empty record at the bottom of the table. The Column Selector can be used to resize or re-order the columns. To resize a column move the mouse to the right hand edge of the column that you wish to resize (the cursor displays as a two headed arrow), click and hold down the mouse and drag the column separator left or right, then release the mouse button. To re-order columns, select a column by clicking in the Column Selector, the column will highlight, click and hold down the mouse button within the column selector, and drag the small rectangle which appears, dropping it into the column to the left of which you wish the selected column to appear. To freeze columns, so that they always display, select them using the column selector and choose Freeze Columns from the Format menu.

11 The Navigation Controls (from left to right) cause the current record to change to the first record in the table, the previous record, the next record and the last record respectively. Click the asterisk to enter a new record.

12

Sorting The information displayed in a table window can be sorted in ascending or descending order based on a single field by clicking one of two Sort Buttons. If a more complex sort is required a query can be used.

Finding a Record in a Table The Binoculars button appears on the tool bar when a table is opened in datasheet view. It can be used to find a record that contains some specified string or pattern.

Figure 7: Finding a Record

In locating a record the search term (characters or pattern) specified in the Find What box is matched against data in records in the table in which the cursor was positioned when the binoculars were clicked. The target field can be changed by selecting a different field in the Look In box, or there is an option to search all fields in the target table. The attempted match can be against the Whole Field, the Start of the Field or Any Part of the Field depending on the option specified in the Match box. The direction of search can be specified in the Search box All, Up or Down. Specifying Match Case requests a case sensitive search. Specifying Search Fields as Formatted12 compares the search term against the data as formatted, otherwise it is compared against the data as it is stored in the field(s).

Special Characters (Wildcards) Patterns as well as words or phrases can be looked for. Special characters enable a pattern to be specified: * Means any sequence of characters. ? Means any single character. # Means any digit. [abc] Enclosing several characters in square brackets means any one of those characters. A range (or ranges) can also be specified [az] and negation can be specified: [!abc] is taken to mean any character except those specified.

12 It is normally recommended that you do Search Fields as Formatted.

13

Insert, Spell Check, Delete and Modify Data It is often preferable to enter and alter data through forms, detailed later in this document. However, a new record can be entered by filling in the blank record at the bottom of the table window - evidenced in figure 6 by an asterisk appearing as in its record selector. Existing records can be changed by appending information to fields, or by deleting or modifying the contents of fields13. Data can be deleted by highlighting it and cutting (or by pressing the Del key) or by positioning the cursor and backspacing over it. Data can be copied and pasted in the normal way14. Changes to the current field can be undone by pressing the Escape key - a second depression will undo any other changes that might have been made to the current record. Alternatively the Edit menu has a number of options that enable changes to the last record edited to be undone. Data can be checked for spelling mistakes. First select (highlight) the data to be checked and then click the Spelling icon on the toolbar A record can be deleted by selecting15 it and then pressing the Delete key, or by choosing Delete Record from the Edit menu. By default you will be prompted to confirm deletions.

Using Existing Data One of a number of choices can be exercised if data already exists and you wish to use it with Access.16 •

Access is capable of importing data from a number of other systems.17



Access can import data from ordinary text files provided each record occupies a single line and fields are delimited by a special character, or alternatively, occupy fixed column positions.



Database tables in other systems, or in another Access database, can be linked to an Access database18. In this case the data can be altered by Access but are maintained outside the Access database file. For multiuser databases it is often a good idea to have data in one database, and

13 The Tab key, Arrow keys, Home and End keys (amongst others) and the mouse can be used to move from field to field. 14 Position the cursor, hold down the left mouse button and drag the cursor until the wanted portion of data is highlighted. (Cut: Ctrl+X. Copy: Ctrl+C. Paste: Ctrl+V.) 15 Click on its record selector. Note: a contiguous group of records can be selected by clicking on the record selector of the first record and then with the shift key depressed clicking on the record selector of the last record. 16 The options are available under the File menu when the Database Window is current. 17 E.g. dBase, Excel, Lotus, Paradox, another Access database or defined ODBC data sources (e.g. SQL/Server). Use File | Get External Data | Import. 18 Use File | Get External Data | Link Tables…

14

all the objects that operate on it (Queries, Forms, Reports etc.) in another. This enables different versions of interfaces to be built and can enable users of different versions of Access to work against common data.

Exporting to Other Systems Data can be exported to all of those systems from which it can be imported.19

Queries One of the principal tasks of a database system is to enable wanted information to be retrieved. Access queries are designed for this purpose. They can be used to: •

Display records in a table that satisfy stated criteria.



Join related information from two or more tables.



Sort information.



Alter data: delete records or perform global inserts or updates.

Displaying Wanted Data from a Single Table The simplest sort of query involves displaying information from a single table that satisfies specified criteria. For example, to create a query which when executed displays the AccountId, Type, Branch and Balance of all current accounts: Click the Query button and then click the New button. A window like that illustrated below will appear.

Figure 8: The NewQuery Window

Selecting Design View and clicking on the OK button causes a Query Window to display containing a dialog box entitled Show Table, which displays a list of all the tables and/or queries20 that currently exist in the database.

19 Use File | Export…

20 Queries can be based on existing queries as well as tables - this is one of the most powerful features Access offers.

15

Figure 9: Query creation: selecting the information source(s)

Add21 the tables and/or queries upon which the query is to be based then Close the Add Table dialog box. The figure below shows the Query Window with one table (tbl_Account) added.

Drag Query Specification

Figure 10: Query Specification

The Query Specification determines the condition(s) records must satisfy to be output, which fields are output, and how output is sorted. The Fields appearing in the top row of the query specification are those which are needed because they contain data that will be queried and/or displayed. They can be specified by dragging and dropping from the table definition in the upper half of the window.22 The Show boxes are crossed to indicate that the corresponding field should appear as output - if one is blank the corresponding field will not appear.23

21 By clicking the Add button. Hereafter, when there is no confusion, the name of the button will be used like a verb - implying that it should be clicked without specifically saying so. 22 Left-click the field name you wish to appear in the Field: line and, holding down the mouse button, drag it across the window and drop it into the appropriate cell. 23 Click a box to toggle it between crossed and uncrossed.

16

The Criteria are simply typed in (Access will automatically add quotes if they are omitted). Wild card characters can also be used - see Special Characters (Wildcards). Note that Access queries are not case sensitive. To run a query, click the Datasheet View button on the tool bar. To return to the design click the Design View button.

Editing Through a Query Information can be edited through a query if certain conditions are met. A query is run and displays a set of records. Alterations can then be made to the underlying information in the tables upon which the query is based by altering the information displayed. If updating is not allowed, a message will display indicating that the query is not updatable.

Sorting Information Records can be sorted in ascending or descending order on specified fields. The sort order is determined by entries in the Sort row of the Query Specification.

Figure 11: Sorting

The output resulting from the above query specification would be sorted in ascending order according to Branch: where records have the same Branch they would appear in descending order of Balance.

Figure 12: Output

Operators The comparison operators (=, =, and (not equal)) can be used in the usual sense. E.g. Criteria: >2 The like operator is used in conjunction with wild card characters - it will be automatically added by Access if omitted. The Is operator can be used with the Null keyword to find missing information e.g. Criteria: Is Not Null excludes records with no information in the corresponding field.

AND, OR and NOT The AND, OR and NOT (Boolean) operators can be used, in conjunction with brackets if needs be, in specifying criteria. Complicated OR expressions can

17

be entered by filling in more than one of the Criteria rows. The following query will display details of Accounts that satisfy at least one of the following conditions: •

Managed at Branch 1, have type Savings or Current, and have a Balance of over £100 but below £1000.



Managed at Branch 2.

Figure 13: A query using comparison and Boolean operators

Expressions In the examples above, fields have been queried and displayed. However Access allows an expression as well as a field to be specified in the Field row of a query specification. The expression is treated in exactly the same way as a field. The query illustrated in the figure below outputs the AccountId, Type (in upper case), Branch, and 5% of the balance labelled as 5pc_balance. UCase and Format are built in Access functions - a complete list of which can be found in the Access Help system.24.

Figure 14: A Query using an Expression and its result

Similarly a field (or expression) specified in the Field row can be compared against another field or expression specified in the Criteria Row25.

24 A user -written function can also be designated. 25 The field in the criteria row is designated as such by enclosing its name in square brackets, e.g. [BranchId].

18

Groups and Summary Statistics Records can be grouped and simple summary statistics output on a per group basis.

Figure 15: Query using Group By and Sum

The query defined in the figure above outputs the sum and average of balances for each branch at which accounts are managed. Clicking in a cell on the Total26 row will display a list that will enable the definition of groups and the selection of summary statistics.

Joining Information In the examples above, queries have been directed at a single table. It is frequently the case, however, that queries need to be directed at several tables containing related information. Two tables, table1 and table2 say, may be joined over pairs of fields which are defined over the same data domain27. For each record in table table1, records in table2 are sought which have the same values in the join fields. A composite record is build from the pairs of records. The collection of all such records is the so-called natural join of table1 and table2 (often referred to as simply the join). This is illustrated by the example below where table1 and table2 to be joined - the join fields are a single pair: field2 from table1 and field1 from table2.

Join fields

Figure 16: Joining two tables 26 To display the Total row – select Totals from the View menu when in Query design

view. 27 I.e. they have data in common and are intended to reference each other.

19

A number of different sorts of join have been identified as useful. For example, note that the last record of table1 does not contribute a record to the natural join, because z does not appear as a value in table2 field1: we may want such an entry. A so-called outer join would generate a record – with a null entry in third column of the output. Access uses queries to join tables and supports both natural and outer joins. This is best illustrated by example. To find out details of Accounts including the address of the Branch at which they are managed it is necessary to join information in the tables tbl_Branch and tbl_Account.

Figure 17: A join in Access

Add the tables tbl_Accounts and tbl_Branches to a new query. The join columns are identified in the figure above by the line running between them. To ‘draw’ the join line, click on the BranchId field in the tbl_Branch, drag it over to tbl_Account table and drop it over the Branch field28. Release the mouse button and the line appears. Double clicking the join line displays the Join dialog box, shown on the left of the figure above, through which a natural or outer join can be specified.

Figure 18: Output from the Query specified in previous Figure

Query Properties Associated with each query are a number of properties, the settings of which affect the way it runs and what data is displayed. Those most commonly used are: 28 Tables will be joined automatically if a Relationship has been defined (see below).

20

Output All Fields: outputs all fields irrespective of all other specifications. Top Values: restricts the number of records displayed. Unique Values: set to Yes to display unique records only – i.e. duplicates will not display Run Permissions: Run either with the permission set of the user running the Query or with that of the Query creator.

Figure 19: The Query Properties Window

Field Properties Each field on the query specification has a number of properties that vary depending on its data type. E.g. the Format, Input Mask, and Caption properties are analogous to the properties of field in a table documented above.

Parameters Query parameters enable a query to be specified in general terms. For example, a parameter will enable a query to be constructed to display account details at a Branch which is specified only when the query is run. As in the example below, a parameter is specified by including a word or phrase in square brackets. The Query Parameters Dialog box can be used to constrain the type of data entered.29 When the query is run, the dialog box illustrated to the right of the figure below, displays.

29 To display the Query Parameters Dialog box choose Parameters... from the Query

menu.

21

Figure 20: Query Parameters

Global Editing In all the examples above queries have been used to select information these are termed select queries. Action queries can be used to insert, modify or delete information in database tables. The Query menu is used to specify the type of query and the format of the Query Specification varies according that chosen. The following is an example of an update query. When executed, it will change the Type from ‘Current’ to ‘Savings’ in all records in tbl_Account for which the value in the Branch field is 2.

Figure 21: An Update Query

To execute an action query click the Run button on the Tool Bar.

22

Relationships

Figure 22: The Relationships Window

Access enables relationships between pairs of tables to be declared. The primary purpose of a relationship is to enforce referential integrity: stopping a reference being made to a non-existent object. By way of example, in the figure above, there is a relationship defined between the tables tbl_Person and tbl_AccountOwner and referential integrity is enforced30. Any attempt is made to create a record in tbl_AccountOwner, which references a PersonId that does not exist that does not exist, will fail. The Cascade Update Related Fields and Cascade Delete Related Records options appear when referential integrity is enforced. They control how Access responds when the primary key of a record is updated, or when such a record is deleted. E.g. given the relationship illustrated in the figure above: if it was possible to alter the value of PersonId in tbl_Person, any attempt so to do would fail if a record in tbl_AccountOwner existed with the Person field referencing the old value. Similarly, an attempt to delete a record in tbl_Person would fail if there was a reference to it. •

If Cascade Update Related Fields was selected, the update of the record in tbl_ Person would succeed and any records in tbl_AccountOwner referencing the old value of PersonId would be updated to reference its new value.



If Cascade Delete Related Records was selected, the deletion of a record in tbl_Person would succeed, and any records in tbl_AccountOwner referencing it would also be deleted. To create or alter relationships, click the Database Window and choose Relationships... from the Tools menu - the Relationships Window will display and a Relationships menu will appear on the Menu Bar. New Tables can be added through Relationships menu. Relationships are specified in

30 Enforce Referential Integrity is selected.

23

the Relationships Window by dragging a field from the referenced table and dropping it over a field in the referencing table. E.g. to create the relationship between tbl_Branch and tbl_Account, the BranchId of tbl_Branch was dragged and dropped over the Branch field of tbl_Account. A dialog box will appear when a relationship is created, enabling it to be specified31. This dialog box can be re-displayed by double clicking on the line denoting a relationship.

Forms Access Forms are used principally as a means of viewing or editing data in tables. The information appearing on a form is typically from a table or generated by a query. A form can also contain sub-forms that display related information from other tables.

Creating a Form for a Single Table using the Form Wizard To create a form click on the Forms button and click the New button. The New Form dialog box displays. This shows several options, including several AutoForm32 options and the Form Wizard.

Figure 23: New Form Dialog Box

Select the table (or query) upon which the form is to be based, then select the Form Wizard from the list of options and click the OK button. A window like that illustrated in the figure below, will appear, enabling you to specify which fields that will appear on the form. The > button moves the currently highlighted field to the list that will appear on the form, >> moves all of the fields: The < and

Suggest Documents