You are on page 1of 7

UNIT 5

DATABASE
RECORD SET
Recordset object is used to hold a set of records from a database table.
The Data Control’s Properties

The most important properties of the Data control are:

• DatabaseName, which specifies the database to be used


• RecordSource, which speci.fies the part of the database seen by the control

At any given the Data control is positioned at a single row (record) of its RecordSet. T
he user can move to any other record with the help of the navigation buttons on the Data control.
The most important properties of a data-bound control are:
• DataSource, which is the name of a Data control through which the controls (e.g., TextBox,
CheckBox) are bound to a Data control; in other words, it’s the name of a Data control through
which the data-bound control “sees” the database.
• DataField, which is the name of a field in the RecordSet that the control displays and updates.
DATA CONTROL METHODS
1. MoveFirst repositions the control to the first record.
2. MoveLast repositions the control to the last record.
3. MovePrevious repositions the control to the previous record.
4. MoveNext repositions the control to the next record.
UNDERSTANDING RELATIONAL CONCEPTS
The databases described and used so far are relational because they are based on relations among
the tables. The foundation of a relational database system is to break the data into multiple tables
that are related by common information (keys).
Suppose you are designing a database for storing invoices. This database should have a table
with customer names, product names, and prices and, of course, a table for the invoices. Each
invoice that is issued to a customer contains a number of products. Instead of storing product
names and customer names in the invoice, you store numbers that uniquely identify the
customers and the products. These numbers are the keys that connect rows in one table to rows in
another table. The keys establish relationships among the tables and make it possible to break the
information into
separate tables and avoid duplicating information.
VISUAL DATA MANAGER
The Visual Data Manager is a Visual Basic tool for designing databases. Although it’s rather
crude, you can use the Visual Data Manager to create and modify tables, to implement security,
and to experiment with SQL.
To create new tables, right-click in the database window to open the shortcut menu and select
New Table. In the Table Structure dialog box that appears, create your fields.
Each time you add a new field to the table by clicking the Add Field button, the Add Field dialog
box opens, as shown in Figure 17.8.

FIGURE 17.8
The Add Field dialog box has the options-shown in Table 17.5.Some of the options are disabled
in the figure and may not be readable, Follow the steps outlined to open the Add Field dialog box
and see all the options on it.
One of the two sample databases that comes with Visual Basic is called BIBLIO. The
BIBLIO database has a simple structure, almost trivial, but it demonstrates many of the topics we
have covered 80 far. You can open the BIBLIO database With the Visual Data Manager and
explore its structure.
The BIBLIO database contains book’ titles, authors, and publishers, and it’s made up of four.
tables, as shown in Figure 17.11. Instead of showing the names of the fields of each table, this
figure shows some of the data they contain, and it shows only the fields needed to demonstrates
the relationship among the tables, The field names. are displayed, as column headings.

FIGURE 17.11
The first rule in database design is to avoid data duplication. Storing author names along with the
titles would violate this rule because an author may have written more than one book. In this
case, you would have to repeat the same author name in more than one row of the Titles table.
Author names are stored in a separate table, the  authors table. The same is true for publishers.
Since each publisher
appears in many books, you shouldn’t store information about publishers in the Titles table. Why
repeat the publisher’s address with each title?
So far, we have established the reasons for the presence of three tables in the database. But the
BIBLIO database has a fourth table. Each book can have more than one author, and an author’s
name may appear with more than a single title. Think about this for a moment. Storing multiple
author names, even author IDs, for each title would require a field for each author. This field
would be analogous to the PubID field, but because a book might have multiple authors, you
would have to provide a number of fields-for storing Author IDs (AuthorID1, AuthorID2, and so
on). It is clear that authors can’t be handled like publishers
The Title Author table sits between the Titles and Authors tables and connects them with a pair
of fields, which are the Title’s ISBN and the Author’s ID. When you want.to see a specific title’s
author, you will do the following:
1. Locate the desired title in the Titles table.
2. Read the Title’s ISBN and use it as a key to locate the matching row(s) in the Title
Author table.
3. For each’of the matching rows in the Title Author table, read the author’s ID and use it as
a key to locate the author’s name in the Authors table .
If you are not familiar with database programming, this procedure may sound complicated.but it
isn’t (perhaps you should just get used to it). Later in this chapter, you’ll see two ways to search
for specific records with keys. The technique just described won’t work efficiently unless there’s
a quick way to locate a record based on the value of a specific field. And this tan be
accomplished with the proper indexing of the tables.
As you can see, the indices are an essential part of database design. The Title Author table, for
example, must be indexed on the ISBN field. If you want to be able to search in the
opposite.direction (given an author’s name; locate the books on which this name appears), you
‘should also index the Title Author table on the AulD. field. Obviously, the Authors table must
be indexed on the AuID field, since the information in the Title Author table is the author/s ID,
not their name. The Publishers table must be indexed on the PubID field so that you can go
quickly from a title to its publisher.

Data Validation with VB

