Marco Cantù 1998, Mastering Delphi 4

Project: SEARCH.DPR


Project Structure


SEARCH.DPR

program Search;

uses
  Forms,
  SearchF in 'SearchF.pas' {SearchForm};

{$R *.RES}

begin
  Application.CreateForm(TSearchForm, SearchForm);
  Application.Run;
end.

SEARCHF.PAS

unit SearchF;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  StdCtrls, Forms, DBCtrls, DB, Buttons, DBTables, Mask, ExtCtrls,
  Dialogs, DBActns, ActnList;

type
  TSearchForm = class(TForm)
    ScrollBox: TScrollBox;
    Label1: TLabel;
    EditEmpNo: TDBEdit;
    Label2: TLabel;
    EditLastName: TDBEdit;
    Label3: TLabel;
    EditFirstName: TDBEdit;
    Label4: TLabel;
    EditPhoneExt: TDBEdit;
    Label5: TLabel;
    EditHireDate: TDBEdit;
    Label6: TLabel;
    EditSalary: TDBEdit;
    Panel1: TPanel;
    DataSource1: TDataSource;
    Panel2: TPanel;
    Table1: TTable;
    SpeedButtonFirst: TSpeedButton;
    SpeedButtonPrior: TSpeedButton;
    SpeedButtonNext: TSpeedButton;
    SpeedButtonLast: TSpeedButton;
    EditName: TEdit;
    SpeedButtonGoto: TSpeedButton;
    SpeedButtonGoNear: TSpeedButton;
    Table1Salary: TFloatField;
    Table1EmpNo: TIntegerField;
    Table1LastName: TStringField;
    Table1FirstName: TStringField;
    Table1PhoneExt: TStringField;
    Table1HireDate: TDateTimeField;
    ActionList1: TActionList;
    First1: TDataSetFirst;
    Last1: TDataSetLast;
    Next1: TDataSetNext;
    Prior1: TDataSetPrior;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButtonGotoClick(Sender: TObject);
    procedure SpeedButtonGoNearClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  SearchForm: TSearchForm;

implementation

{$R *.DFM}

procedure TSearchForm.FormCreate(Sender: TObject);
begin
  Table1.First;
end;

