1. Home
  2. Computing & Technology
  3. Delphi Programming
Behind data in datasets
Page 2: From BOF to EOF and back in the Middle
 More of this Feature
• Page 1: The State of Data

Printer friendly versionPrinter friendly version
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
 Related Resources
• free DB Course.TOC
• Database tutorials
• Selecting Fields.howto

In the last chapter we used the DBNavigator component to navigate through the dataset. This component presents a visual tool for navigating through a dataset. As stated, the DBNavigator has buttons that the user can click to move among dataset's records at run-time.

   Moving on from BOF to EOF
To iterate through a recordset and to sum some values we'll need to use methods of a dataset component. Take a look at the following code:

...
 ADOTable1.DisableControls;
 try
  ADOTable1.First;
  while not ADOTable1.EOF do begin;
    Do_Summing_Calculation;
    ADOTable1.Next;
  end;
 finally
  ADOTable1.EnableControls;
 end;
...

The First method is used to set the current row in the dataset to the first one; the Next moves to the next row in a dataset. The EOF (and BOF) property indicates whether the dataset is at the last (first) row.
In most cases, the dataset is connected to one or more data-aware controls. When long iterations take place it's quite interesting to "disconnect" those data-aware controls from the dataset - to prevent data-aware controls from updating every time the active record changes. The DisableControls and EnableControls are used to disable or enable data display in controls associated with the dataset.
The error catching (try-finally) part simply ensures that all data-aware controls remain connected to the dataset if some exception occurs.
The Do_Summing_Calculation should obviously sum values represented by fields in a dataset.

Bookmarking
Prior to calling the above code the dataset was probably at some *middle* position - the user was browsing a dataset with a DBGrid. The code moves the *current* row to the end (EOF) causing the program to loose the previous position. It would be much better (and user friendly) if we could store the current position and make it the current one (again) when the iteration completes. Of course, Delphi has that option. The Bookmark property of the ADOTable (and any other TDataset decedent) can be used to store and set the current record's position. Bookmarks are used like:

var Bok : TBookmarkStr
...
Bok := ADOTable1.Bookmark;
{iteration code}
ADOTable1.Bookmark := Bok;

   The value of data
In the previous code the Do_Summing_Calculation part was left. Most likely that part should get the value of some field (column) in a dataset and sum it.
When we talk about record values in datasets we talk about values of data fields. As we have seen in the previous chapters the fields of a dataset are represented with unvisible Field components. In the examples from previous chapters we used the Object Inspector to set up a list of persistent fields for a dataset.

When data-aware controls are connected to a dataset and the user moves through a recordset the corresponding field values are presented in those controls. When we want to use the same values directly in code we need to know how to read them.
By default, when Delphi gives names to field objects the following notation is used: Table name + Field name. This means that if we have the Type field in table the filed object connected to that, hm, field will have the name: ADOTable1Type.

To access the data value from a field we can use several notations.

ADOTable1Type.Value
ADOTable1.Fields[x].Value
ADOTable1.FieldByName('Type').Value

Note: All fields of a dataset are stored in the Fields array. x represents the position of the field in the fields array.

The Value property for a field object holds the data value. Since Value is a varian type it's preferable to cast fields value to a type that we currently need. In other words an application should use the AsString property to convert a value (date, integer, currency, ...) in a field to a string when the string representation of the fields value is needed.

Now we can write the entire code to iterate through a recordset and count how many 'database' applications are in a table (of course we are talking about Applications table in our AboutDelphi.mdb Access database).

var Bok : TBookmarkStr
    ict : Integer;
begin
 ict:=0;
 Bok:=ADOTable1.Bookmark;
 try
  ADOTable1.DisableControls;
  try
   ADOTable1.First;
   while not ADOTable1.EOF do begin;
    if ADOTable1.FieldByName('Type').AsString
       = 'database' then Inc(ict);
    ADOTable1.Next;
   end;
  finally
   ADOTable1.EnableControls;
  end;
 finally
  ADOTable1.Bookmark:=Bok;
 end;
 ShowMessage('Number of database
              apps: ' + IntToStr(ict));
end;

I agree with you! We should use ADOQuery for such purposes!

That's it for the fifth chapter. Next time we'll see how to add, delete and insert recordset to a database table.

First page > The State of data > Page 1, 2

DB Course Next Chapter >>
>> Chapter 6: Data modification

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.