Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 14 - Project GetMax

Project Structure

GetMax.dpr
program GetMax;

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

{$R *.RES}

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

interface

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

type
  TForm1 = class(TForm)
    EmpTable: TTable;
    EmpQuery: TQuery;
    Database1: TDatabase;
    BtnTable: TButton;
    BtnQuery: TButton;
    procedure BtnTableClick(Sender: TObject);
    procedure BtnQueryClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BtnTableClick(Sender: TObject);
var
  MaxSalary: Double;
  Tick: Cardinal;
begin
  Tick := GetTickCount;
  EmpTable.Open;
  EmpTable.First;
  MaxSalary := 0;
  while not EmpTable.Eof do
  begin
    if EmpTable.FieldByName ('Salary').AsCurrency > MaxSalary then
      MaxSalary := EmpTable.FieldByName ('Salary').AsCurrency;
    EmpTable.Next;
  end;
  Caption := 'Time: ' + IntToStr (GetTickCount - Tick);
  ShowMessage (FloatToStr (MaxSalary));
end;

procedure TForm1.BtnQueryClick(Sender: TObject);
var
  Tick: Cardinal;
begin
  Tick := GetTickCount;
  EmpQuery.Open;
  Caption := 'Time: ' + IntToStr (GetTickCount - Tick);
  ShowMessage (EmpQuery.Fields[0].AsString);
end;

end.
MaxForm.dfm
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 281
  Height = 184
  Caption = 'GetMax'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object BtnTable: TButton
    Left = 96
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Table Max'
    TabOrder = 0
    OnClick = BtnTableClick
  end
  object BtnQuery: TButton
    Left = 96
    Top = 72
    Width = 75
    Height = 25
    Caption = 'Query Max'
    TabOrder = 1
    OnClick = BtnQueryClick
  end
  object EmpTable: TTable
    DatabaseName = 'IB'
    TableName = 'EMPLOYEE'
    Left = 56
    Top = 8
  end
  object EmpQuery: TQuery
    DatabaseName = 'IB'
    SQL.Strings = (
      'select Max(Salary) from Employee ')
    Left = 48
    Top = 96
  end
  object Database1: TDatabase
    AliasName = 'IBLOCAL'
    Connected = True
    DatabaseName = 'IB'
    LoginPrompt = False
    Params.Strings = (
      'USER NAME=SYSDBA'
      'PASSWORD=masterkey')
    SessionName = 'Default'
    Left = 24
    Top = 48
  end
end