Beginner's Guide To Delphi Database Programming
Beginner's Guide To Delphi Database Programming
Programming
CHAPTER 9:
Searching for data
Walking through various methods of data seeking and locating while developing ADO
based Delphi database applications.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 10:
ADO Cursors
How ADO uses cursors as a storage and access mechanism, and what you should do to
choose the best cursor for your Delphi ADO application.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 11:
From Paradox to Access with ADO and Delphi
Focusing on the TADOCommand components and using the SQL DDL language to help
porting your BDE/Paradox data to ADO/Access.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 12:
Master detail relationships
How to use master-detail database relationships, with ADO and Delphi, to deal effectively
with the problem of joining two database tables to present information.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 13:
New...Access Database from Delphi
How to create an MS Access database without the MS Access. How to create a table, add
an index to an existing table, how to join two tables and set up referential integrity. No MS
Access, only Pure Delphi code.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 14:
Charting with Databases
Introducing the TDBChart component by integrating some basic charts into a Delphi ADO
based application to quickly make graphs directly for the data in recordsets without
requiring any code.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 15:
Lookup!
See how to use lookup fields in Delphi to achieve faster, better and safer data editing.
Also, find how to create a new field for a dataset and discuss some of the key lookup
properties. Plus, take a look at how to place a combo box inside a DBGrid.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 16:
Compacting an Access database with ADO and Delphi
While working in a database application you change data in a database, the database
becomes fragmented and uses more disk space than is necessary. Periodically, you can
compact your database to defragment the database file. This article shows how to use JRO
from Delphi in order to compact an Access database from code.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 17:
Database reports with Delphi and ADO
How to use QuickReport set of components to create database reports with Delphi. See
how to produce database output with text, images, charts and memos - quickly and easily.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 18:
Data Modules
How to use the TDataModule class - central location for collecting and encapsulating
DataSet and DataSource objects, their properties, events and code.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 19:
Handling database errors
Introducing error handling techniques in Delphi ADO database application development.
Find out about global exception handling and dataset specific error events. See how to
write an error logging procedure.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 20:
From ADO Query to HTML
How to export your data to HTML using Delphi and ADO. This is the first step in publishing
your database on the Internet - see how to create a static HTML page from an ADO query.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 21:
Using ADO in Delphi 3 and 4 (before AdoExpress / dbGO)
How to import Active Data Objects (ADO) type-libraries in Delphi 3 and 4 to create a
wrapper around components that encapsulate the functionality of ADO objects, properties
and methods.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 22:
Transactions in Delphi ADO database development
How many times have you wanted to insert, delete or update a lot of records collectively
wanting that either all of them get executed or if there is an error then none is executed at
all? This article will show you how to post or undo a series of changes made to the source
data in a single call.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 23:
Deploying Delphi ADO database applications
It is time to make your Delphi ADO database application available for others to run. Once
you have created a Delphi ADO based solution, the final step is to successfully deploy it to
the user's computer.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 24:
Delphi ADO/DB programming: Real Problems - Real Solutions
In real world situations, really doing database programming is much more complex than
writing about. This chapter points to some great Delphi Programming Forum threads
initiated by this Course - discussions that solve problems on the field.
CHAPTER 25:
TOP ADO programming TIPS
Collection of frequently asked questions, answers, tips and tricks about ADO
programming.
Discuss about questions, comments, problems and solutions related to this chapter!
CHAPTER 26:
Quiz: Delphi ADO Programming
What would it look like: Who Wants to be a Delphi ADO Database Programming Guru - the
trivia game.
Discuss about questions, comments, problems and solutions related to this chapter!
Appendices
What follows is a list of articles (quick tips) explaining how to use various Delphi DB related
components more efficiently at design and run time.
APPENDIX 0
DB Aware Grid Components
The list of the best Data Aware Grid components available for Delphi. The TDBGrid
component enhanced to maximum.
APPENDIX A
DBGrid to the MAX
Contrary to most other Delphi data-aware controls, the DBGrid component has many nice
features and is more powerful than you would have thought.
The "standard" DBGrid does its job of displaying and manipulating records from a dataset in
a tabular grid. However, there are many ways (and reasons) why you should consider
customizing the output of a DBGrid:
Adjusting DBGrid column widths automatically, DBGrid with MultiSelect Coloring DBGrid,
Selecting and highlighting a row in a DBGrid - "OnMouseOverRow", Sorting records in DBGrid
by Clicking on Column Title, Adding components to a DBGrid - theory, CheckBox inside a
DBGrid, DateTimePicker (calendar) inside a DBGrid, Drop down pick list inside a DBGrid - part
1, Drop down list (DBLookupComboBox) inside a DBGrid - part 2, Accessing protected
members of a DBGrid, Exposing the OnClick event for a DBGrid, What is being typed into the
DBGrid?, How to Display Only Selected Fields in a DbGrid, How to get DBGrid Cell
coordinates, How to create a simple database display form, Get the line number of a
selected row in a DBGrid, Prevent CTRL+DELETE in DBGrid, How to correctly use the mouse
wheel in DBGrid, Making the Enter key work like a Tab key in a DBGrid ...
APPENDIX B
Customizing the DBNavigator
Enhancing the TDBNavigator component with modified graphics (glyphs), custom button
captions, and more. Exposing the OnMouseUp/Down event for every button.
Discuss about questions, comments, problems and solutions related to this quick tip!
APPENDIX C
Accessing and managing MS Excel sheets with Delphi
How to retrieve, display and edit Microsoft Excel spreadsheets with ADO (dbGO) and
Delphi. This step-by-step article describes how to connect to Excel, retrieve sheet data,
and enable editing of data (using the DBGrid). You'll also find a list of most common errors
(and how to deal with them) that might pop up in the process.
Discuss about questions, comments, problems and solutions related to this quick tip!
APPENDIX D
Enumerating available SQL Servers. Retrieving databases on a SQL Server
Here's how to create your own connection dialog for a SQL Server database. Full Delphi
source code for getting the list of available MS SQL Servers (on a network) and listing
database names on a Server.
Discuss about questions, comments, problems and solutions related to this quick tip!
Don't runaway
Database programming, of course, is not trivial. In this course we will try to bring closer some of the
techniques, problems and solutions to database programming with Delphi along with all the secrets it hides
from us.
Before we move on to using various data components/tools in Delphi we should first see some of the concepts
of database design and try to build a simple database.
Page 2: Building a new Database.
Before we start interacting with databases using Delphi, it is a good idea to get a
feel what modern databases are about.
When you think of a word database you should generally think of any type of data stored inside a computer even a SomeFile.pas file (code to some Delphi unit) is some kind of database. Another type of database is a
Word document or a simple .ini file. To access an .ini file we generally use routines and techniques for typed or
untyped files.
Building a modern database application requires us to think of data in a relational way. The basic idea behind
the relational model is that a database consists of a series of tables (or relations) that can be manipulated using
operations that return tables or so-called views. Simply put, a database is best described as a collection of
related data. A database may include many different tables. Tables are like grids where columns are called
fields and rows are called ... rows.
To fully address the concepts of database design and relational model we would need an extra online course.
For a great overview check out the Fundamentals of Relational Database Design.
New...Database
Since this course will primarily focus on ADO/Access Delphi approach to database programming we will now
see how to create a new .mdb database in MS Access.
If you have never built a database with MS Access, go see MS Access tutorials for a great info.
I hope you know that on this site there is a Members Area where Delphi developers can upload their free code
applications and components. Each member has it's name, an email address and a possibly a web page. If we
would like to keep track of every application posted to this community we could assemble a database of three
tables: Applications (general information about an application), Authors (who made the application) and Types
(what kind of app is it). Let's see how to do just that:
Start Access and create a blank database named aboutdelphi.mdb. Create three tables in Design view:
Applications, Authors and Types.
Let's see the structure of those tables:
The Applications table contains fields that match the application description requirements: Name, Description,
Author, Type, Size, Cost, DateUpl and Picture. Name, Description, Author and Type fields contain Text data, 50
characters by default. The Size filed is of a Number (Single) type - used to store the size of a file in Kb. The Cost
field is a Currency field - if the app is shareware or commercial. The DateUpl field is a date/time value. The
Picture is of a OLE Object type and will hold an (optional) picture for an application. Let the filed Name be the
primary key.
The Authors table contains fields that match the application author requirements: AuthorName, Email and
Web. All the fields contain character data (50 chars by default). Let the filed AuthorName be the primary key.
The Types table contains only one field: TypeName which is the primary key for this table. This table will be
used to store the type of application (graphical, multimedia, database, ...)
We now only have to set up a relation in the relationships window and the database is ready.
When we write a database application in Delphi, we need to use some database engine to access a data in a
database. The database engine permits you to concentrate on what data you want to access, instead of how to
access it. From the first version, Delphi provides database developers with the BDE (Borland Database Engine).
Beside the BDE, Delphi from the fifth version supports Microsoft ADO database interface.
This course will primarily focus on MS Access local database producing the single-tiered application.
for accessing all kinds of data sources. These objects provide the functionality to connect to data sources, query
and update record sets, and report errors. Delphi, through several VCL components provides wrapper
components to access those objects. Let's see what are some of the Objects ADO works with:
The Connection object represents a connection to the data source with the connection strings. In BDE/Delphi a
Connection object is a combination of the Database and Session components.
The Command object enables us to operate on a data source. Ir represents a command (also known as a query
or statement) that can be processed to add, delete, query or update the data in a database.
The Recordset object is a result of a Query command. You can think of a Recordset as a Delphi Table or Query
component. Each row that the Recordset returns consists of multiple Field objects.
Several other objects like: the Field object, the Parameter Object and the Error object also exist in ADO model will get back to them in the following chapters of this course.
Page 4: Connecting to a database with ADOExpress.
Before going on to the brief explanation of each component in AdoExpress collection, let's first see how to
connect to an Access database. Of course, we will be connecting to our AboutDelphi.mdb sample database.
Delphi (5) ADO support is concentrated in the ADOExpress components on the ADO tab of the component
palette. Several other database enabled components will be used through this course. For the moment we will
focus on the minimal set of components needed to access an Access database with ADO.
Start Delphi, this will open a new application with one blank form.
In order to be able to access data in an Access database with ADO and Delphi, you must add at least three data
aware components to our project. First, the DBGrid on the DataControls component page - used to browse
through the records retrieved from a table or by a query. Second, the DataSource (DataAccess Page) used to
provide a link between a dataset and DBGrid component on a form that enable display, navigation, and editing
of the data underlying the dataset. And finally the ADOTable (ADO page) that represents a table retrieved from
an ADO data store. Drop all of them on a form. Let the names be the default one. The form should look
something like:
If you run the application now, nothing is displayed in a Grid - of course, we did nothing to
really connect to a database. Note just one more thing: only the Grid is displayed, the rest
two component are controls - unvisible to the user.
Link between components
In order to display some data from a database we have to link all three components
together. Using the Object Inspector, set the following:
DBGrid1.DataSource = DataSource1
DataSource1.DataSet = ADOTable1
We have now reached the hard part, to really get the data from our database we have to
build a ConnectionString. This string indicates where the database is physically stored and
how we are accessing it. When you double click the ellipsis button at the ConnectionString
property of the AdoTable component you get the next dialog box:
When building a connection string we have two choices: use the Data Link File (.UDL) or
build a connection string by hand. Let's build it. Press the Build button - this pops up the
Data Link Properties dialog. This dialog has 4 pages. The Provider tab allows you to specify
the provider - select the Microsoft Jet 4.0 OLE DB Provider. The Next button leads us to the
second page: Connection. Select the ellipsis button to browse for our database
(AboutDelphi.mdb). Press the Test Connection button to see if the connection is successful it should be. Leave all the other pages as they are. Finally, click on OK to close the Data Link
Properties dialog, again OK to close the ConnectionString dialog - the connection string is
stored in the ConnectionString property of the ADTTable component. The connection string
should look something like:
Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\!gajba\About\aboutdelphi.mdb;
Persist Security Info=False
To finish, we have to set the table name that is to be accessed by the ADOTable component again use the Object Inspector.
ADOTable1.TableName = Applications
If you want to see the data at design time use the ADOTable Active property - set it to True.
Ha! If you have done all the steps you now see the only one record (row) in the Applications
table. When you start the application you can even change the data in the database. Of
course, you cannot do much more - this is the simplest ADO example I could think of.
This concludes this chapter. In the next chapter we will address all the ADO component
provided with Delphi and how they communicate with the rest data-aware components to
crate a powerfull Delphi database application.
Current Results
In this third chapter of the Delphi database course, we'll see how to pull out and display the graphical data (images)
inside an Access database with ADO. Don't be worried with the fact that working with images inside an Access
database requires more database programming skills than this course has provided so far. Let's pretend that we
know more to get more.
If you have followed this course from the beginning (specially the second chapter), you know how to connect to a
database and display the Applications (from our working aboutdelphi.mdb database) table in a DBGrid. Remember,
we used 3 data components: DBGrid, ADOTable and DataSource to get and display the data from the Applications
table.
Back in the first chapter when we created our database, the last filed in the Applications table was left blank (after
filling our database with some dummy data). The last field has the name Picture and is of the OLE object type.
If you scroll right to the last column of the DBGrid you'll see something like:
When using MS Access, we can store images (and other large data objects such as sound or
video) in a field that has the OLE object type. This type of data is referred to as a Binary Large
Object Bitmap (BLOB).
Naturally when working with images, several types of picture formats are available. The most
commonly used include JPEG, GIF and BMP. JPEG format has proven to be widely accepted by
Web masters because of the small amount of storage required (in other words JPEGs are smaller
than BMPs in bytes).
Delphi, of course, has means of handling BMP, GIF and JPEG graphic formats. The rest of this
article will deal with JPEG file formats.
Storing pictures in Access
Before going on to discussion about displaying the image inside a table within a Delphi form, we
need to add some graphical data to our database.
Start Access and open the aboutdelphi.mdb database. Open the Applications table (it should
have one row of data) and select the Picture field.
2. Click on the Browse button, this pops up the Browse open dialog. Note: you probably have
some .jpg files on your computer, so you could use those, or if you have Win 98 and newer, MS
Paint will save pictures in this format, as will many other programs. Navigate to a directory
where your pictures are stored and select one.
Note: the text in the Picture field holds the name of an executable used to work with JPEG files
on your computer. Of course you don't see the picture in a table grid. To actually see the
graphics double click that field. This will load the image within the JPG type associated
application.
Now, when we have a picture inside a database, let's see how to display it inside a Delphi form.
We already have a Delphi form with data components on it from the second chapter of this
course.
All clear, put a DBImage on form and leave the DBImage1 name. To actually link a DBImage with a BLOB field in
a Table we simply need to do the following assignment (using the Object Inspector):
DBImage1.DataSource = DataSource1
DBImage1.Field = Picture
This should do the trick of displaying the JPEG image stored in the Picture field of the Applications table.
To see whether this assignment will work the only thing we have to do is to set the Active property of the
ADOTable1 component to True. We can do this at design time with the Object Inspector. Once you set it you'll
get the following:
Now what? Why does it say "Bitmap image is not valid." We have a JPEG picture not the
BMP - is this the problem? Let's go back to the Help.
After a few clicks through the Help the conclusion is: to get the JPG inside a database we
need to use the TJpegImage object. To display the picture we need the simple, non-data
aware, version of the Image component. Even more we'll need to use streams to load a
picture from a BLOB object. The Help states that we should use TADOBlobStream to access
or modify the value of a BLOB or memo field in an ADO dataset.
uses jpeg;
...
procedure TForm1.btnShowImageClick(Sender: TObject);
var
bS : TADOBlobStream;
Pic : TJpegImage;
begin
bS := TADOBlobStream.Create
(AdoTable1Picture, bmRead);
try
Pic:=TJpegImage.Create;
try
Pic.LoadFromStream(bS);
ADOImage.Picture.Graphic:=Pic;
finally
Pic.Free;
end;
finally
bS.Free
end;
end;
Ok, let's run the project now. Of course set the ADOTable1.Active property to True. The form
is displayed and after clicking on a button this is what we got:
Hm, what now? The code in the procedure is 100% correct but the image doesn't get
displayed! Remember the "Never give up, never surrender"? Let's go down to byte level to
see what's happening!
Would you believe this! MS Access stores the path of a linked OLE object as part of the
object's definition in the OLE object field. Because the definition of OLE object storage is not
documented (!? this is straight from MS) there is no way to know what gets written before
the actual image data.
Think about this twice. First: we'll need to seek to the 'FFD8' and read the image from there.
Second, the 'FFD8' might not always be at the same position in the file. Conclusion: we need
a function that returns the position of the SOI marker for the JPG file stored as OLE object in
an Access database.
The correct way - take four!
Provided with the Blob type field our function should return the position of the 'FFD8' string
inside the ADOBlobStream. The ReadBuffer reads byte by byte from the stream. Each call to
ReadBuffer moves the position of the stream by one. When two bytes together (as hex
values) result in SOI marker the function returns the stream position. This is the function:
function JpegStartsInBlob
(PicField:TBlobField):integer;
var
bS : TADOBlobStream;
buffer : Word;
hx : string;
begin
Result := -1;
bS := TADOBlobStream.Create(PicField, bmRead);
try
while (Result = -1) and
(bS.Position + 1 do
begin
bS.ReadBuffer(buffer, 1);
hx:=IntToHex(buffer, 2);
if hx = 'FF' then begin
bS.ReadBuffer(buffer, 1);
hx:=IntToHex(buffer, 2);
if hx = 'D8' then Result := bS.Position - 2
else if hx = 'FF' then
bS.Position := bS.Position-1;
end; //if
end; //while
finally
bS.Free
end; //try
end;
Once we have the position of the SOI marker we use it to seek to it in the ADOBlob stream.
uses jpeg;
...
procedure TForm1.btnShowImageClick(Sender: TObject);
var
bS : TADOBlobStream;
Pic : TJpegImage;
begin
bS := TADOBlobStream.Create
(AdoTable1Picture, bmRead);
try
bS.Seek(JpegStartsInBlob(AdoTable1Picture),
soFromBeginning);
Pic:=TJpegImage.Create;
try
Pic.LoadFromStream(bS);
ADOImage.Picture.Graphic:=Pic;
finally
Pic.Free;
end;
finally
bS.Free
end;
end;
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.
Ads
Delphi Image Components
www.imageen.com
Powerful Native Image Display and Editing Library for Delphi
SDL Delphi Components
www.lohninger.com
HiTech components: 3D-plots, math, statistics, FFT, regression, meters
Start Download
www.allin1convert.com
Convert 50+ Files On-line Free All in One Converter!
Component Delphi
Delphi Controls
Data
Database Applications
Delphi Editor
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
2 of 6
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.
Ads
Delphi Image Components
www.imageen.com
Powerful Native Image Display and Editing Library for Delphi
Component Delphi
Data
Delphi Controls
Data First Applications
Database Applications
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.
3 of 6
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.
Ads
Delphi Image Components
www.imageen.com
Powerful Native Image Display and Editing Library for Delphi
Component Delphi
Delphi Controls
Data
Database Applications
Delphi Editor
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.
4 of 6
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.
Ads
Delphi Image Components
www.imageen.com
Powerful Native Image Display and Editing Library for Delphi
Component Delphi
Delphi Controls
User Interface
Data
Database Applications
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.
5 of 6
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.
Ads
Delphi Image Components
www.imageen.com
Powerful Native Image Display and Editing Library for Delphi
Component Delphi
Delphi Controls
Delphi Editor
Data
Database Applications
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.
6 of 6
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.
Component Delphi
Data
Delphi Controls
Data First Applications
Database Applications
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...