Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 5

Project PARQUERY

Project Structure


PARQUERY.DPR

program ParQuery;

uses
  Forms,
  ParQForm in 'ParQForm.pas' {QueryForm};

{$R *.RES}

begin
  Application.CreateForm(TQueryForm, QueryForm);
  Application.Run;
end.

PARQFORM.PAS

unit ParQForm;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, ExtCtrls, DBCtrls, StdCtrls, Mask, DB, DBTables, DBLookup,
  Grids, DBGrids;

type
  TQueryForm = class(TForm)
    DataSource1: TDataSource;
    Query1: TQuery;
    ListBox1: TListBox;
    DBGrid1: TDBGrid;
    Splitter1: TSplitter;
    Query2: TQuery;
    DBNavigator1: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Query1BeforePost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  QueryForm: TQueryForm;

implementation

{$R *.DFM}

procedure TQueryForm.FormCreate(Sender: TObject);
begin
  // get the list of continents
  Query2.Open;
  while not Query2.EOF do
  begin
    ListBox1.Items.Add (Query2.Fields [0].AsString);
    Query2.Next;
  end;
  ListBox1.ItemIndex := 0;

  // prepare and open the first query
  Query1.Prepare;
  Query1.Params[0].Value := ListBox1.Items [0];
  Query1.Open;
end;

procedure TQueryForm.ListBox1Click(Sender: TObject);
begin
  Query1.Close;
  Query1.Params[0].Value :=
    ListBox1.Items [Listbox1.ItemIndex];
  Query1.Open;
end;

procedure TQueryForm.FormDestroy(Sender: TObject);
begin
  Query1.Close;
  Query1.Unprepare;
end;

procedure TQueryForm.Query1BeforePost(DataSet: TDataSet);
var
  StrNewCont: string;
begin
  // add the continent, if not already in the list
  StrNewCont := Query1.FieldByName ('Continent').AsString;
  if ListBox1.Items.IndexOf (StrNewCont) < 0 then
    ListBox1.Items.Add (StrNewCont);
end;

end.

PARQFORM.DFM

object QueryForm: TQueryForm
  Left = 254
  Top = 118
  Width = 457
  Height = 271
  Caption = 'Parameterized Query'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clBlack
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Visible = True
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Splitter1: TSplitter
    Left = 137
    Top = 25
    Width = 1
    Height = 219
    Cursor = crHSplit
  end
  object ListBox1: TListBox
    Left = 0
    Top = 25
    Width = 137
    Height = 219
    Align = alLeft
    ItemHeight = 13
    TabOrder = 0
    OnClick = ListBox1Click
  end
  object DBGrid1: TDBGrid
    Left = 138
    Top = 25
    Width = 311
    Height = 219
    Align = alClient
    DataSource = DataSource1
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clBlack
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object DBNavigator1: TDBNavigator
    Left = 0
    Top = 0
    Width = 449
    Height = 25
    DataSource = DataSource1
    Align = alTop
    TabOrder = 2
  end
  object DataSource1: TDataSource
    DataSet = Query1
    Left = 32
    Top = 64
  end
  object Query1: TQuery
    BeforePost = Query1BeforePost
    DatabaseName = 'DBDEMOS'
    RequestLive = True
    SQL.Strings = (
      'select * from Country '
      'where Continent = :Continent')
    Left = 32
    Top = 16
    ParamData = <
      item
        DataType = ftString
        Name = 'Continent'
        ParamType = ptUnknown
        Value = 'North America'
      end>
  end
  object Query2: TQuery
    DatabaseName = 'DBDEMOS'
    SQL.Strings = (
      'select distinct Continent '
      'from Country')
    Left = 32
    Top = 120
  end
end