Delphi Starter database tutorials, part 2

Libraria, an application for Libraries

Let's build a small program with "Delphi Community" for entering data in our Books table.

  1. Open Delphi and start a new project: menu File > New > VCL Forms Application
  2. Drop the following components on the form:

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

    TForm Caption Libraria
      Name formBooks
    TClientDataSet Name cdsBooks
    TDataSource DataSet cdsBooks
      Name dsBooks
    TDBGrid DataSource gridBooks
      Name gridBooks
    TDBNavigator DataSource dsBooks
      Name navBooks
  4. Save your project, e.g. in a folder \Libraria


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 formBooks: in the Object Inspector, go to the Events tab of formBooks and double click next to OnCreate.
  2. In the code editor, complete the event handler as follows:
    procedure TformBooks.FormCreate(Sender: TObject);
      cdsBooks.FieldDefs.Add('TITLE', ftString, 30, False);
      cdsBooks.FieldDefs.Add('AUTHOR', ftString, 30, False);
      cdsBooks.FieldDefs.Add('ISBN', ftString, 13, False);
      cdsBooks.FieldDefs.Add('ID', ftString, 4, False);
  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 cdsBooks is not saved in a file when you stop the program. That's for our next tutorial.

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


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