Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 13 - Project DbAware

Project Structure

DbAware.dpr
program DbAware;

uses
  Forms,
  DbAwForm in 'DbAwForm.pas' {DbaForm};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TDbaForm, DbaForm);
  Application.Run;
end.
DbAwForm.pas
unit DbAwForm;

interface

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

type
  TDbaForm = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    Panel1: TPanel;
    Button2: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    DBRadioGroup1: TDBRadioGroup;
    DBCheckBox1: TDBCheckBox;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBComboBox1: TDBComboBox;
    DBText1: TDBText;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure AddRandomData;
  end;

var
  DbaForm: TDbaForm;

implementation

{$R *.DFM}

procedure TDbaForm.FormCreate(Sender: TObject);
begin
  if not Table1.Exists then
    Table1.CreateTable;
  Table1.Open;
end;

procedure TDbaForm.Button2Click(Sender: TObject);
begin
  AddRandomData;
end;

const
  FirstNames : array [1..10] of string =
    ('John', 'Paul', 'Mark', 'Joseph', 'Bill',
    'Peter', 'Tim', 'Ralph', 'Bob', 'Gary');
  LastNames : array [1..10] of string =
    ('Ford', 'Osborse', 'White', 'MacDonald', 'Lee',
    'Young', 'Parker', 'Reed', 'Gates', 'Green');
  NoDept = 4;
  NoBranch = 30;
  NewRecords = 10;

procedure TDbaForm.AddRandomData;
var
  I: Integer;
begin
  Randomize;
  for I := 1 to NewRecords do
    Table1.InsertRecord ([
      LastNames [Random (High (LastNames)) + 1],
      FirstNames [Random (High (FirstNames)) + 1],
      Random (NoDept) + 1,
      DbComboBox1.Items [Random (NoBranch) + 1],
      Boolean (Random (2)),
      Date - Random (1000)]);
  ShowMessage (IntToStr (NewRecords) + ' added');
end;

end.
DbAwForm.dfm
object DbaForm: TDbaForm
  Left = 196
  Top = 109
  Width = 489
  Height = 289
  Caption = 'Workers (Data Aware Demo)'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 481
    Height = 41
    Align = alTop
    TabOrder = 0
    object Button2: TButton
      Left = 16
      Top = 8
      Width = 121
      Height = 25
      Caption = '&Add Random Data'
      TabOrder = 0
      OnClick = Button2Click
    end
  end
  object PageControl1: TPageControl
    Left = 0
    Top = 41
    Width = 481
    Height = 221
    ActivePage = TabSheet1
    Align = alClient
    TabIndex = 0
    TabOrder = 1
    object TabSheet1: TTabSheet
      Caption = 'Record View'
      object DBText1: TDBText
        Left = 336
        Top = 16
        Width = 42
        Height = 13
        AutoSize = True
        DataField = 'HireDate'
        DataSource = DataSource1
      end
      object Label1: TLabel
        Left = 280
        Top = 16
        Width = 46
        Height = 13
        Caption = 'Hire date:'
      end
      object Label2: TLabel
        Left = 18
        Top = 52
        Width = 51
        Height = 13
        Caption = '&Last Name'
        FocusControl = DBEdit1
      end
      object Label3: TLabel
        Left = 18
        Top = 80
        Width = 50
        Height = 13
        Caption = '&First Name'
        FocusControl = DBEdit2
      end
      object Label4: TLabel
        Left = 18
        Top = 112
        Width = 34
        Height = 13
        Caption = '&Branch'
        FocusControl = DBComboBox1
      end
      object DBNavigator1: TDBNavigator
        Left = 16
        Top = 8
        Width = 240
        Height = 25
        DataSource = DataSource1
        TabOrder = 0
      end
      object DBRadioGroup1: TDBRadioGroup
        Left = 272
        Top = 48
        Width = 185
        Height = 121
        Caption = '&Department'
        DataField = 'Department'
        DataSource = DataSource1
        Items.Strings = (
          'Sales'
          'Accounting'
          'Production'
          'Management')
        TabOrder = 1
        Values.Strings = (
          '1'
          '2'
          '3'
          '4')
      end
      object DBCheckBox1: TDBCheckBox
        Left = 80
        Top = 144
        Width = 81
        Height = 17
        Caption = '&Senior'
        DataField = 'Senior'
        DataSource = DataSource1
        TabOrder = 2
        ValueChecked = 'True'
        ValueUnchecked = 'False'
      end
      object DBEdit1: TDBEdit
        Left = 80
        Top = 48
        Width = 121
        Height = 21
        DataField = 'LastName'
        DataSource = DataSource1
        TabOrder = 3
      end
      object DBEdit2: TDBEdit
        Left = 80
        Top = 80
        Width = 121
        Height = 21
        DataField = 'FirstName'
        DataSource = DataSource1
        TabOrder = 4
      end
      object DBComboBox1: TDBComboBox
        Left = 80
        Top = 112
        Width = 121
        Height = 21
        DataField = 'Branch'
        DataSource = DataSource1
        ItemHeight = 13
        Items.Strings = (
          'Baltimore'
          'Berlin'
          'Boston'
          'Brasilia'
          'Cape Town'
          'Chicago'
          'Dallas'
          'Denver'
          'Dublin'
          'Las Vegas'
          'London'
          'Los Angeles'
          'Louisville'
          'Mexico City'
          'Miami'
          'Minneapolis'
          'Moscow'
          'New Orleans'
          'New York'
          'Orlando'
          'Rome'
          'Salt Lake City'
          'San Diego'
          'San Francisco'
          'San Jose'
          'Seattle'
          'Singapore'
          'Tokio'
          'Toronto'
          'Vancouver')
        Sorted = True
        TabOrder = 5
        TabStop = True
      end
    end
    object TabSheet2: TTabSheet
      Caption = 'Grid View'
      ImageIndex = 1
      object DBGrid1: TDBGrid
        Left = 0
        Top = 0
        Width = 473
        Height = 193
        Align = alClient
        DataSource = DataSource1
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'MS Sans Serif'
        TitleFont.Style = []
      end
    end
  end
  object Table1: TTable
    DatabaseName = 'DBDEMOS'
    FieldDefs = <
      item
        Name = 'LastName'
        DataType = ftString
        Size = 20
      end
      item
        Name = 'FirstName'
        DataType = ftString
        Size = 20
      end
      item
        Name = 'Department'
        DataType = ftSmallint
      end
      item
        Name = 'Branch'
        DataType = ftString
        Size = 20
      end
      item
        Name = 'Senior'
        DataType = ftBoolean
      end
      item
        Name = 'HireDate'
        DataType = ftDate
      end>
    StoreDefs = True
    TableName = 'Workers'
    Left = 384
    Top = 8
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 344
    Top = 8
  end
end