KHARWASTAN SECONDARY
GRADE 11 GOOGLE CLASSROOM TERM 2 ACTIVITY
DATABASE AND DELPHI
LESSON 2: LINKING A DATABASE TO DELPHI
N.B.
In this worksheet we will be using the [Link] database created in Lesson 1 in your folder on your
desktop.
Do not move the folder from Lesson 1 that you saved on your desktop because we will be linking Delphi and
database using that location.
Your new Delphi project for this lesson will also be saved in the same folder as the [Link] database from
Lesson 1.
In this lesson you will learn how to connect to the database you created in Delphi only using specific steps.
In lesson 3 (to follow) I will teach you how to use code in Delphi to interact with the database.
Please don’t get stressed out as to the number of steps in this lesson and the new concepts. I have typed
out every single step with pictures as if I would be speaking/teaching in our classroom! Just carefully
follow each step…
_______________________________________________________________________________________________
PART 1: CREATE A DELPHI PROJECT
a. Open Delphi and create a new VCL form application (project).
Save the project as its default names viz. unit1 and project1 in the GR11 DATABASE LESSON folder
from Lesson 1 where your first database was created i.e. [Link].
Delphi project:
Save Project in the Lesson 1 folder that already has [Link]:
1|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
PART 2: ADDING A DATA MODULE FOR THE FIRST TIME
b. ADDING A DATA MODULE:
i. A data module is used to connect to the [Link] database and thereafter to the
LearnerTbl table from the database itself.
ii. We will only use a data module (this method/steps) for your PAT this year and classwork
exercises. For exam and test purposes, a direct connection will be provided.
iii. While the project is opened in Delphi:
1. Go to File…New…select Other
2. In the Dialog box that opens, make sure Delphi Files selected on left and thereafter
select Data Module. Click ok.
3. Your blank new data module will look as the picture below.
2|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
Go to File…Save As…it will open in your current project folder.
Save the data module as KSSdbModule
(external name of the data module). Go to the object inspector and change
Name to dm (internal name of data
module).
N.B.
The external name of the data module (KSSdbModule) will be used/added
to the uses list of any form that needs access to the database in your
project.
The internal name that you changed in object inspector (dm) will be used in
the actual code when we need access to any component on the data
module.
4. The following components must be added and set up from the tool palette to the
data module after saving:
COMPONENT 1
a. ADOCONNECTION – only 1 connection is needed to link to the database.
Type ADOCONNECTION in the tool palette and add to the data module.
While the ADOCONNECTION is still highlighted/selected as above, go to the
object inspector and make the following 3 changes:
- Change the Name to conn (easier than using ADOCONNECTION1).
- Deselect the LoginPrompt.
- Go to ConnectionString to setup the path…click on the ellipsis.
3|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
N.B. There will be different dialog boxes appearing/opening as you
follow steps below as per pictures.
- Click on Build.
- Select the provider we going to use i.e. Microsoft Jet 4.0 OLE DB
Provider and click next.
- Go to Select or Enter a database name and click on ellipsis.
- N.B. At this stage if you saved your database in Lesson 1 correctly as a
2002-2003 db then the database from your folder will appear and be
available for selection. If you didn’t save the [Link] in Lesson 1
correctly, you need to either go back and start from Lesson 1 again OR
re-save the KSSdb database as 2002-2003 db.
4|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
Select the database and click open.
Click on Test Connection to show that you are successful connecting to
the database.
Click ok.
Click ok.
Click ok.
N.B. This will now take you back to the data module and the
connection to the [Link] is done. Now we can add the other
necessary components for this example.
5|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
COMPONENTS 2 and 3
The next 2 components to be added from the tool palette to the data
module is needed for every table i.e. works as a pair.
In our example, we only have 1 table called LearnerTbl, therefore we need
one pair of the following 2 components.
b. ADOTABLE – add an ADOTABLE from the tool palette to the data module.
While the ADOTABLE is still highlighted/selected, make the following
changes in the object inspector:
- Go to Connection and click the arrow for a drop down list of available
ADOCONNECTIONS and select conn (which is the name of the
ADOCONNECTION already set above and the only connection available
in our example).
-
6|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
- Thereafter change the Name in the object inspector of the ADOTABLE
to LearnerTbl (We will keep the same as the database table LearnerTbl
so no confusion).
- Now go to TableName in the object inspector and click on the arrow for
a drop down list of all available tables. In our example, we have only 1
table in the database called LearnerTbl. Select the table.
- Lastly, select the Active property of the table (true) in the object
inspector.
7|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
c. DATASOURCE – this is the source component required for the ADOTABLE
set above. Add a DATASOURCE from the tool palette to the data module.
Whilst the DATASOURCE component is still highlighted/selected, make the
following changes in the object inspector:
- Go to DATASET and click on the arrow for the list of all available
ADOTABLES.
In our example, we have only 1 ADOTABLE called LearnerTbl setup
above. Select this table as the Dataset.
- Change the Name property to dsLearnerTbl (user friendly name) to
indicate which table it is linked to.
This is important especially if you have many tables in your database
and not just 1 table as in our example.
The data module is now linked and setup for our Delphi project interaction with
database…
8|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
PART 3: USING THE DATA MODULE LINK NOW TO VIEW THE TABLE IN YOUR DELPHI
FORM
c. VIEWING THE DATABASE TABLE IN THE DELPHI FORM IN RUNTIME WITHOUT CODE:
i. Go back to unit1 (i.e. the form) of your project.
ii. Go to the uses list of the form and add the external name of the Data Module
(KSSdbModule).
iii. From the tool palette add a DBgrid to the form. A DBgrid is a component in Delphi that will
allow us to view Tables from a database.
Whilst the dbgrid is still highlighted/selected, go to the object inspector and in the
DATASOURCE property, click the arrow for available datasources.
In our example we have only 1 Datasource called dsLearnerTbl which is the datasource
component linked to the table.
9|P ag e “ A I M F O R E X C E L L E N C E ” … E C M
Select this datasource.
Once you complete this step, automatically the records in the database table LearnerTbl
will appear in the dbgrid without having to open up the database in Access or running the
project because it is now linked!!!
DELPHI SAMPLE RUN:
_______________________________________________________________________________________________
N.B.
- Lesson 2 is now completed and if you followed each step carefully, you would now be able to practice
connecting to other databases in Delphi using a datamodule in new projects.
- In Lesson 3, I will teach you how to use code to interact with the database in Delphi using your successful
Lesson 1 and Lesson 2 project. Therefore, do NOT delete this folder.
10 | P a g e “ A I M F O R E X C E L L E N C E ” … E C M