chapter 8 relational databases activex database controls 8 exploring microsoft visual basic 6.0...
TRANSCRIPT
Chapter 8Relational Databases
ActiveX Database Controls
8
Exploring Microsoft Visual Basic 6.0Copyright © 1999 Prentice-Hall, Inc.
By Carlotta Eaton
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 2
Objectives...
1. Basic terminology such as tables and relationships
2. Use controls specialized for databases such as the data control, data-bound list box, data-bound combo box, and data-bound grid
3. Differences between FlexGrid control and data-bound grid control
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 3
Objectives
4. Implement a front-end application for an existing Access database
5. Use Visual Data Manager6. Create a new database
application with Application Wizard7. Create new forms with the Data
Form Wizard
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 4
Overview
Access is the relational database program included with Office Professional Edition
Minimize code using data-bound controls
Use standard ActiveX controls and the FlexGrid control
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 5
Relational Databases...
Relational databases - composed of tables and relationships
Table - consists of rows of records and columns of fields
Relationship - association between fields in two tables one-to-one one-to-many many-to-many
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 6
Relational Databases...
Primary key - field(s) that uniquely determines a record in a database
Foreign key - used to defined relationships between tables - a field in a table that also serves as a primary key in another table
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 7
Birthday Access DatabaseBirthdays Table
Occupations Table
Primary Key
Foreign Key
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 8
Database Controls...
Data control - use for each table we want to work with in the database
Data control Properties DatabaseName property - path and
filename of the existing database RecordSource property - select from
the list of tables in the database
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 9
Birthday Access Database
Data Control for the Occupations Table
Data Control for the Birthdays Table
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 10
ActiveX Database Controls...
Data-bound list control - displays a list box using a database field
Data-bound combo control - displays combo box using a database field
Data-bound grid control - displays a grid using a database field(s)
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 11
ActiveX Database Controls...
Properties to set before utilizing Row Source List Field Text
Properties used to update and manage the database Data Source Data Field Bound Column
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 12
ActiveX Database Controls
Properties must be set in order to work correctly1) Row Source2) List Field 3) Text Property4) Data Source5) Data Field6) Bound
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 13
Database Tools
Picture Label
Text boxCombo box
Check box
List box Data
Image OLE container
DBList DBGrid
DBCombo
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 14
More Properties
Data-bound Grid Control Data Source property
Intrinsic Data-Aware Control Properties Data Source property Data Field property
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 15
Birthday Database Using Access
Hands-On Exercise 1 Create project, then add and edit forms Add the Data control Edit the module code Edit and verify the Main form code Test the Splash and Main forms Add another Data control, and a Data-bound
combo box Test the Data-bound combo control
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 16
Managing and Displaying Records
Recordset - a record(s) selected from a table(s) in a databaseTable recordsets - limited to a single table,
and can modify recordsDynaset recordsets - can be used for multiple
tables, and can modify recordsSnapshot recordsets - can be used for
multiple tables, but cannot modify records
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 17
Navigating Records with Data Control Methods
Name.Recordset.Method
Where
Name is the name of the data control
Method is one of the following:
MoveFirst moves to the first record
MovePrevious moves to the previous record
MoveNext moves to the next record
MoveLast moves to the last record
Example:
datBirthdays.Recordset.MoveFirst
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 18
Finding Records with the Data Control
Name.Recordset.Method
Where
Name is the name of the data control
Method is one of the following:
FindFirst finds the first record
FindPrevious finds the previous record
FindNext finds the next record
FindLast finds the last record
Example:
datBirthdays.Recordset.FindFirst “LastName = Dion”
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 19
Adding and Deleting Records with the Data
ControlName.Recordset.Method
Where
Name is the name of the data control
Method is one of the following:
AddNew adds a new record to the end of the database
Refresh redisplays the current record
Update modifies the fields in the current record
Delete deletes the current record from the database
Close closes the recordset, and frees the memory
allocated for storage
Example: datBirthdays.Recordset.AddNew
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 20
Finish the Birthday Access Project
Hands-On Exercise 2 Edit the Module code Test the Add Record and Delete Record
code Add code and test the Find Record button
and procedure Modify the List form and add the Grid
controls Test the List form
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 21
More Database Tools...
Different ways to access data ActiveX Data Objects (ADO) Remote Data Objects (RDO) Data Access Objects (DAO)
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 22
More Database Tools
Learning Edition Tools ActiveX database controls Intrinsic data-aware controls
Professional Edition Tools Data View windows (new to VB 6.0) Data Environment Design Data Report feature
Enterprise Edition Tools Query Designer Database Designer
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 23
Add-Ins
Add-ins: tools that are programs that customize and extend the Visual Basic environment1) Add-ins: generic term
2) Builders: helps view or set control properties3) Wizards: leads a user step-by-step through a
task4) Utilities: add-in that may be run outside
Visual Basic
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 24
Visual Data Manager
Add a Field Remove a Field
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 25
Visual Data Manager and the Birthday Database
Hands-On Exercise 3 Create a new project & new database
Create a new table and add fields Add a record to the database Use the Application Wizard Use the Data Form Wizard Finish the Application Wizard Test the Update form and the Grid form
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 26
Summary ...
Access included with Microsoft Office Professional Edition
Relational databases composed of several tables and relationships
Relationships established between two tables using a foreign key
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 27
Summary ...
ActiveX database controls include Data-bound list box Data-bound combo box Data-bound grid control FlexGrid control
Data control must be used for each table we want to access
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 28
Summary ...
Navigate database using recordsets Table recordset Dynaset recordset Snapshot recordset
Define new databases using the Visual Data Manager add-in included in all 3 editions of Visual Basic
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 29
Summary
Data Form Wizard (new with VB 6.0) helps create database forms
Application Wizard creates general forms such as Splash and About forms for applications
Exploring MS Visual Basic 6
Copyright 1999 Prentice-Hall, Inc. 30
Practice with Visual Basic
1. Demonstration: Data Browser Program2. Sample Program: Bibliography
Database Browser3. Demonstration: Music Review Notepad4. Personal Address Book5. Access Demo6. Access Demo Front End