Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 5

Project DBAWARE2

Project Structure


DBAWARE2.DPR

program DbAware2;

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,
  ADODB;

type
  TDbaForm = class(TForm)
    Table1: TADOTable;
    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;
    ADOConnection: TADOConnection;
    ADOCommand: TADOCommand;
    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);
var
  TablesList: TStringList;
begin
  // read table names from database
  TablesList := TStringList.Create;
  try
    ADOConnection.GetTableNames (TablesList);
    // check if the table already exists
    if TablesList.IndexOf (Table1.TableName) < 0 then
      // create it
      ADOCommand.Execute;
    // open the new or existing table
    Table1.Open;
  finally
    TablesList.Free;
  end;
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 = 'DbAware2 (MS Access)'
  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
    TabOrder = 1
    object TabSheet1: TTabSheet
      Caption = 'Record View'
      object DBText1: TDBText
        Left = 336
        Top = 16
        Width = 3
        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
      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: TADOTable
    Active = True
    CacheSize = 50
    Connection = ADOConnection
    CursorLocation = clUseServer
    MaxRecords = 50
    TableDirect = True
    TableName = 'Workers'
    Left = 384
    Top = 8
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 344
    Top = 8
  end
  object ADOConnection: TADOConnection
    Connected = True
    ConnectionString =
       'Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data ' +
      'Source=C:\md5code\Part3\12\data\MdData.mdb;Mode=Share Deny None;' +
      'Extended Properties="";Locale Identifier=1033;Jet OLEDB:System d' +
      'atabase="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Passwor' +
      'd="";Jet OLEDB:Engine Type=4;Jet OLEDB:Database Locking Mode=0;J' +
      'et OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transac' +
      'tions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create Syst' +
      'em Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don' +
      '''t Copy Locale on Compact=False;Jet OLEDB:Compact Without Replic'  +
      'a Repair=False;Jet OLEDB:SFP=False'
    LoginPrompt = False
    Mode = cmShareDenyNone
    Provider = 'Microsoft.Jet.OLEDB.4.0'
    Left = 424
    Top = 8
  end
  object ADOCommand: TADOCommand
    CommandText =
       'create table workers ('#13#10'  firstname TEXT(30),'#13#10'  lastname TEXT(3'     +
      '0),'#13#10'  department INTEGER,'#13#10'  branch TEXT (20),'#13#10'  senior YESNO,'       +
      #13#10'  hiredate DATETIME);'
    Connection = ADOConnection
    Parameters = <>
    Left = 296
    Top = 8
  end
end