Navigation in a DataSet
In a DataSet, there can be only one "active" record at a time. If you need to access data in multiple records, you must move to each of them, process the data, then move again, and so on.
To move around the dataset, you can use methods of the TDataSet class:
These dataset operations are also available in the DBNavigator component.
A DataSet has two runtime properties, EOF (End-of-file) and BOF (Beginning-of-file), useful when you want to iterate through all the records.
When EOF is True, it indicates that the cursor is at the last row in a dataset. EOF becomes True when an application:
When you want to process of all records of a dataset, you test EOF in a loop condition. Example:
CustTable.First; // go to first record, which sets EOF to False while not CustTable.Eof do begin // Process each record here // ... CustTable.Next; end;
When BOF is True, it indicates that the cursor is at the first row in a dataset. BOF becomes True when an
Bookmarks: marking and returning to records
A bookmark is a special variable that stores the current position of a record in a dataset. You use GotoBookmark to return to a bookmarked record, and FreeBookmark to delete a bookmark. Example:
Prodedure ProcessRecords; var Bookmark1: TBookmark; begin Bookmark1 := DataSet1.GetBookmark; // Process a number of records // ... DataSet1.GotoBookmark(Bookmark1); DataSet1.FreeBookmark(Bookmark1); end;
Status of a Dataset
A dataset can be in different states, indicated by the State property:
The transitions between these states are handled automatically. For example, when a program requests an Edit operation, the dataset fires the BeforeEdit event just before entering edit mode. After entering edit mode, the AfterEdit event is fired. After the user has finished editing and requests to store the data by executing the Post command, the dataset fires the BeforePost event. Finally, the AfterPost event is fired after the operation has been successfully completed.
When a filter is applied to a dataset, only those records that meet the filter's condition are available. The property
Filter describes the filter condition.
You can use SQL wildcards such as percent (%) and underscore (_) in the condition when you use the LIKE
Product LIKE 'D%'
You can also use more complex expressions in filter conditions, by combining conditions with the operators AND, OR and NOT.You can also set these properties in your source code:
DataSet1.Filter := QuotedStr(product > 'D'); Filtered := True;
The function QuotedStr() inserts a single quote at the beginning and at the end of a string. This is necessary because Filter must be a string.
Note: by using the event OnFilterRecord, you can set up combinations of complex conditions, based on multiple lines of code. OnFilterRecord is generated by the dataset for each record when Filtered is True. Example:
Procedure TForm1.DataSet1FilterRecord(DataSet: TDataSet; var Accept: Boolean); begin Accept := (Uppercase(DataSet1.FieldByName('product').AsString) > 'BOX') and (DataSet1.FieldByName('price').AsInteger < 20); end;
Database Tutorials FAQ Crash Course Delphi Tips Source Code Downloads Links