Using BufDataSets in Lazarus

We have experimented with the TBufDataset component in Lazarus and found it (like the less-functional TMemdataset) trickier to use and less reliable than the TClientDataset component in Lazarus. This Free Pascal wiki page was helpful, but more complicated than necessary to enable a school student to get started.

If you follow these instructions you should be able to:
  • populate a dataset with types such as string, integer and real using Pascal code;
  • save the data to a local file;
  • load the data from the local file to the dataset;
  • use buttons on the DBNavigator component to step through the records;
  • edit data directly in the DBGrid and insert and populate new records in the grid;
  • make use of the inbuilt validation whereby, for example, digits only are allowed in an integer field;
  • sort the data by any of the fields;
  • filter the records so that only those matching your criteria are shown in the grid.

Most of the steps make use of the Object Inspector.

  1. Start a new application and select menu item File > Save all.
  2. Save the project as BufDatasetDemo1 and the unit as uBufDatasetDemo1.
  3. Drop onto the form a TBufDataset and TDataSource (Data Access tab), a TDBNavigator and TDBGrid (Data Controls tab) and a CheckBox (Standard tab).
  4. In the Object Inspector, set the Filtered property of BufDataset1 to true and enter Strings for its IndexFieldNames property.
  5. In the Object Inspector click on the triangle to the left of BufDataset1, right click on <unknown collection>: TFieldDefs and select the menu item Add Item.
  6. Change the data type of the new FieldDef to ftString, its name to Strings and its size to 10.
  7. Similarly create a FieldDef named Integers with type ftInteger, Size 4 and Precision 4.
  8. Similarly create a FieldDef named Floats with type ftFloat, Size 8 and Precision 4.
  9. In the Object Inspector, select BufDataset1 for the Dataset property of DataSource1.
    BufDataset1 is, of course, the only dataset in the drop-down list.
  10. Similarly, set the DataSource property of DBGrid1 and DBNavigator1 to DataSource1.
  11. Right-click on DBGrid1 in the Object Inspector and select the menu item Edit Columns ... .
  12. Click the + icon three times to add three columns.
  13. Select the FieldName property of the first column to be Strings, the second to be Integers and the third to be Floats.
    The column titles will given the same name as the field name, but you can change them if you wish.
  14. Set the Checked property of CheckBox1 to be true and change its Caption property to Filtered.
  15. Set the width of DBGrid1 to be 300 and suitably position it and the other visible components, DBNavigator1 and CheckBox1. Adjust the size of the form.
  16. Double click on the form to create the FormCreate procedure and paste the following code into it between begin and end.
     if FileExists('demo1.bds') then
        with BufDataSet1 do
            Fields[0].Value := 'One';
            Fields[1].Value := 1;
            Fields[2].Value := 1.2;
            Fields[0].Value := 'Two';
            Fields[1].Value := 2;
            Fields[2].Value := 2.3;
            Fields[0].Value := 'Three';
            Fields[1].Value := 3;
            Fields[2].Value := 3.4;
            Fields[0].Value := 'Four';
            Fields[1].Value := 4;
            Fields[2].Value := 4.5;

    Note that we include the instruction CreateDataset and do not change the value of the Active property of the dataset to true (as we did with the MemDataset in the previous section).

  17. Double click in the OnClose event edit box of the form to create the FormClose procedure and paste these lines of code into it between begin and end:
      BufDataSet1.Filtered := False;
  18. Double click in the OnFilterRecord property of BufDataset1 and paste this line of code between begin and end of the newly created procedure:
      Accept := BufDataSet1.Fields[0].AsString[1] = 'T';    
  19. Double click in the OnChange event of CheckBox1 and paste this line of code between begin and end of the newly created procedure:
      BufDataSet1.Filtered := Checkbox1.Checked;    
  20. Execute the program:

    Filtered sorted data

    As expected, only the strings beginning with T are shown and they are in alphabetical order.

  21. Uncheck the CheckBox to view all of the records. Confirm that you can navigate through the records, and click on the + icon to add a new row.
  22. Type in the data Five, 5 and 5.6 then click in another row or on the Post (tick) icon to store the changes. Close the application then execute it again.
    You should find that your new data has been saved to file and reloaded successfully.
  23. Try editing data and also typing erroneous data into the number columns or a string longer than ten characters into the first column.

The second demo (on the next page) requires the same kind of set-up and has a ComboBox to choose the Sort field, a label, two labelled edit boxes to enter a range for the filter and a button to execute the filter. It transfers the initial (real world) data from arrays to the dataset.

Programming - a skill for life!

Creating, editing, and searching a Firebird database, printing a report, stored procedures and ClientDataSets