MemDataset Demonstration with Simple Search

This demonstration requires the same kind of set-up as the simpler example on the previous page, so we provide here the code of the unit and of the form rather than stepwise instructions. Whereas the first example used trivial data, this one is useful for finding areas and capitals of European countries. We copied the areas from this Wikipedia page. The code follows a screenshot of a search in action.

Searching

Searching

Code of Unit

unit uMemDatasetMemo2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, memds, FileUtil, Forms, Controls, Graphics, Dialogs,
  DbCtrls, DBGrids, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    btnSearch: TButton;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    leSearch: TLabeledEdit;
    MemDataset1: TMemDataset;
    procedure btnSearchClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    const
      CountriesAndCapitals: array[1..78] of string =
       ('England', 'London', 'Scotland', 'Edinburgh', 'Northern Ireland', 'Belfast',
        'Wales', 'Cardiff', 'Ireland', 'Dublin', 'Germany', 'Berlin',
        'France', 'Paris', 'Greece', 'Athens', 'Finland', 'Helsinki',
        'Hungary', 'Budapest', 'Italy', 'Rome', 'Liechtenstein', 'Vaduz',
        'Lithuania', 'Vilnius', 'Luxembourg', 'Luxembourg', 'Poland', 'Warsaw',
        'Portugal', 'Lisbon', 'Romania', 'Bucharest', 'Russia', 'Moscow',
        'Serbia', 'Belgrade', 'Slovakia', 'Bratislava', 'Slovenia', 'Ljubljana',
        'Spain', 'Madrid', 'Sweden', 'Stockholm', 'Switzerland', 'Bern',
        'Norway', 'Oslo', 'Austria', 'Vienna', 'Belgium', 'Brussels',
        'Bosnia and Herzegovina', 'Sarajevo', 'Bulgaria', 'Sofia', 'Netherlands', 'Amsterdam',
        'Czech Republic', 'Prague', 'Moldova', 'Chisinau', 'Latvia', 'Riga',
        'Iceland', 'Reykjavik', 'Malta', 'Valletta', 'Macedonia', 'Skopje',
        'Montenegro', 'Podgorica', 'Turkey', 'Ankara', 'Estonia', 'Tallinn');

      Areas: array[1..39] of integer = (130395, 78387, 14148, 20732, 70280, 357021,
                                        547030, 131940, 338424, 93030, 301230, 160,
                                        65200, 2586, 312685, 92391, 23839, 17075400,
                                        88361, 48845, 20273, 505782, 449964, 41290,
                                        385252, 83858, 30510, 51129, 110910, 41526,
                                        78866, 33843, 64589, 103001, 316, 25713,
                                        13812, 783562, 45226);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i : integer;
begin
  with  MemDataSet1 do
    begin
      if fileExists('demo2.mds') then
        begin
          LoadFromFile('demo2.mds') ;
          Active := true;
        end
      else
        begin
          Active := true;
          for i := 1 to 39 do
            begin
              Append;
              Fields[0].Value := CountriesAndCapitals[i * 2 - 1];
              Fields[1].Value := Areas[i];
              Fields[2].Value := CountriesAndCapitals[i * 2];
            end;
          Post;
          SaveToFile('demo2.mds');
          First;
        end;
     end;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  MemDataSet1.SaveToFile('demo2.mds');
end;

procedure TForm1.btnSearchClick(Sender: TObject);
begin
  with MemDataset1 do
    begin
      First;
      Tag := 0; // Inbuilt user variable
      repeat
        if Fields[0].AsString = leSearch.Text then
          Tag := 1
        else
          Next;
      until EOF or (Tag = 1);
      if Tag = 0 then
        ShowMessage('Not Found');
    end;
end;

end.

Code of Form

object Form1: TForm1
  Left = 447
  Height = 240
  Top = 174
  Width = 360
  Caption = 'Areas and Capitals (Europe)'
  ClientHeight = 240
  ClientWidth = 360
  OnClose = FormClose
  OnCreate = FormCreate
  LCLVersion = '1.2.4.0'
  object DBNavigator1: TDBNavigator
    Left = 64
    Height = 25
    Top = 168
    Width = 241
    BevelOuter = bvNone
    ChildSizing.EnlargeHorizontal = crsScaleChilds
    ChildSizing.EnlargeVertical = crsScaleChilds
    ChildSizing.ShrinkHorizontal = crsScaleChilds
    ChildSizing.ShrinkVertical = crsScaleChilds
    ChildSizing.Layout = cclLeftToRightThenTopToBottom
    ChildSizing.ControlsPerLine = 100
    ClientHeight = 25
    ClientWidth = 241
    DataSource = DataSource1
    Options = []
    TabOrder = 0
  end
  object DBGrid1: TDBGrid
    Left = 1
    Height = 144
    Top = 16
    Width = 357
    Color = clWindow
    Columns = <    
      item
        MaxSize = 200
        Title.Caption = 'Country'
        Width = 150
        FieldName = 'Country'
      end    
      item
        MaxSize = 200
        Title.Caption = 'Area (sq km)'
        Width = 75
        FieldName = 'Area (sq km)'
      end    
      item
        MaxSize = 200
        Title.Caption = 'Capital'
        Width = 100
        FieldName = 'Capital'
      end>
    DataSource = DataSource1
    TabOrder = 1
  end
  object btnSearch: TButton
    Left = 280
    Height = 25
    Top = 200
    Width = 75
    Caption = 'Search'
    OnClick = btnSearchClick
    TabOrder = 2
  end
  object leSearch: TLabeledEdit
    Left = 120
    Height = 23
    Top = 200
    Width = 156
    EditLabel.AnchorSideTop.Control = leSearch
    EditLabel.AnchorSideTop.Side = asrCenter
    EditLabel.AnchorSideRight.Control = leSearch
    EditLabel.AnchorSideBottom.Control = leSearch
    EditLabel.AnchorSideBottom.Side = asrBottom
    EditLabel.Left = 2
    EditLabel.Height = 15
    EditLabel.Top = 204
    EditLabel.Width = 115
    EditLabel.Caption = 'Enter country to find: '
    EditLabel.ParentColor = False
    LabelPosition = lpLeft
    TabOrder = 3
    Text = 'Bosnia and Herzegovina'
  end
  object DataSource1: TDataSource
    DataSet = MemDataset1
    left = 8
    top = 104
  end
  object MemDataset1: TMemDataset
    FieldDefs = <    
      item
        Name = 'Country'
        DataType = ftString
        Precision = 0
        Size = 25
      end    
      item
        Name = 'Area (sq km)'
        DataType = ftInteger
        Precision = 8
        Size = 4
      end    
      item
        Name = 'Capital'
        DataType = ftString
        Precision = 0
        Size = 25
      end>
    left = 56
    top = 104
  end
end
    
Programming - a skill for life!

How to set-up and use the TMemDataset component in Lazarus