92
Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Embed Size (px)

Citation preview

Page 1: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Introduction of Running Microsoft Access 97

Source: USC Database Course (SC485)

Page 2: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Main Topics

Building a DatabaseWorking with DataUsing FormsCreating an Application

Page 3: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building a Database

Conceptual data design E-R model

Creating tables Reducing E-R model to tables Creating tables in MS Access 97

Modifying database design Deleting and renaming tables

Modifying fields within a table

Page 4: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

E-R Model

Entity setsRelationship setskeysMapping constrains

Entity EntityRelationship

Key Key Attribute Attribute

N 1

Page 5: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Reducing E-R Model to Tables

Strong entity sets Loan (loan-number, amount)

Weak entity sets Payment (payment-number, pay-date, pay-amount)

Relationship sets Redundancy of tables (links weak and strong

entity) Loan-Payment (loan-number, payment-number)

Combination of tables (many-to-one) Branch(name, location)---has---account(account-number,

amount)

Page 6: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

1. Create a blank database (courses), click New to create a new table.

Page 7: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

2. Choose the Design View and click OK.

Page 8: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

3. Input the Fields Names, Descriptions, and select Data Types.

Page 9: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

4. Define the primary key.

Page 10: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

5. Add indexes.

Page 11: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

6. Input Index Name, choose Field Name and Sort Order.

Page 12: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating tables in MS Access 97

7. Save the table.

Page 13: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Deleting and Renaming Tables

Right-click the mouse on the table name that you want to delete or rename.

Page 14: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Modifying Fields within a Table

Changing field name Open the table in Design View and change the field name

Page 15: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Modifying Fields within a Table

Inserting and deleting fields Open the table in Design View, use the toolbar or menus.

Page 16: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Modifying Fields within a Table

Copying fields Open the table in Design View, use the Edit menu.

Page 17: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Modifying Fields within a Table

Moving fields Open the table in Design View, drag and drop the row

selector.

Page 18: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Working with Data

Using Datasheets view, change, sort and search data

Creating and using queries Select queries Action queries

Importing, linking and exporting data ODBC (Open Database Connectivity)

SQL

Page 19: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Using Datasheets

View data

Page 20: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Using Datasheets

Changing data (adding, modifying, copying, deleting)

Page 21: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Using Datasheets

Modifying datasheet format Using the Format menu, you can:

Selecting fonts Changing row height and column width Moving columns Hiding and showing columns freezing columns Removing gridlines

Page 22: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Using Datasheets

Sorting and searching data Sorting using toolbar and searching using Find... in

Edit menu

Page 23: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Select Queries 1. Switch to the Queries folder.

Page 24: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Select Queries 2. Open a new query in Design View.

Page 25: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Select Queries 3. Choose the table(s) used to create the query.

Page 26: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Select Queries 4. Select the display fields of the query and input the selection

criteria.

Page 27: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Select Queries 5. Save the query.

Page 28: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Select Queries 6. Double-click the query name or use Open to run the query.

Page 29: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Action queries Update Query Append Query Delete Query Make-Table Query

Example: Create an update query

Page 30: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Create an update query 1. Create an select query with the criteria of the update query.

Page 31: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Create an update query 2. Convert the select query to update query.

Page 32: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating and using queries

Create an update query 3. Specify how you want the data changed.

Page 33: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing, linking and exporting data

ODBC (Open Database Connectivity) Create a new ODBC data source

Importing Linking Exporting

Page 34: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

ODBC

ODBC (Open Database Connectivity) is a SQL database interface developed by Microsoft. It allow one database product that “speaks” SQL be able to “talk” to other database product that understand SQL.

With ODBC, Microsoft Access can import (copy in) or link (connect to) data that is in text files, spreadsheets, other Access database, dBASE, Paradox, Microsoft FoxPro, and other SQL database that support ODBC. Microsoft Access can also export (copy out) data from Access tables to the databases, spreadsheets, Web pages, or test files of other application.

Page 35: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Create a New ODBC Data Source

1. Open the control Panel, and click the 32bit ODBC.

Page 36: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Create a New ODBC Data Source

2. In the User DSN folder, choose Add… to add your new data source.

Page 37: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Create a New ODBC Data Source

3. Select the driver for the new data source.

Page 38: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Create a New ODBC Data Source

