Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 5

Project DYNQUERY

Project Structure


DYNQUERY.DPR

program DynQuery;

uses
  Forms,
  DynQForm in 'DynQForm.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

DYNQFORM.PAS

unit DynQForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids, Db, DBTables;

type
  TForm1 = class(TForm)
    Query1: TQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Query1.Open;
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  Query1.Close;
  Query1.Sql.Clear;
  Query1.Sql.Add ('select * from Country');
  Query1.Open;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  Query1.Close;
  Query1.Sql.Clear;
  Query1.Sql.Add ('select * from Country');
  Query1.Sql.Add ('where Continent = "' +
    (Sender as TRadioButton).Caption + '"');
  Query1.Open;
end;

procedure TForm1.RadioButton4Click(Sender: TObject);
begin
  Query1.Close;
  if (Edit1.Text <> '') then
  begin
    Query1.Sql.Clear;
    Query1.Sql.Add ('select * from Country');
    Query1.Sql.Add ('where ' + Edit1.Text);
  end;
  try
    Query1.Open;
  except
    on EDatabaseError do
      ShowMessage ('Invalid condition:'#13 +
        Edit1.Text);
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  RadioButton4.Enabled := Edit1.Text <> '';
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if RadioButton4.Checked then
      RadioButton4Click (Self)
    else
      RadioButton4.Checked := True;
    Key := #0;
  end;
end;

end.

DYNQFORM.DFM

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 534
  Height = 305
  Caption = 'DynQuery'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 0
    Top = 0
    Width = 526
    Height = 173
    Align = alClient
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object GroupBox1: TGroupBox
    Left = 0
    Top = 173
    Width = 526
    Height = 105
    Align = alBottom
    Caption = 'Selection'
    TabOrder = 1
    object RadioButton1: TRadioButton
      Left = 16
      Top = 16
      Width = 113
      Height = 17
      Caption = 'All'
      Checked = True
      TabOrder = 0
      TabStop = True
      OnClick = RadioButton1Click
    end
    object RadioButton2: TRadioButton
      Left = 16
      Top = 35
      Width = 113
      Height = 17
      Caption = 'North America'
      TabOrder = 1
      OnClick = RadioButton2Click
    end
    object RadioButton3: TRadioButton
      Left = 16
      Top = 53
      Width = 137
      Height = 17
      Caption = 'South America'
      TabOrder = 2
      OnClick = RadioButton2Click
    end
    object RadioButton4: TRadioButton
      Left = 16
      Top = 72
      Width = 81
      Height = 17
      Caption = 'Custom:'
      Enabled = False
      TabOrder = 3
      OnClick = RadioButton4Click
    end
    object Edit1: TEdit
      Left = 104
      Top = 72
      Width = 193
      Height = 21
      TabOrder = 4
      OnChange = Edit1Change
      OnKeyPress = Edit1KeyPress
    end
  end
  object Query1: TQuery
    DatabaseName = 'DBDEMOS'
    SQL.Strings = (
      'select * from Country')
    Left = 32
    Top = 80
  end
  object DataSource1: TDataSource
    DataSet = Query1
    Left = 32
    Top = 32
  end
end