Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Part 5
Database Programming

Chapter 19
Database Basics

One of the most outstanding features of Visual Basic is the powerful assortment of database programming tools it provides. In this chapter, we’ll start examining these tools.

If you surveyed all the computers in the world, what kind of program would be running most often? Not games, not word processors, but database programs. When a long distance company calls to pitch its service, the agent uses a database program. When someone sends an order for elk-lined pajamas to L.L. Bean, the order goes into a database program. When the clerk at the auto parts store checks to see whether they have a left-handed cam inverter for your 1971 Ford Falcon, he or she uses a database program. Wherever information needs to be managed, you usually find a database program at work.

A good percentage of those programs were created with Visual Basic. Recognizing that database programming is in great demand, Microsoft wisely included a slew of powerful database tools in Visual Basic. The Basic language itself has all the features you need to create a simple database program from scratch, as you’ll see in the early part of this chapter. Visual Basic also includes some specialized database tools that permit you to create sophisticated user interfaces and access a variety of standard database file formats, all with relatively little programming.

We’ll look at some of Visual Basic’s database tools later in the chapter. First, we need to get some basics under our belt.

So, What’s A Database?

A database is a collection of information, or data, arranged in a particular manner. The basic unit of information in a database is called a record. Each record in a database contains two or more fields that hold specific types of information. Perhaps the most common example of a database is an address list. Each entry in the database constitutes one record: an individual’s name and address information. Each record in the database contains fields that hold separate items of information: first name, last name, address, city, and so on. These fields are the same for every record in the database, and they are assigned names identifying the data they contain.

A database is sometimes displayed in row and column format. Each row contains one record, and each column contains one field, as illustrated in Figure 19.1. In this case, a single record can be referred to as a row, and an individual field as a column. The entire collection of records is called a table. Some databases contain more than one table, with the records in each table having a different field structure. We will deal with multiple-table databases in later chapters; for now, we’ll stick to single-table databases.

A database program is designed to let you work with the information in a database. Some capabilities are common to any database program—adding, finding, and deleting records, for example. Many other capabilities are customized to fit a specific program. In an address list database, for example, you may want the ability to print envelopes and sort the records by ZIP code. A graphics database might need the ability to input and store images from a scanner. The possibilities are endless. If you write database programs as part of your job, you never know what someone might ask you to do next. Here’s one of the advantages of Visual Basic: As a full-featured programming language, it offers the flexibility to build many capabilities right into the database program. Thanks to its database tools, most of the fundamental database tasks are simple to accomplish.


Figure 19.1  A database table has one row per record and one column per field.

Doing It From Scratch

Before Visual Basic had specialized database tools, programmers had no choice but to create all of the program’s functionality by using Visual Basic’s standard controls and the Basic language. Many database programs were created this way. This approach is still possible, but the practice of programming database applications “from scratch” is a lot less common. Still, you should know the process for a couple of reasons. For certain relatively simple database tasks, the old methods are more than adequate and provide a smaller, faster program that avoids the overhead associated with the specialized data access tools. Later in the chapter, when I cover the Jet database engine (one of Visual Basic’s database tools), I’ll explain some of the factors to consider when deciding upon the best approach. In addition, learning how to create a database program without using the specialized tools will teach you some valuable programming techniques.

The program we’ll develop in this section is a bare-bones address list. It’s not something you would actually use to keep track of names and addresses, because it’s lacking a number of features that most people would consider essential. It demonstrates the fundamentals of roll-your-own database programming, however, as well as several other Visual Basic techniques. After you complete this project, you’ll have a basic, but perfectly functional, program that you can enhance as needed.

Planning The Database

What features do we want in the address-list program? Most fundamental, of course, is keeping track of people’s address information. The first step is to define a data structure to hold the required information, as shown here:

Type Address
    FName As String * 20
    LName As String * 20
    Address As String * 40
    City As String * 15
    State As String * 2
    Zip As String * 5
End Type

The user-defined Address type gives us a place to store address information while the program is running. It also defines the structure of the random access file used for disk storage of the database information. We reviewed the relationship between a user-defined type and random access files in Chapter 13; now, we’ll see them in action.

The program should have the basic features of viewing records, adding new records, deleting records that are no longer needed, and editing existing records. We’ll also add the ability to display an alphabetical list of all records. A number of other features are possible, but let’s stop here. We want the project to be manageable. Later, I’ll describe some other features you might want to add.


Figure 19.2  The completed address database form.

Designing The Form

The address form needs one Text Box for each of the six pieces of information, or fields, that every address record will contain. Each Text Box also requires an identification label. Command Buttons arranged in two control arrays of four buttons each will permit the user to enter commands. Start a Standard EXE project and place these controls on the form. The completed form, which is saved as ADDRESS.FRM, is shown in Figure 19.2. The upper row of Command Buttons is assigned the Name property of cmdAction, with Index properties zero to three, from left to right. The lower set of Command Buttons is named cmdMove and has the same Index properties as cmdAction. The full description of this form’s objects and properties is given in Listing 19.1. Save the project under the name ADDRESS.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.