Introduction of Running Microsoft Access 97

Download Report

Transcript Introduction of Running Microsoft Access 97

Introduction of Running
Microsoft Access 97
Source: USC Database Course (SC485)
Main Topics
Building a Database
Working with Data
Using Forms
Creating an Application
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
E-R Model
Entity sets
Relationship sets
keys
Mapping constrains
Entity
Key
N
Attribute
Relationship
1
Entity
Key
Attribute
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)
Creating tables in MS Access 97
 1. Create a blank database (courses), click New to create a new table.
Creating tables in MS Access 97
 2. Choose the Design View and click OK.
Creating tables in MS Access 97
 3. Input the Fields Names, Descriptions, and select Data Types.
Creating tables in MS Access 97
 4. Define the primary key.
Creating tables in MS Access 97
 5. Add indexes.
Creating tables in MS Access 97
 6. Input Index Name, choose Field Name and Sort Order.
Creating tables in MS Access 97
 7. Save the table.
Deleting and Renaming Tables
 Right-click the mouse on the table name that you want to delete or
rename.
Modifying Fields within a Table
 Changing field name
 Open the table in Design View and change the field name
Modifying Fields within a Table
 Inserting and deleting fields
 Open the table in Design View, use the toolbar or menus.
Modifying Fields within a Table
 Copying fields
 Open the table in Design View, use the Edit menu.
Modifying Fields within a Table
 Moving fields
 Open the table in Design View, drag and drop the row selector.
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
Using Datasheets
 View data
Using Datasheets
 Changing data (adding, modifying, copying, deleting)
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
Using Datasheets
 Sorting and searching data
Sorting using toolbar and searching using Find... in Edit menu
Creating and using queries
 Select Queries
 1. Switch to the Queries folder.
Creating and using queries
 Select Queries
 2. Open a new query in Design View.
Creating and using queries
 Select Queries
 3. Choose the table(s) used to create the query.
Creating and using queries
 Select Queries
 4. Select the display fields of the query and input the selection criteria.
Creating and using queries
 Select Queries
 5. Save the query.
Creating and using queries
 Select Queries
 6. Double-click the query name or use Open to run the query.
Creating and using queries
 Action queries
 Update Query
 Append Query
 Delete Query
 Make-Table Query
 Example:
 Create an update query
Creating and using queries
 Create an update query
 1. Create an select query with the criteria of the update query.
Creating and using queries
 Create an update query
 2. Convert the select query to update query.
Creating and using queries
 Create an update query
 3. Specify how you want the data changed.
Importing, linking and exporting
data
 ODBC (Open Database Connectivity)
Create a new ODBC data source
 Importing
 Linking
 Exporting
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.
Create a New ODBC Data Source
 1. Open the control Panel, and click the 32bit ODBC.
Create a New ODBC Data Source
 2. In the User DSN folder, choose Add… to add your new data source.
Create a New ODBC Data Source
 3. Select the driver for the new data source.
Create a New ODBC Data Source
 4. Input the data source name and create the database.
Create a New ODBC Data Source
 5. A new user data source forCS485 is added.
Importing Data







Importing
Importing
Importing
Importing
Importing
Importing
Importing
dBASE files
Paradox files
FoxPro files
spreadsheet files
text files
Access objects
SQL tables
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.
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.
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.
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.
Importing Data
 Importing SQL tables
Linking Data







Linking
Linking
Linking
Linking
Linking
Linking
Linking
dBASE files
Paradox files
FoxPro files
spreadsheet files
text files
Access objects
SQL tables
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.
Exporting Data








Exporting
Exporting
Exporting
Exporting
Exporting
Exporting
Exporting
Exporting
to
to
to
to
to
to
to
to
dBASE file
Paradox files
FoxPro files
spreadsheet files
text files
Access objects
SQL tables
a web page
SQL
 In Microsoft Access, you can use SQL to:
 create tables
 build select queries
 build append queries
 build delete queries
 build update queries
SQL
 1. Open a new query in design view.
SQL
 2. Close the Show Table Dialog box without adding any table.
SQL
 3. Switch the query to SQL View.
SQL
 4. Write the SQL statement and save the query.
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
Using Forms
 Building forms
Building forms from scratch
Building forms using Form Wizard
Advanced form design
Building Forms from Scratch
 1. Open a new form in Design View.
Building Forms from Scratch
 2. Use the tools in the Toolbox to set the layout of the form.
Building Forms from Scratch
 3. Add the command buttons.
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.
Building Forms using Form Wizard
 1. Open a new form using Form Wizard.
Building Forms using Form Wizard
 2. Select the fields to be displayed on the form.
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.
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.
Advanced Form Design
 1. Create a query (used for the results form) based on the course and
faculty tables.
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.
Advanced Form Design
 3. Set the layout of the form.
Advanced Form Design
 4. Set the properties of the form and controls, and save the from.
Advanced Form Design
 5. Build the main form (refer to Building Forms from Scratch).
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
… ...
 Close
DoCmd.Close
Advanced Form Design
 7. Save the main form and run it.
Advanced Form Design
 8. The form results is shown after click the Search button.
Creating an Application
 Macros
 Creating Macros
 Automating applications with Macros
 VBA (Visual Basic Application)
 Visual Basic in MS Access
 Automating applications with Visual Basic
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.
 … ...
Creating Macros
 1. Switch to the Macros folder.
Creating Macros
 2. Open a new Macro.
Creating Macros
 3. Select the Action, and input the Condition and Comment. Input or
choose the Action Arguments, and save the macro.
Creating Macros
 4. Run the macro.
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.
Automating Applications with
Macros
 Example: A database startup routine
 1. Create a new macro.
Automating Applications with
Macros
 Example: A database startup routine
 2. Specify the actions of the startup routine.
Automating Applications with
Macros
 Example: A database startup routine
 3. Save the macro as Autoexec.
Automating Applications with
Macros
 Example: A database startup routine
 4. Open the database that contains the macro Autoexec.
Visual Basic in MS Access
 Modules
Module Objects
Forms or Report Modules
 VBA Debug Tools
Setting Breakpoints
Using Debug Window
 Collections and Objects
Modules
 Module Objects
Modules
 Forms or Report Modules
VBA Debug Tools
 Setting Breakpoints
VBA Debug Tools
 Using Debug Window
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
Dim rcds As RecordSet
| Set db = CurrentDb()
| Set rcds = db.OpenRecordSet(“queryName”)
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 Select
End Function
Automating applications with Visual
Basic
 Example: Get search conditions from user input
Automating applications with Visual
Basic
 Example: Use SQL query to check result before display the form
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.