Introduction to the Database Components in Borland C++Builder
Written by Richard Lussier. Revised by Jeff Cottingham
Data Aware Controls
The data aware controls provide
the means for an application to display data from a table to the
user. They also allow the user to change the data presented or
enter new information. These data-aware controls are counterparts
to the standard Windows controls. There are labels, edit controls,
comboboxes, radiobuttons, and list boxes. In addition, a spreadsheet-like
control called TDBGrid is provided. Below is a list of some of
the data-aware controls and descriptions of their functionality.
Controls which have many properties in common are discussed first.
Sections on TDBGrid, and DBLookupCombo follow. Lastly, the
DBNavigator components are discussed.
TDBEdit, TDBCombobox, TDBListbox,
TDBRadioGroup, TDBCheckBox, TDBImage, TDBMemo
These controls are very self
explanatory and represent data-aware equivalents of standard Windows
controls. The key properties for all these fields are:
DataField: This
is the actual field in the table or query. A drop-down list of
field names is available from which to choose. This property should
be set once the DataSource property is set.
DataSource: The
DataSource property links a data-aware control to a TDataSource
object. This establishes the tiered relationship between the DataSet
object, DataSource object, and Data Control object.
ReadOnly: As
implied, the developer can specify that a particular field is
read-only. This gives much greater control than specifying write
access for an entire table.
TDBGrid
TDBGrid offers a spreadsheet-like
look for presenting data. In many situations, the spreadsheet
metaphor is appropriate. Most users are familiar with its look
and feel, and for presenting large amounts of data in a small
space, it can't be beat. The DBGrid is also useful when designing
forms displaying a 'one-to-many' relationship. Often, the detail
records can be easily presented in a DBGrid.
A description of the key properties
follows:
DataSource:
Like all the data-aware controls the DataSource property links
the DBGrid control to the TDataSource object, which is in turn
linked to a TTable or TQuery object.
Options: The
Options property allows a developer to fine-tune the look and
operation of the DBGrid. For example, the developer can specify
if the user will see Grid Lines, Column Title, or a record indicator,
among other options.
TDBLookupList and TDBLookupCombo
These two very powerful data-aware
controls offer the C++Builder database application developer an
easy way to display "lookup" data to the user. Lookups
are usually small tables which contains all the valid values a
certain field might hold. For example, in a Customer database,
a customer's address is usually entered. The State in which the
customer can reside can be a 'lookup' to a States table. This
will ensure uniform, clean data in the database. The LookupList
and LookupCombo allow you to display the full description of the
possible lookup values, but store the lookup code. This serves
the dual role of ease of use and smaller database tables - it
is nice to show the words "California", or "Massachusetts"
to support the selection process of users but store "CA"
or "MA" in the Customers State field. The difference
can be about 15 characters per customer record for this single
field!
Along with the normal data-aware
control properties of DataField and DataSource, the properties
that allow you to perform the special lookup functions are:
LookupDisplay: This
is the field in the lookup table that contains the description
of the lookup code. For example, part number 101 may have a description
of "Size 10 Widgets". This property allows the developer
to display the more intuitive description rather than the code.
LookupField: This
property is the field in the lookup table that contains the lookup
code itself.
LookupSource: This
refers to a DataSource control that ties into the lookup table.
Remember, to get information from the lookup table, it too will
need a TTable (or TQuery) and TDataSource objects established.
The DBNavigator Component
The DBNavigator is an extraordinarily
important and powerful component. This component allows the developer
to add the now standard look and feel of 'VCR' controls to a form
to allow users to move from record to record. In addition to the
First, Last, Next, and Prior buttons, the DBNavigator also has
options for Edit, Post, Insert, Delete, Cancel and Refresh! It
provides the essential functionality every database application
needs. Each button is optional and you can mix and match at will.
For example, in a read-only form, you wouldn't want any of the
editing buttons. In a client/server application, carefully consider
allowing the user to access First or Last buttons as these might
result in long queries.
Putting it All Together
Now that your familiar with
all the components available to you for database access and manipulation,
lets take a brief look at how these components can be put together
to create a database form.
Three Steps to Design a
Form
Designing a C++Builder database
form requires just three simple steps. First, place a DataSet
object (TTable or TQuery) on the form and set its properties according
to your preferences. Second, put a TDataSource on the form and
set its DataSet property to reference the TTable or TQuery object
from the first step. Third and last, drop your data-aware controls
on the form and set their DataSource and DataField properties.
While your at it, drop on a DBNavigator and set its DataSource.
That's all it takes to build a fully functional database application
supporting data display, record navigation, and data editing!
The Database Form Wizard
C++Builder provides a very
powerful tool that can streamline even the minimal steps described
above and allow you to build fully functional database applications
with a few mouse clicks.
The Database Form Wizard is
a great utility that comes with C++Builder that can make the development
of database forms a breeze. By following the prompts you can design
simple single table forms or even one-to-many forms using the
Wizard. It even works on remote server tables! The Wizard produces
professional looking forms - the basis on which to build an application.
Customization is, of course, fully supported whether involving
adding code, error checking, or providing custom lookups. The
Wizard is extremely successful at meeting most needs for common
database application development tasks.
Conclusion
C++Builder offers the client/server
database applications developer a plethora of tools, components
and options. With this rich set of features, virtually any database
application can be written in C++Builder.
|
Connect with Us