Delphi Community Edition 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:

Preparations

  1. If you haven't done so already, create a new folder \DelphiLand.
  2. If you haven't done so already, create directory UfoData "under" \DelphiLand.
  3. Next, download ufodata.zip and unzip it to this directory.

Setting up the project

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

    ClientDataSet (Tool Palette, category Data Access)
    DataSource (Tool Palette, category Data Access)
    DBNavigator (Tool Palette, category Data Controls)
  4. Below the panel drop a DBGrid (Tool Palette, category Data Controls)
  5. Let's customize some of the properties of the components. In the Object Inspector, change these properties:

    Component Property New value
    Form Caption UfoData
    ClientDataSet Name cdsUfo
    DataSource DataSet cdsUfo
      Name dsUfo
    DBGrid Align alClient
      DataSource dsUfo
    DBNavigator DataSource dsUfo
  6. Save your project, e.g. in 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;
    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 the 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 the database 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 module into the exe-file by modifying the project's DPR file 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