procedure TSearchForm.SpeedButtonGotoClick(Sender: TObject);
begin
  // locate
  if not Table1.Locate ('LastName', EditName.Text, []) then
    MessageDlg ('Name not found', mtError, [mbOk], 0);

  // short-hand
{  if not Table1.FindKey ([EditName.Text]) then
    MessageDlg ('Name not found', mtError, [mbOk], 0);

  // alternative code:
  {Table1.SetKey;
  Table1 ['LastName'] := EditName.Text;
  Table1.KeyFieldCount := 1;
  if not Table1.GotoKey then
    MessageDlg ('Name not found', mtError, [mbOk], 0);}
end;

procedure TSearchForm.SpeedButtonGoNearClick(Sender: TObject);
begin
  // short-hand
//  Table1.FindNearest ([EditName.Text]);

  // alternative code:
  Table1.SetKey;
  Table1 ['LastName'] := EditName.Text;
  Table1.GotoNearest;
end;

end.

SEARCHF.DFM

object SearchForm: TSearchForm
  Left = 275
  Top = 158
  Width = 383
  Height = 190
  ActiveControl = Panel1
  Caption = 'Table Search'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clBlack
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Position = poScreenCenter
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 375
    Height = 41
    Align = alTop
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -11
    Font.Name = 'Arial'
    Font.Style = []
    ParentFont = False
    TabOrder = 0
    object SpeedButtonFirst: TSpeedButton
      Left = 8
      Top = 8
      Width = 40
      Height = 25
      Action = First1
    end
    object SpeedButtonGoto: TSpeedButton
      Left = 312
      Top = 8
      Width = 25
      Height = 25
      Caption = '->'
      OnClick = SpeedButtonGotoClick
    end
    object SpeedButtonPrior: TSpeedButton
      Left = 48
      Top = 8
      Width = 40
      Height = 25
      Action = Prior1
    end
    object SpeedButtonNext: TSpeedButton
      Left = 88
      Top = 8
      Width = 40
      Height = 25
      Action = Next1
    end
    object SpeedButtonLast: TSpeedButton
      Left = 128
      Top = 8
      Width = 40
      Height = 25
      Action = Last1
    end
    object SpeedButtonGoNear: TSpeedButton
      Left = 336
      Top = 8
      Width = 25
      Height = 25
      Caption = '~>'
      OnClick = SpeedButtonGoNearClick
    end
    object EditName: TEdit
      Left = 175
      Top = 11
      Width = 137
      Height = 21
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clBlack
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      ParentFont = False
      TabOrder = 0
      Text = 'Williams'
    end
  end
  object Panel2: TPanel
    Left = 0
    Top = 41
    Width = 375
    Height = 122
    Align = alClient
    BevelInner = bvLowered
    BorderWidth = 4
    Caption = 'Panel2'
    TabOrder = 1
    object ScrollBox: TScrollBox
      Left = 6
      Top = 6
      Width = 363
      Height = 110
      HorzScrollBar.Margin = 6
      VertScrollBar.Margin = 6
      Align = alClient
      BorderStyle = bsNone
      TabOrder = 0
      object Label1: TLabel
        Left = 230
        Top = 17
        Width = 60
        Height = 13
        Alignment = taRightJustify
        AutoSize = False
        Caption = '&Emp No'
        FocusControl = EditEmpNo
      end
      object Label2: TLabel
        Left = 14
        Top = 15
        Width = 60
        Height = 13
        Alignment = taRightJustify
        AutoSize = False
        Caption = '&Last Name'
        FocusControl = EditLastName
      end
      object Label3: TLabel
        Left = 14
        Top = 36
        Width = 60
        Height = 13
        Alignment = taRightJustify
        AutoSize = False
        Caption = '&First Name'
        FocusControl = EditFirstName
      end
      object Label4: TLabel
        Left = 14
        Top = 58
        Width = 60
        Height = 13
        Alignment = taRightJustify
        AutoSize = False
        Caption = '&Phone Ext'
        FocusControl = EditPhoneExt
      end
      object Label5: TLabel
        Left = 230
        Top = 39
        Width = 60
        Height = 13
        Alignment = taRightJustify
        AutoSize = False
        Caption = '&Hire Date'
        FocusControl = EditHireDate
      end
      object Label6: TLabel
        Left = 14
        Top = 79
        Width = 60
        Height = 13
        Alignment = taRightJustify
        AutoSize = False
        Caption = '&Salary'
        FocusControl = EditSalary
      end
      object EditEmpNo: TDBEdit
        Left = 296
        Top = 14
        Width = 57
        Height = 21
        DataField = 'EmpNo'
        DataSource = DataSource1
        TabOrder = 0
      end
      object EditLastName: TDBEdit
        Left = 80
        Top = 12
        Width = 97
        Height = 21
        DataField = 'LastName'
        DataSource = DataSource1
        TabOrder = 1
      end
      object EditFirstName: TDBEdit
        Left = 80
        Top = 33
        Width = 97
        Height = 21
        DataField = 'FirstName'
        DataSource = DataSource1
        TabOrder = 2
      end
      object EditPhoneExt: TDBEdit
        Left = 80
        Top = 55
        Width = 97
        Height = 21
        DataField = 'PhoneExt'
        DataSource = DataSource1
        TabOrder = 3
      end
      object EditHireDate: TDBEdit
        Left = 296
        Top = 36
        Width = 57
        Height = 21
        DataField = 'HireDate'
        DataSource = DataSource1
        TabOrder = 4
      end
      object EditSalary: TDBEdit
        Left = 80
        Top = 76
        Width = 97
        Height = 21
        DataField = 'Salary'
        DataSource = DataSource1
        TabOrder = 5
      end
    end
  end
  object DataSource1: TDataSource
    AutoEdit = False
    DataSet = Table1
    Left = 279
    Top = 109
  end
  object Table1: TTable
    Active = True
    DatabaseName = 'DBDEMOS'
    IndexFieldNames = 'LastName;FirstName'
    TableName = 'employee.db'
    Left = 332
    Top = 109
    object Table1Salary: TFloatField
      FieldName = 'Salary'
    end
    object Table1EmpNo: TIntegerField
      FieldName = 'EmpNo'
    end
    object Table1LastName: TStringField
      FieldName = 'LastName'
    end
    object Table1FirstName: TStringField
      FieldName = 'FirstName'
      Size = 15
    end
    object Table1PhoneExt: TStringField
      FieldName = 'PhoneExt'
      Size = 4
    end
    object Table1HireDate: TDateTimeField
      FieldName = 'HireDate'
    end
  end
  object ActionList1: TActionList
    Left = 214
    Top = 111
    object First1: TDataSetFirst
      Category = 'Dataset'
      Caption = '&First'
      Hint = 'First'
      ImageIndex = 0
      DataSource = DataSource1
    end
    object Last1: TDataSetLast
      Category = 'Dataset'
      Caption = '&Last'
      Hint = 'Last'
      ImageIndex = 3
      DataSource = DataSource1
    end
    object Next1: TDataSetNext
      Category = 'Dataset'
      Caption = '&Next'
      Hint = 'Next'
      ImageIndex = 2
      DataSource = DataSource1
    end
    object Prior1: TDataSetPrior
      Category = 'Dataset'
      Caption = '&Prior'
      Hint = 'Prior'
      ImageIndex = 1
      DataSource = DataSource1
    end
  end
end


Copyright Marco Cantù 1998