Image of Navigational Map linked to Home / Contents / Search Navigating Your Database (for Delphi 2 beginners)

Joseph Dannaoui - GUI Computing
Image of Line Break

Jeepers, creepers! The boss has just told you that he wants you to create a database (BDE) viewer with no bells and whistles using Delphi within 30 minutes!

"No sweat", you say as your boss awaits your e-mail with the EXE. So you quickly start up the new application by adding a DataSource and Table. Then you wire them together and set the DatabaseName, TableName and DataSet properties. You set the Active property in the Table to be True and also set the AutoEdit property in the DataSource as False (after all, the boss did say viewer).

So far, so good. You right click on the Table icon, and select the Fields Editor. The Fields Editor should be blank at this stage, so you decide that you've got to add some fields. So you right click on the empty space in the Fields Editor and select Add Fields. Then you select the fields you want to add and click OK. Those fields now appear in your Fields Editor. You select all the fields you want from the Fields Editor and drag them on the form. You make the form look a bit pretty by moving all the components around.

Realising that your boss will want to view more than one record, you quickly add the DBNavigate component and wire it in to your DataSource. Stripping back the buttons you don't require you have something that looks like this.

Database Viewer
Fig 1. Sample application using the Biolife.db from the DBDEMOS alias

You test it, to make sure it works. After quickly compiling the code, you e-mail the EXE with the required DLL's to your boss. If you really want to impress the boss, create an installation disk using WISE (version 5 available through GUI Computing at an affordable price).

So there you have it. You meet the deadline quite easily, and you didn't even have to write a line of code. So you start walking around the office proud as can be with a head the size of a pumpkin and whistling the theme from Rocky, when all of a sudden, the boss calls you into his office. Now is that perfect time to ask for that promotion.

Except when you get in there, he tells you there is something wrong with the Viewer. He says that when he gets to the last record, the next button doesn't disable until he presses the next button again. He also said that the same thing happened when he was using the previous button on the first record (See Fig 2. & 3.). Pedantic or what? I told him this was a design feature (speak to Microsoft for a definition) of the Navigator component. He said that it wasn't good enough, and should be fixed ASAP.

Database Viewer
Fig 2. Position at the last record, but the Last button and Next button are still active. Technically speaking, the database is not at EOF (End-of-File).

Database Viewer
Fig 3. Position at the last record, after the boss pressed next one more time. However, the data hasn't changed. Now, the boss is at EOF.

So, you quickly realise two things:

  1. You can't tell at a glance what record you are currently at, unless you are at BOF or EOF.
  2. You are not a guru at Delphi.

So you type the following code in the FormShow event:

  table1.last;
  gsLastRecord := Table1['Common_name'];
  table1.first;
  gsFirstRecord := Table1['Common_name'];

(Make sure you declare gsLastRecord and gsFirstRecord globally as strings. Common_name is the name of a unique field used in this example. You will obviously use a unique field from your database.)

So, every time the boss clicks on the Navigator, you should check to see if you are at the first or last record like so:

  if gsFirstRecord = Table1['Common_name'] then Table1.First;
  if gsLastRecord = Table1['Common_name'] then Table1.Last;

By calling the First or Last Method, you are forcing the database to move to the BOF or EOF markers respectively.

You now have what the boss wants. You quickly re-compile the WISE script and create a new EXE. He is absolutely wrapped with the results - talk about RADical development!

If you had an extra 5 minutes to spare, I suggest that you don't use the DBNavigator component at all. Create separate buttons that navigate your database by using the First, Last, Next and Previous methods. By creating separate buttons, you avoid having to automatically post changes every time you move to a different record on a database you want to edit. It didn't matter in the above example, but just be wary of it anyway.



Written by: Joe Dannaoui
December '96

Image of Arrow linked to Previous Article
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]