Delphi Starter database tutorials, part 2
Setting up the ClientDataset


UfoData, an application for UFO buffs

Let's build a small program for saving data of UFO sightings into a table. We have built the program using the "Delphi Community" edition. Even when you're not interested in UFOs, the techniques used will inspire you for other database applications.

Features:

  • Add / modify / delete records
  • Each record has a unique ID, containing the date plus case number for that date
  • Upon adding or modifying, the ID is validated for contents and uniqueness
  • The database is sorted on field ID
  • Memo field for a short summary of each case
  • Easily find a record containing a keyword (or part thereof) in "description" or "location"
  • Surf to the webpage of the case in question

Here's how the application will look like when it's finished:



You can download the exe-file plus the database file from: UFOdatabank

Setting up the project

  1. Open Delphi and start a new project: menu File > New > VCL Forms Application
  2. Drop a TPanel on the form. Clear its property Caption and set property Align to alTop.
  3. Drop the following components on the panel:

    TClientDataSet (Tool Palette, category Data Access)
    TDataSource (Tool Palette, category Data Access)
    TDBNavigator (Tool Palette, category Data Controls)
  4. Below the panel drop a TDBGrid (Tool Palette, category Data Controls)
  5. Let's customize some of the properties of the components. In the Object Inspector, change a few properties:

    TForm Caption UfoData
    TClientDataSet Name cdsUfo
    TDataSource DataSet cdsUfo
      Name dsUfo
    TDBGrid Align alClient
      DataSource dsUfo
    TDBNavigator DataSource dsUfo
  6. Save your project, e.g. in a folder \UfoData

Form1

Setting up the fields of the ClientDataSet

You can set up your ClientDataSet in the Object Inspector, but I prefer to do this in the source code. This gives us more control over the process. Proceed as follows:
  1. Add an OnCreate event handler to Form1: in the Object Inspector, go to the Events tab of Form1 and double click next to OnCreate.
  2. In the code editor, complete the event handler as follows:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
        cdsUfo.FieldDefs.Add('ID',       ftString, 10, False);
        cdsUfo.FieldDefs.Add('DESC',     ftString, 25, False);
        cdsUfo.FieldDefs.Add('LOCATION', ftString, 16, False);
        cdsUfo.FieldDefs.Add('COUNTRY',  ftString, 16, False);
        cdsUfo.FieldDefs.Add('CE',       ftString,  1, False);
        cdsUfo.FieldDefs.Add('SOURCE',   ftString, 25, False);
        cdsUfo.FieldDefs.Add('DURAT',    ftString, 16, False);
        cdsUfo.FieldDefs.Add('WIT',      ftString,  5, False);
        cdsUfo.FieldDefs.Add('SHAPE',    ftString, 10, False);
        cdsUfo.FieldDefs.Add('APPEAR',   ftString, 10, False);
        cdsUfo.FieldDefs.Add('SIZE',     ftString, 10, False);
        cdsUfo.FieldDefs.Add('OCC',      ftString,  3, False);
        cdsUfo.FieldDefs.Add('LINK2',    ftString, 50, False);
        cdsUfo.FieldDefs.Add('NOTES',    ftMemo,   10, False);
        cdsUfo.CreateDataSet;
    end;
    Of course, you can add and remove fields as desired, but don't remove the fields ID, DESC, LOCATION and LINK2 because these will be used later on in our project.
  3. Run the project (press key F9).

Don't forget to save your project!

Note that you can add/edit/delete records in the grid. However, the contents of cdsUfo is not saved in a file when you stop the program. That's for our next tutorial.

Deploying on another computer

An application with a TClientDataSet uses code that is contained in the file MIDAS.DLL
That's fine as long as the computer contains Delphi, but your application probably won't work when you deploy it on another PC without Delphi. Therefore, you should compile the MIDAS code into the exe-file by modifying the project's DPR as follows:

Add MIDASLIB to the "uses" clause of the DPR:

program Project1;

uses
  Vcl.Forms, 
  Unit1 in 'Unit1.pas' {Form1},
  [other units...,] 
  MIDASLIB;

Part 1Database Tutorials - Part 1     Database Tutorials - Part 3 Part 3

 


Crash Course Delphi  Database tutorials Delphi Starter  FAQ  Tips  Source Code  Downloads  Links