CSCI 220
Programming With Visual Basic
Lecture 13- The Data Access Control
Overview of Data Access
Allows programmer to build database with 3rd party
program
Allows for large, complex databases
Encourages use of relational (multi-table) databases
Makes Index management simple
Can read several different kinds of databases
Requires little code for simplest incarnation
Creating a database
Use a database management system such as:
DBase, Foxpro, Access
Create data format, enter starting data if preferred
Define tables, fields, and indices
Save the database
Use the data manager program to create an Access database
DATAMGR.EXE in VB directory
Allows development and rudimentary editing of ACCESS
format databases
Create Database
Create Table (start with 1)
Define fields
Define Indices
Close Data manager
The Data Control
Select the data control from the toolbar and draw it on
your form
Set the DatabaseName property to whatever you called your
database
If you used a format other than ACCESS, use the connect
property to indicate which type
Set the recordsource property to the table you wish to
access
Bound controls:
The following controls can be bound to a database:
Text boxes, Picture boxes, Images, Labels, and Check
boxes
Draw an appropriate control on the form
Set the DataSource property to the name of the Data
Control (Data1)
Set the DataField property to the field you wish the
object to reflect
When running, changes to this field will automatically be
reflected in the database
Database changes will also be reflected in this field
automatically.
Creating a new record
Create a command button with the following code:
Data1.Recordset.AddNew
Moving around the database
VCR - like control buttons are already attached to the
Data control.
Language
The RecordSet object
Handy commands
Move commands
Find Commands
Searching
SQL
Using on - line help
Program design considerations:
Getting help
Use the Search command on help menu or...
Type a keyword, place the cursor on that word, and
press F1 key
Don't be afraid to experiment.
Steps for designing a program
Define the program in English: What will it do?
Sketch out a design for the program's main form(s)
Determine what controls will be used to solve the
problem.
Write (in English) what will happen when the user
activates key controls
Translate those directions into more precise pseudocode
Translate pseudocode into Code
Test and Debug
Debugging
Run your program
Try different input methods
Try to mess it up
Errors will happen
Tracing crashes
Look at what was happening on screen when program crashed
Try to anticipate what code was operating.
Guess what went wrong.
look carefully at error message
Press F1 for help if you don't understand
look at where cursor is when you are returned to code.
Note that computer sets point where it NOTICED error,
NOT necessarily where error OCCURRED
Bugs
Program doesn't crash outright, but still doesn't perform
as you expected
Follow same technique as for crashes
Print values of appropriate control variables to screen,
temp labels
Use of DEBUG window
Debug menu Watch etc.