Chapter 1 Introduction to Visual Basic

Download Report

Transcript Chapter 1 Introduction to Visual Basic

7
Chapter 7
Database Basics
Tables,
Records, and
Fields
By Carlotta Eaton
Exploring Microsoft Visual Basic 6.0
Copyright © 1999 Prentice-Hall, Inc.
Objectives
1. Explain the differences between a flat-file
database and a relational database.
2. Explain the basic terminology for
databases such as table, record and field.
3. Implement a flat-file database using
random access.
4. Explain the use of the Microsoft FlexGril
control.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
2
Database Concepts...
Database - collection of data for a
particular purpose that is stored and
later retrieved
Flat-file database - database that is
stored in a single table
Relational database - database that is
stored in multiple tables and crossreferenced
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
3
Database Concepts
Field - the smallest unit of data (or fact) in
a database
Record - consists of several fields; stores
data about a single entity in a database
such as a client, product, student
Table - represents data where each row
represents a record, and each column
represents a field
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
4
The Type statement...
 Create records by defining a new data type
 Use the Type statement to define and create a
new data type
Example:
Type Person
Firstname As String * 25
Lastname As String * 50
Birthdate As String * 35
City As String * 25
State As String * 2
End Type
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
5
The Type statement
Type variablename
elementname1 As datatype
elementname2 As datatype...
End Type
Where
variablename is a programmer-named variable
elementname is a programmer-named element
(or field) contained within the new type
datatype is a Visual Basic data type such as Integer,
Currency, String etc.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
6
Declaring a New Data Type
Public variablename As Typename
Where
variablename is a programmer named
variable
typename is a programmer named data type
as defined with a type statement
Example:
Public CurrentRecord As Person
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
7
Birthday Database
Create a flat-file
database. Store
the records in
binary file
format, and use
random access.
Notice the 7 fields
in the displayed
record.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
8
Birthday Database
Hands-On Exercise 1






Create a new project
Set properties and add code for the Splash
form
Add code and test the module
Create the Main form
Add record declarations and code to the
module
Add more code and test the start procedure
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
9
Navigating the Database...
Variables in the Birthday database





CurrentRecord - stores current record data
NewRecord - flag that indicates if the record
needs to be written to the database file
RecordNumber - the number of the current
record
MaxNumber - the number of records in the
database
OldRecordNumber - previous record number
displayed in form
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
10
Navigating the Database...
Procedures in the Birthday Database
Main - opens the database file, and displays the
Splash form
 Start - displays the Main form
 AddRecord - adds a new record
 DeleteRecord - deletes a record
 SaveRecord - writes a record to the file
 DisplayRecord - reads a record and displays it
in the Main form
 ExitApplication - closes file, unloads form,
exits
Exploring MS Visual
Copyright 1999 Prentice-Hall, Inc.
11

Basic 6
Navigating the Database
lblNumber
cmdPrevious
cmdNext
cmdFirst
cmdLast
Move
First
Move
Move
Previous
Exploring MS Visual
Basic 6
Caption
Copyright 1999 Prentice-Hall, Inc.
Move
Last
Next
12
Continue the Birthday Database
Hands-On Exercise 2









Add code for AddRecord, DisplayRecord, SaveRecord,
and ExitApplication procedures
Add code to load & unload the Main form
Add code for Add and Save Record buttons
Test Start procedure and code for a new database
Add first and last record and exit
Add code and test first, last, next and previous buttons
Test navigation buttons
Test and add more records
Exit the application
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
13
The Microsoft FlexGrid Control
 Specially designed to display tables or grids of
data
 Use to sort, merge, and format tables
 Add to Toolbox first
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
14
FlexGrid Properties
FlexGrid control has over 100 different properties
Property
Description
Name
Name used to refer to the control in the code
Cols, Rows
Number of columns or rows displayed
Col, Row
Current column and row in grid
ColWidth
Width of current column
RowHeight
Height of current row
Text
Contents of current cell
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
15
Finish the Birthday Database
Hands-On Exercise 3
 Add
code and test Cancel button
 Add code and test Delete button
 Create, add code and test the List form
 Add code for the GridHeadings and
GridCells procedures
 Test the FlexGrid control
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
16
Summary ...
 A database is a collection of data
 A table consists of rows (records) and
columns (field)
 A field is the smallest unit of data
 A record is composed of several fields and
stores data about a single entity
 A flat-file database consists of a single
table
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
17
Summary
 Use the Type statement to create your
own data types
 The Microsoft FlexGrid control is designed
to display tables or grids of data
 The Birthday database
demonstrates a simple flat-file database
application
 has shortcomings that can be overcome by
adding more code such as searching and
handling deleted records

Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
18
Practice with Visual Basic
1. WindChill Table
2. Personal Address Book
3. Practice with Access
4. Birthday Deletion Improvement
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
19
Case Studies
Database Mystery
Beginning Database Design
Flat-file Database
FlexGrid Case Study
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc.
20