4. Input the data source name and create the database.

Page 39: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Create a New ODBC Data Source

5. A new user data source forCS485 is added.

Page 40: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing Data

Importing dBASE files Importing Paradox files Importing FoxPro files Importing spreadsheet files Importing text files Importing Access objects Importing SQL tables

Page 41: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing Data

Importing dBASE files, Paradox files, FoxPro files 1. Open the database to import the file. 2. Choose the Get External Data from the File menu, then choose

the Import submenu.

3. In Import dialog box, select the appropriate file type and file name.

4. Click the Import button to import the file. 5. Access responses with a message box that informs the result of

the importing procedure (i.e., successfully imported ‘xxxxx’). 6. Click the OK button to confirm the import procedure.

Page 42: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing Data

Importing spreadsheet Steps 1-4 are the same as previous one. 5. An Import Spreadsheet Wizard dialog box is popped up.

Simply follow the instruction of the wizard to finish specifying the detail of the table (i.e., choose whether first row contains column heading, select index and key, etc.).

6. Access responses with a message box that informs the result of the importing procedure.

7. Click the OK button to confirm the import procedure.

Page 43: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing Data

Importing text files Steps 1-4 are the same as previous one. 5. An Import Text Wizard dialog box is popped up. Simply

follow the instruction of the wizard to finish specifying the detail of the table (i.e., choose whether the data format is delimited or fixed width, whether first row contains column heading, to import to an existing table or a new table, etc.).

6. Access responses with a message box that informs the result of the importing procedure.

7. Click the OK button to confirm the import procedure.

Page 44: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing Data

Importing Access objects Steps 1-4 are the same as previous one. 5. An Import Objects dialog box is popped up. Choose the

object to be imported and confirm the importing procedure.

Page 45: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing Data

Importing SQL tables

Page 46: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Linking Data

Linking dBASE files Linking Paradox files Linking FoxPro files Linking spreadsheet files Linking text files Linking Access objects Linking SQL tables

Page 47: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Importing vs. Linking

Importing File is relatively small. Data is not changed frequently by users of other database

application. Data need not to be shared with other database application. Best performance is desired.

Linking File is large (i.e., larger than maximum capacity of local Access

database [1 GB]). Data is changed frequently by users of other database application. Data need to be shared over network with other database

applications. Performance does not matter.

Page 48: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Exporting Data

Exporting to dBASE file Exporting to Paradox files Exporting to FoxPro files Exporting to spreadsheet files Exporting to text files Exporting to Access objects Exporting to SQL tables Exporting to a web page

Page 49: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

SQL

In Microsoft Access, you can use SQL to: create tables build select queries build append queries build delete queries build update queries

Page 50: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

SQL

1. Open a new query in design view.

Page 51: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

SQL

2. Close the Show Table Dialog box without adding any table.

Page 52: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

SQL

3. Switch the query to SQL View.

Page 53: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

SQL

4. Write the SQL statement and save the query.

Page 54: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Using Forms

Forms are the primary interface between users and Microsoft Access application.

Using Forms in Microsoft Access, you can: Displaying and editing data Controlling application flow Accepting input Displaying message Printing information

Page 55: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Using Forms

Building forms Building forms from scratch Building forms using Form Wizard Advanced form design

Page 56: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms from Scratch

1. Open a new form in Design View.

Page 57: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms from Scratch

2. Use the tools in the Toolbox to set the layout of the form.

Page 58: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms from Scratch

3. Add the command buttons.

Page 59: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms from Scratch

4. Set the properties of the controls (i.e., specify the event procedure of On Click for the Search button) and save the form.

Page 60: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms using Form Wizard

1. Open a new form using Form Wizard.

Page 61: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms using Form Wizard

2. Select the fields to be displayed on the form.

Page 62: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Building Forms using Form Wizard

2. Follow the instructions to select the layout (i.e., Columnar) and style (i.e., Stone) of the form.

3. Specify the name and save the form.

Page 63: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

Basing a form on a multiple-tables query Creating Multiple-page forms Creating and embedding subform Linking forms with a command button

Example: a simple search on Course and Faculty tables A main form (search) used to input the search condition. A second form (results) used to display the the result of

the search.

Page 64: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

