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.
- Start by making a backup copy of the Books.xml that resides in the same folder as Libraria.exe
- 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
- Save Books.xml
- Start Libraria.exe and see how the data has changed.
- Stop Libraria.exe
- 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.
- Drop an additional TClientDataBase on the form and name it cdsBooks2.
- Create an event handler for the event AfterInsert of cdsBooks and complete the code as follows:
procedure TformBooks.cdsBooksAfterInsert(DataSet: TDataSet);
IdLast, IdNew: string;
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:
- 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).
- Right-click MainMenu1 and select Menu Designer...
- 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).
- 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.
- Likewise, add menu items with the captions &Return, &Authors and &Members. Name them
mitReturn, mitAuthors and mitMembers
The finished menu looks like this:
Database Tutorials - Part 4