Using MemDataSets in Lazarus

We have experimented with the TMemDataset component in Lazarus and found it trickier to use and less reliable and versatile than the TClientDataset component in Delphi. 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.
Writing your own code to achieve all of the above with a file of records and a string grid would take much longer. Also, many of the steps required will be useful when accessing databases.

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 MemDatasetDemo1 and the unit as uMemDatasetDemo1.
  3. Drop onto the form a TMemDataset and TDataSource (Data Access tab) and a TDBNavigator and TDBGrid (Data Controls tab).
  4. In the Object Inspector, right-click on MemDataset1 and select menu item Create dataset.
    This step does not provide you with the opportunity to enter some data as it does in Delphi's TClientDateset and it might not be necessary in Lazarus, but it does no harm.
  5. Click in the FieldDefs edit box then on the ellipsis icon to bring up the FieldDefs editor.
  6. Click on the + icon to add a FieldDef and change its data type 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 MemDataset1 for the Dataset property of DataSource1.
    MemDataset1 is, of course, the only dataset in the drop-down list.
  10. Similarly, set the DataSource property of DBGrid1 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 width of DBGrid1 to be 300 and suitably position it and the other visible component, DBNavigator1.
  15. Select the DataSource property of DBNavigator1 to be DataSource1.
  16. Double click on the form to create the FormCreate procedure and paste the following code into it between begin and end.
      with MemDataSet1 do
          if fileExists('demo.mds') then
              LoadFromFile('demo.mds') ;
              Active := true;
              Active := true;
              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;
  17. Double click in the OnClose event edit box to create the FormClose procedure and paste this line of code into it between begin and end:
  18. Execute the program, confirm that you can navigate through the records, and click on the + icon to add a new row:
    Inserting a row

    Inserting a row

  19. Type in the data Four, 4 and 4.5 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.
  20. 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 labelled edit box to contain search data and a button to execute the search. 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