Free Pascal
LAZARUS

Database Tutorial - part 2

Structure of the tables

Members.dbf contains the details of each club member. Its records (table rows) have the following fields (table columns):

Lastname
Firstname
ID

Guitars.dbf has the following fields:

Brand
Type
Kind
Year
MemberID

Each record of Members.dbf is related to one or more records of Guitars.dbf, by means of the fields ID and MemberID, and a related index file (specifying the order of the records). The relation works like this:

Table 1     -> MasterField  ->  Table 2     ->  Index file

Members.dbf -> ID           ->  Guitars.dbf ->  Guitars.mdx (index on MemberID)


Project GuitarClub

Below is a snapshot of the application at runtime, with a relation set up between the two tables.
As you can see, President John is in the database with 4 guitars.

GuitarClub1

For this project, create a folder FPlaz\GuitarClub1, download GuitarClub1.zip and unzip to this folder.

  1. Start Lazarus and open project GuitarClub1.
  2. Compile and run the application (press F9).
    Note that at this stage, the lower grid shows all of the guitars.
  3. Stop the program.
  4. Now comes the magic: we'll set a relation between the two tables.

    Select component dbfGuitars.
    Set its property MasterSource to dsMembers.
    Set MasterFields to ClubID.
    Press F9 and navigate through the members (top grid). If all went well, now the bottom grid only shows the corresponding records.