Download Document

Document related concepts
no text concepts found
Transcript
WFM 5201: Data Management and Statistical Analysis © Dr. Akm Saiful Islam
WFM 5201: Data Management and
Statistical Analysis
Lab-2: Working with Microsoft Access 97
Akm Saiful Islam
Institute of Water and Flood Management (IWFM)
Bangladesh University of Engineering and Technology (BUET)
June, 2008
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Main Topics
Building a Database
 Working with Data
 Using Forms
 Creating an Application

WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Building a Database

Conceptual data design


Creating tables



E-R model
Reducing E-R model to tables
Creating tables in MS Access 97
Modifying database design

Deleting and renaming tables

Modifying fields within a table
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
E-R Model




Entity sets
Relationship sets
keys
Mapping constrains
Entity
Key
N
Attribute
Relationship
1
Entity
Key
Attribute
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Reducing E-R Model to Tables

Strong entity sets


Weak entity sets


Loan (loan-number, amount)
Payment (payment-number, pay-date, pay-amount)
Relationship sets
 Redundancy

Loan-Payment (loan-number, payment-number)
 Combination

of tables (links weak and strong entity)
of tables (many-to-one)
Branch(name, location)---has---account(account-number, amount)
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

1. Create a blank database (courses), click New to create a new table.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

2. Choose the Design View and click OK.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

3. Input the Fields Names, Descriptions, and select Data Types.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

4. Define the primary key.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

5. Add indexes.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

6. Input Index Name, choose Field Name and Sort Order.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating tables in MS Access 97

7. Save the table.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Deleting and Renaming Tables

Right-click the mouse on the table name that you want to delete or rename.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Modifying Fields within a Table

Changing field name

Open the table in Design View and change the field name
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Modifying Fields within a Table

Inserting and deleting fields

Open the table in Design View, use the toolbar or menus.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Modifying Fields within a Table

Copying fields

Open the table in Design View, use the Edit menu.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Modifying Fields within a Table

Moving fields

Open the table in Design View, drag and drop the row selector.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Working with Data

Using Datasheets


Creating and using queries



Select queries
Action queries
Importing, linking and exporting data


view, change, sort and search data
ODBC (Open Database Connectivity)
SQL
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Using Datasheets

View data
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Using Datasheets

Changing data (adding, modifying, copying, deleting)
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Using Datasheets

Sorting and searching data

Sorting using toolbar and searching using Find... in Edit menu
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Select Queries

1. Switch to the Queries folder.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Select Queries

2. Open a new query in Design View.
WFM 5201: Data Management and Statistical Analysis © Dr. Akm Saiful Islam
Creating and using queries

Select Queries

3. Choose the table(s) used to create the query.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Select Queries

4. Select the display fields of the query and input the selection criteria.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Select Queries

5. Save the query.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Select Queries

6. Double-click the query name or use Open to run the query.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Action queries

Update Query
 Append Query
 Delete Query
 Make-Table Query

Example:

Create an update query
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Create an update query

1. Create an select query with the criteria of the update query.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Create an update query

2. Convert the select query to update query.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating and using queries

Create an update query

3. Specify how you want the data changed.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Importing, linking and exporting data

ODBC (Open Database Connectivity)




Create a new ODBC data source
Importing
Linking
Exporting
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Create a New ODBC Data Source

1. Open the control Panel, and click the 32bit ODBC.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Create a New ODBC Data Source

2. In the User DSN folder, choose Add… to add your new data source.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Create a New ODBC Data Source

3. Select the driver for the new data source.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Create a New ODBC Data Source

4. Input the data source name and create the database.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Create a New ODBC Data Source

5. A new user data source forCS485 is added.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Importing Data







Importing dBASE files
Importing Paradox files
Importing FoxPro files
Importing spreadsheet files
Importing text files
Importing Access objects
Importing SQL tables
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Importing Data

Importing SQL tables
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Linking Data







Linking dBASE files
Linking Paradox files
Linking FoxPro files
Linking spreadsheet files
Linking text files
Linking Access objects
Linking SQL tables
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
SQL

In Microsoft Access, you can use SQL to:





create tables
build select queries
build append queries
build delete queries
build update queries
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
SQL

1. Open a new query in design view.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
SQL

2. Close the Show Table Dialog box without adding any table.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
SQL

3. Switch the query to SQL View.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
SQL

4. Write the SQL statement and save the query.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Using Forms

Building forms



Building forms from scratch
Building forms using Form Wizard
Advanced form design
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Building Forms from Scratch

1. Open a new form in Design View.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Building Forms from Scratch

2. Use the tools in the Toolbox to set the layout of the form.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Building Forms from Scratch

3. Add the command buttons.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Building Forms using Form Wizard

1. Open a new form using Form Wizard.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Building Forms using Form Wizard

2. Select the fields to be displayed on the form.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Advanced Form Design

1. Create a query (used for the results form) based on the course and
faculty tables.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Advanced Form Design

3. Set the layout of the form.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Advanced Form Design

4. Set the properties of the form and controls, and save the from.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Advanced Form Design

5. Build the main form (refer to Building Forms from Scratch).
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Advanced Form Design

7. Save the main form and run it.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Advanced Form Design

8. The form results is shown after click the Search button.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating an Application

Macros

Creating Macros
 Automating applications with Macros

VBA (Visual Basic Application)

Visual Basic in MS Access
 Automating applications with Visual Basic
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
… ...
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating Macros

1. Switch to the Macros folder.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating Macros

2. Open a new Macro.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating Macros

3. Select the Action, and input the Condition and Comment. Input or
choose the Action Arguments, and save the macro.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Creating Macros

4. Run the macro.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Automating Applications with Macros

Example: A database startup routine

1. Create a new macro.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Automating Applications with Macros

Example: A database startup routine

2. Specify the actions of the startup routine.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Automating Applications with Macros

Example: A database startup routine

3. Save the macro as Autoexec.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Automating Applications with Macros

Example: A database startup routine

4. Open the database that contains the macro Autoexec.
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Visual Basic in MS Access

Modules



VBA Debug Tools



Module Objects
Forms or Report Modules
Setting Breakpoints
Using Debug Window
Collections and Objects
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Modules

Module Objects
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Modules

Forms or Report Modules
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
VBA Debug Tools

Setting Breakpoints
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
VBA Debug Tools

Using Debug Window
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Collections and Objects

Collections


Forms Collection, Reports Collection, Modules Collection
Objects

Forms, Reports, Module


Controls


Me![FID]
DoCmd, Screen


Forms![search]
DoCmd.OpenForm FormName:="results", whereCondition:=searchCondition
Database, RecordSet


Dim db As Database
Dim rcds As RecordSet
| Set db = CurrentDb()
| Set rcds = db.OpenRecordSet(“queryName”)
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
Automating applications with Visual Basic

Example: Get search conditions from user input
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
WFM 6103: hydrologic Information System © Dr. Akm Saiful Islam
Automating applications with Visual Basic

Example: Use SQL query to check result before display the form
WFM WFM
5201:6103:
Data Management
and Statistical
Analysis
Dr.Saiful
Akm Saiful
hydrologic Information
System
© Dr. ©
Akm
Islam Islam
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.
Related documents