Data validation is the process of ensuring, at least as far as is possible, that the data given to a
program by a user or from a file (essentially, the program's input) is of the correct type, and in
the correct format. Although the programmer will obviously take every precaution to ensure the
correct operation of the program, and will attempt to eliminate bugs that could cause a problem
through a rigorous process of testing, they have no real control over mistakes made by the user
during data entry. Nor can they guarantee that any data files used by the program will be free of
errors and in the correct format.
There are however measures that can be taken to restrict the program's input to valid data. Such
measures involve the application of validation rules to any data being input to the program. Input
not meeting the program's requirements (i.e. input that does not obey the validation rules) can be
dealt with in a pre-defined way, and will not cause the program to crash or produce spurious
output. 

ACCESSING FIELD IN RECORDSET

The field values can be accessed via the Fields object of the RecordSet. The following
expression:
recordset.Fields
represents the fields (columns) of the RecordSet. The recordset variable represents a RecordSet
(it could be a Data control’s RecordSet property, Data1.RecordSet, or a RecordSet variable).
You access individual fields through the field’s name or through the field’s ordinal position in
the table. If the Data1 Data control is connected to the Titles table of the BIBLIO database, you
can access the Title field of the current record with either of the following statements:
bookTitle = Data1.Recordset.Fields(0)
bookTitle = Data1.Recordset.Fields.(‘Title’)
Two more properties of interest are the RecordCount property of the RecordSet object (which
returns the number of records in the RecordSet) and the Count property of the Fields object
(which returns the number of fields in the RecordSet’s row). These two properties are actually
the dimensions of the RecordSet. The number of rows in the RecordSet of the Data1 Data control
is:
Data1. RecordSet.RecordCount
and the number of columns in the same RecordSet is:
Data1. RecordSet. Fields.count
SQL is a standard language for accessing and manipulating databases.

What is SQL?

 SQL stands for Structured Query Language


 SQL lets you access and manipulate databases
 SQL became a standard of the American National Standards Institute (ANSI) in 1986,
and of the International Organization for Standardization (ISO) in 1987

What Can SQL do?

 SQL can execute queries against a database


 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views

ADVANCED DATA BOUND CONTROL

we are going to look at three data-bound controls that are quite different from those we’ve used
so far:
1. The data-bound List control
2. The data-bound ComboBox control
3. The data-bound Grid control
. .
Unlike the other data-bound controls, these controls can display fields from multiple rows. The
data-bound List control is similar to the regular ListBox control and can be populated with an
entire column of a RecordSet. The data-bound List control has all the functionality of the regular
ListBox control, and in addition, allows the use’lo enter new values (records). The data-bound
Grid control is similar to the Grid control and can display an entire RecordSet. Each row of the
grid holds a row (record) of the RecordSet, and the RecordSet’s columns correspond to the
columns (fields) of the RecordSet,
These two data-bound controls aren’t installed by default. To use them in your projects, you
must first add them to the Toolbox. To do so, follow these steps:
1. Right-click the Toolbox and select Components to open the Components dialog .box .
2. Check the Microsoft Data Bound Grid Control and the Microsoft Data Bound List
Control boxes.
3. Click the Close button to close the Components dialog box.
Using the Data-Bound List Control
The data-bound List control can be bound to a specific column of the RecordSet and is
commonly used as a lookup table. Figure 17.21 shows how the data-bound List control can be
used as a lookup table to simplify navigation in a RecordSet.

The data-bound List control is different from the data-bound controls we have looked at thus far:
it can connect to two data controls. It has the standard Data- Source/DataField properties, which
are used just like any other data-bound control, and it has RowSource/ListField properties that
determine how the control is populated:
1. RowSource specifies the source (RecordSet or Data control) for populating the list.
2. ListField specifies the field that will be used to fill the list.
VB6 at Work: The DBList Project
The DBList application demonstrates the use of the data-bound List control as a navigation tool.
The Form you see in Figure 17.21 contains a number of fields from the Products table in the
NWIND database. Design the Form shown in Figure 17.21 by binding the various textboxes to
their corresponding fields through a Data control.. as if you were going to navigate through the
list of products with the buttons on·the Data control.
The problem with the Data control is that you can’t really use it to navigate through RecordSet,
even if it’s indexed, because you only see one record at a time. If you could place the key values
in a ListBox control and use it as a navigation tool, you would have a much more convenient
user interface.
Place a data-bound List control on the Form, and set its RowSource property to the name of the
Datal Data control and its ListField property to the name of the field you want to display in the
list. For the DBList application, this is the Product- Name field. The DataSource and DataField
properties of the data-bound List control
are empty.
If you run the application now, nothing will happen, because you haven’t specified how the List
control should react to the Click event. Basically, you want to reposition the Data control to the
row of the RecordSet that has the  matching Product Name field. To do this, insert the following
code in the List control’s Click event:

The Bookmark property identifies a row in the RecordSet. By setting this property to a value,
you are in effect forcing the Data control to be repositioned to the specified row. The control’s
SelectedItem property is not .the text displayed in the edit box of the control, but the bookmark
of the record to which the selected field belongs. This code repositions the Data control in the
RccordSet and updates the
data-bound labels on the Form. For a single line, the DBList application sure does a lot.

You might also like