Delphi Community database tutorials, part 5

Automatically fill field ID in the Books-database

Before adding this new functionality, it's necessary that the ID of all current books is correct. Start up your project and check that every book has a unique ID of width 4, for example: 0001, 0002, 0003...

If that's not the case, there's a nifty little utility that can come to the rescue.

Changing the field definitions of a MyBase XML file

With Microsoft XML Notepad, you can easily change the structure and even the contents of an XML file. Thus it's not necessary to change the source code of Libaria and start all over with an empty database! This handy tool can be downloaded for free.

  1. Start by making a backup copy of the Books.xml that resides in the same folder as Libraria.exe
  2. Start XML Notepad, open Books.xml and play with it for a while, it's very intuitive.
    Change a few things in "Tree View", for example:
    * in METADATA FIELDS, change the WIDTH of field ID to 5 or whatever you like;
    * in ROWDATA, first ROW, change the value of field ID to 00009
  3. Save Books.xml
  4. Start Libraria.exe and see how the data has changed.
  5. Stop Libraria.exe
  6. If desired, restore Books.xml from the backup file that you made.

Calculating the next ID

Suppose that the highest ID is 0003, how do we get the next ID value of 0004?
When the user clicks the + button of the navigator, the program should proceed as follows:

* Open a copy of the Books database and sort the records by ID.
* Go to the last record and read the value of field ID into a string variable.
* Convert the string to an integer number and add 1.
* Convert the number back to a string and add leading zero's until the length is 4.
* Set field ID of cdsBooks to the calculated string.
* Show the other fields and let the user fill them in as usual.

Let's go!

  1. Drop an additional TClientDataBase on the form and name it cdsBooks2.
  2. Create an event handler for the event AfterInsert of cdsBooks and complete the code as follows:
    procedure TformBooks.cdsBooksAfterInsert(DataSet: TDataSet);
      IdLast, IdNew: string;
      N: integer;
      cdsBooks2.IndexFieldNames := 'ID';
      IdLast := cdsBooks2['ID'];
      N := StrToInt(IdLast) + 1;
      IdNew := IntToStr(N);
      while Length(IdNew) < Length(IdLast) do
        IdNew := '0' + IdNew;
      cdsBooks['ID'] := IdNew;

Adding a main menu

Later on, we'll add forms for the Authors and the Members (lenders). We should have some provision to access these forms, so let's add a menu to our form:

  1. Drop a TMainMenu component on the form (it doesn't matter where you put it, Delphi will add the menu at the top of the form).
  2. Right-click MainMenu1 and select Menu Designer...
  3. Click on the blue rectangle (the first menu item). In the Object Inspector, change its caption to &File (the & in front of the F will make the F underlined). Change its name to mitFile (mit stands for: menu item).
  4. In the Menu Designer, click on the dotted rectangle to the right (second menu item). In the Object Inspector, change its caption to &Borrow and its name to mitBorrow.
  5. Likewise, add menu items with the captions &Return, &Authors and &Members. Name them mitReturn, mitAuthors and mitMembers
    The finished menu looks like this:


Part 4 Database Tutorials - Part 4

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