Technology Programming

What Delphi Components and Controls are Needed for Data Access

What Delphi Components and Controls are Needed for Data Access

Welcome to the fourth chapter of the free Delphi Database Course. So far, this course has provided enough information to help you connect to an Access database and to display data coming from a database table. In the last chapter we were discussing some *advanced* database programming techniques - let's now go back to more *for beginners* topics.

This time, you will learn how to build a form (the real one) which can be used to browse through the records of a database table.

All the examples presented in the previous chapters have used several data-aware (ADOTable, DBGrid, ...) components without going into detail what each component is designed for, and how all the components link together.

Working together

When developing (ADO-based) Delphi database applications, the components on the Data Controls page, the dbGo page, and the Data Access page of the component palette enable our application to read from and write information to databases.
Every data-aware Delphi form, in general, will host:
  • Several data-aware controls (Data Controls tab) used to create the visual user interface (the look of the data form).
  • At least one DataSource component (Data Access tab) which represents an interface between a dataset component and data-aware controls on a form.
  • One or more dataset components providing access to the data residing in a database table or query.
  • A connection component pointing all the dataset components to a specific data store.

Data Controls; Data Source; Datasets

Delphi's data-aware controls are components that normally reside on a Standard palette tab but have been modified to display and manipulate the content of data in a dataset (table or query). The choice of controls is determined by how we want to present the information and how we want to let users browse (and manipulate - add or edit) through the records of a dataset.

DBEdit and DBMemo, for example, are used to represent an individual record from a dataset.

The DBGrid, on the other hand, is generally used when displaying the contents of an entire dataset.

Since all the data-aware controls are counterparts to the standard Windows controls - with a few extra properties, building a functional database application should be a relatively familiar task. All the data-aware components share one common property: data source.

Data Source

Simply put, the TDataSource component provides a mechanism to hook dataset components to the visual data-aware components that display the data. You generally will need one datasource component for each dataset component to present a link to one or more data-aware controls. Datasets To create an ADO based application, Delphi provides us with four dataset components: TADODataSet, TAdoTable, TADOQuery and TADOStoredProc. All the components are designed to retrieve, present and modify the data. All those components can connect directly (as like in the previous chapter's examples) to an ADO data store (such as data in an Access database) through the ConnectionString property or they can chare a single connection. When connecting through a TAdoConnection the Connection property specifies an ADO connection object to use to connect to an ADO data store.

ADO Connection (TADOConnection)

The ADOConnection component is used to establish the connection with an ADO data store. Although each ADO dataset component can directly connect to the database, you will typically want to use the ADOConnection component since the component provides methods and properties for activating the connection, accessing the ADO data store directly and for working with transactions. In order to connect to a specific database use the ConnectionString property.

Enough theory, it's time to see some action. The next step is to build a "data-aware" form. Before we move on, it'll be a good idea to open the database with Access and add some "dummy" data (3-4 records) to the database just to have some operational data.

There are two different ways of creating forms with access to a data from the database. The first way is to use the Database Form Expert. Unfortunately, the Database Form Expert works only with the BDE set of dataset components. The second way is to place and connect all the data components manually.

Defining the User Interface

We'll build our data browsing form in three steps. First step is to define the user interface for the form. Next, the data access components are added and configured. In the third and final step, the data-aware controls are added.
Before you start, close any open projects. Then use the following steps:
  1. Select File|New Application. This creates a new project containing an empty form, a unit, and a project file.

  1. Place one TPageControl on the form. TThe PageControl can be found on the Win32 tab on the component palette. Let it have the default name, PageControl1.
  2. Add two tab sheets to the PageControl (right-click on the PageControl1 and select "New Page"). Set the Caption of the first TabSheet1 to "Browse". Set the Caption of the second TabSheet2 to "Edit".
  1. Place a TDataSource (DataAccess tab), a TADOTable and a TADOConnection (ADO tab) component on the form. Leave all the components with their default names.
  2. Select the first page of the PageControl and place a TDBGrid (Data Controls tab) component on the "Browse" tab sheet.
  3. Place a TDBNavigator component (Data Controls tab). The navigator buttons are used to move through the records in the recordset.
Using the Object Inspector set the link between the components as in:
DBNavigator1.DataSource = DataSource1 DBGrid1.DataSource = DataSource1 DataSource1.DataSet = ADOTable1 ADOTable1.Connection = ADOConnection1 ADOConnection1.ConnectionString = ... ADOConnection1.LoginPrompt = False ADOTable1.Table = 'Applications' You can use the same connection string as in the second chapter, or you can build one by invoking the connection string editor.
Setting the LoginPrompt property of the ADOConnection component to False suppresses the database login from showing. Since we have not set any password for our database we don't need the login prompt.

The TDBGrid component is generally used when you want to present the entire recordset to the user (data in a table).

Even though we can use the DBGrid to let the user add, edit and delete records from the database table - a better approach is to use Field objects for all the fields in a table. Field objects are mostly used to control the display and editing of data in your applications.

By using the Fields Editor you can set the list of persistent field object for every column in a table.

The Field Editor is invoked by double clicking the DataSet component (ADOTable1). To add fields to the list of persistent fields for a dataset right-click the list and choose Add Fields.

Rather than presenting all the data in the table to the user (with the DBGrid), we might want to use field-oriented data aware components, such as edit boxes. The TDBEdit component for example, is a data-aware version of the TEdit class.

The easiest way to place a TDBEdit on the form and connect it with a field in the table is to:
  1. Invoke the Fields Editor by double clicking on the ADOTable1 component.
  2. Select the Name field, for example. Let the second tab of the Page control be the selected one.
  3. Drag the Name field on the form (precisely: TabSheet1).
When you drop the Name field on the tab sheet, Delphi places one TLabel and one TDBEdit component on it. The Caption of the Label component is the same as the DisplayLabel property of the dragged field. The DBEdit component is linked to the dataset's datasource through it's DataSource property.
If you select more than one filed from the Fields Editor and drag it on the form, Delphi will set as much Label/DBEdit components as you have dropped on the form.

Note: if the table column is a "yes/no" column, allowing true/false values, Delphi will add a TDBCheckBox control.

Ok, all set up. Everything we have to do now is to activate the connection and scroll through the records. The Active property of a dataset component (ADOTable1) indicates whether we have a live connection to the table or not. Setting Active to True or calling the Open method sets the Connected property of the ADOConnection1 to True - and displays the data in related data-aware controls.

First, Move by, Last,...

You are finally ready for some action.

The next step is to see how to "walk" through the recordset.
The DBNavigator component gives a simple and friendly tool for navigating through the recordset. In addition to it's navigational abilities, the DBNavigator provides means for manipulating the data with actions like Insert, Delete or Cancel the changes. For example, if the we click the Delete button, the correct record is deleted from the DBGrid - and from tabase table! Each button is optional and you can mix and match at will.

Using the buttons set you can skip to the last record or move to the previous one. For example, clicking on the Last button sets the current record to the last record in the recordset and disables the Last and Next buttons. Clicking the Last button is functionally the same as calling the Last method of a dataset.

Note that one of the navigational operations that the DBNavigator cannot process is moving forward or backward in a recordset by a number or records. The MoveBy method of a dataset is used to position the cursor on a record relative to the active record in the recordset.

That's it for this chapter. We are now ready to move on to topics like editing and searching the recordset, expect to learn about that in the following chapters of this course...

Leave a reply