1. Create a query (used for the results form) based on the course and faculty tables.

Page 65: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

2. Create a new form as the results form, specify the query created in step 1 as the data source of the form.

Page 66: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

3. Set the layout of the form.

Page 67: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

4. Set the properties of the form and controls, and save the from.

Page 68: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

5. Build the main form (refer to Building Forms from Scratch).

Page 69: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

6. Write the event procedure for the Search and Close button. Search

/* check input and form the search condition */If Not IsNothing(Me!FID) Then searchCondition = " FID = " & Me!FID… …/* open the subform results with the desired search condition */

DoCmd.OpenForm FormName:="results", whereCondition:=searchCondition

… ...

CloseDoCmd.Close

Page 70: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

7. Save the main form and run it.

Page 71: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Advanced Form Design

8. The form results is shown after click the Search button.

Page 72: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating an Application

Macros Creating Macros Automating applications with Macros

VBA (Visual Basic Application) Visual Basic in MS Access Automating applications with Visual Basic

Page 73: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Macros

Using macros in MS Access, you can: Open any table, query, form, or report in any available view. Rename any objects in the database. Execute other macros or VBA functions. Emulate keyboard actions and supply input to system dialog

boxes. Execute any command on any Access menu. Display informative message or sound a beep to draw

attention. Send the data from a table, query, form or report to an output

file. … ...

Page 74: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating Macros

1. Switch to the Macros folder.

Page 75: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating Macros

2. Open a new Macro.

Page 76: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating Macros

3. Select the Action, and input the Condition and Comment. Input or choose the Action Arguments, and save the macro.

Page 77: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Creating Macros

4. Run the macro.

Page 78: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating Applications with Macros

Example: A database startup routine Display a Welcome message when the database application

start up. Next open a form that used to display course information of

CS department. Display the information of course cs485 as default view.

Page 79: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating Applications with Macros

Example: A database startup routine 1. Create a new macro.

Page 80: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating Applications with Macros

Example: A database startup routine 2. Specify the actions of the startup routine.

Page 81: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating Applications with Macros

Example: A database startup routine 3. Save the macro as Autoexec.

Page 82: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating Applications with Macros

Example: A database startup routine 4. Open the database that contains the macro Autoexec.

Page 83: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Visual Basic in MS Access

Modules Module Objects Forms or Report Modules

VBA Debug Tools Setting Breakpoints Using Debug Window

Collections and Objects

Page 84: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Modules

Module Objects

Page 85: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Modules

Forms or Report Modules

Page 86: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

VBA Debug Tools

Setting Breakpoints

Page 87: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

VBA Debug Tools

Using Debug Window

Page 88: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Collections and Objects

Collections Forms Collection, Reports Collection, Modules Collection

Objects Forms, Reports, Module

Forms![search] Controls

Me![FID] DoCmd, Screen

DoCmd.OpenForm FormName:="results", whereCondition:=searchCondition

Database, RecordSet Dim db As Database | Set db = CurrentDb() Dim rcds As RecordSet | Set rcds =

db.OpenRecordSet(“queryName”)

Page 89: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating applications with Visual Basic

Example: A public function that checks user inputs of a form

Function IsNothing(v As Variant) As Integer IsNothing = True Select Case VarType(v) Case vbEmpty Exit Function Case vbNull Exit Function Case vbBoolean If varToTest Then IsNothing = False Case vbByte, vbInteger, vbLong, vbSingle, vbDouble, vbCurrency If v <> 0 Then IsNothing = False Case vbDate IsNothing = False Case vbString If (Len(v) <> 0 And v <> " ") Then IsNothing = False End SelectEnd Function

Page 90: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating applications with Visual Basic

Example: Get search conditions from user input

Page 91: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Automating applications with Visual Basic

Example: Use SQL query to check result before display the form

Page 92: Introduction of Running Microsoft Access 97 Source: USC Database Course (SC485)

Macros vs. VBA

Macros Don’t need to trap errors Application consists of only a few forms. Defining alternative actions for certain keystrokes. Creating a startup routine that runs when the database opens.

VBA Need to discrete error handling. Define new functions. Handle events that pass parameters or accept return values. Create new objects (tables, queries, forms…) from application

code Directly call Windows API functions. Manipulate data in a recordset on a record-by-record basis.