Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 5

Project SENDTODB

Project Structure


SENDTODB.DPR

program SendToDb;

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

{$R *.RES}

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

SENDTOF.PAS

unit SendToF;

interface

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

type
  TForm1 = class(TForm)
    Table1: TTable;
    Table1Name: TStringField;
    Table1Capital: TStringField;
    Table1Continent: TStringField;
    Table1Area: TFloatField;
    Table1Population: TFloatField;
    EditCapital: TEdit;
    EditPopulation: TEdit;
    EditArea: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    ComboContinent: TComboBox;
    Button1: TButton;
    Button2: TButton;
    ComboName: TComboBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ComboNameKeyPress(Sender: TObject; var Key: Char);
    procedure ComboNameClick(Sender: TObject);
  private
    { Private declarations }
  public
    procedure GetData;
    procedure SendData;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.GetData;
begin
  Table1.FindNearest ([ComboName.Text]);
  ComboName.Text := Table1Name.AsString;
  EditCapital.Text := Table1Capital.AsString;
  ComboContinent.Text := Table1Continent.AsString;
  EditArea.Text := Table1Area.AsString;
  EditPopulation.Text := Table1Population.AsString;
end;

procedure TForm1.SendData;
begin
  // raise an exception if there is no name
  if ComboName.Text = '' then
    raise Exception.Create ('Insert the name');

  // check if the record is already in the table    
  if Table1.FindKey ([ComboName.Text]) then
  begin
    // modify found record
    Table1.Edit;
    Table1Capital.AsString := EditCapital.Text;
    Table1Continent.AsString := ComboContinent.Text;
    Table1Area.AsString := EditArea.Text;
    Table1Population.AsString := EditPopulation.Text;
    Table1.Post;
  end
  else
  begin
    // insert new record
    Table1.InsertRecord ([ComboName.Text,
      EditCapital.Text, ComboContinent.Text,
      EditArea.Text, EditPopulation.Text]);
    // add to list
    ComboName.Items.Add (ComboName.Text)
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  GetData;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SendData;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // fill the list of names
  Table1.Open;
  while not Table1.Eof do
  begin
    ComboName.Items.Add (Table1Name.AsString);
    Table1.Next;
  end;
end;

procedure TForm1.ComboNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    GetData;
end;

procedure TForm1.ComboNameClick(Sender: TObject);
begin
  GetData;
end;

end.

SENDTOF.DFM

object Form1: TForm1
  Left = 201
  Top = 113
  Width = 424
  Height = 287
  Caption = 'Send To Database'
  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 Label1: TLabel
    Left = 72
    Top = 88
    Width = 28
    Height = 13
    Caption = '&Name'
  end
  object Label2: TLabel
    Left = 72
    Top = 123
    Width = 32
    Height = 13
    Caption = '&Capital'
    FocusControl = EditCapital
  end
  object Label3: TLabel
    Left = 72
    Top = 158
    Width = 45
    Height = 13
    Caption = 'C&ontinent'
    FocusControl = ComboContinent
  end
  object Label4: TLabel
    Left = 72
    Top = 193
    Width = 50
    Height = 13
    Caption = '&Population'
    FocusControl = EditPopulation
  end
  object Label5: TLabel
    Left = 72
    Top = 228
    Width = 22
    Height = 13
    Caption = '&Area'
    FocusControl = EditArea
  end
  object EditCapital: TEdit
    Left = 160
    Top = 119
    Width = 150
    Height = 21
    TabOrder = 1
  end
  object EditPopulation: TEdit
    Left = 160
    Top = 189
    Width = 150
    Height = 21
    TabOrder = 2
  end
  object EditArea: TEdit
    Left = 160
    Top = 224
    Width = 150
    Height = 21
    TabOrder = 3
  end
  object ComboContinent: TComboBox
    Left = 160
    Top = 154
    Width = 150
    Height = 21
    ItemHeight = 13
    TabOrder = 4
    Items.Strings = (
      'South America'
      'North America'
      'Europe'
      'Asia'
      'Africa')
  end
  object Button1: TButton
    Left = 104
    Top = 24
    Width = 75
    Height = 25
    Caption = '&Get'
    TabOrder = 5
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 200
    Top = 24
    Width = 75
    Height = 25
    Caption = '&Send'
    TabOrder = 6
    OnClick = Button2Click
  end
  object ComboName: TComboBox
    Left = 160
    Top = 83
    Width = 150
    Height = 21
    ItemHeight = 13
    TabOrder = 0
    OnClick = ComboNameClick
    OnKeyPress = ComboNameKeyPress
  end
  object Table1: TTable
    DatabaseName = 'DBDEMOS'
    TableName = 'COUNTRY.DB'
    Left = 16
    Top = 8
    object Table1Name: TStringField
      DisplayWidth = 17
      FieldName = 'Name'
      Visible = False
      Size = 24
    end
    object Table1Capital: TStringField
      DisplayWidth = 18
      FieldName = 'Capital'
      Size = 24
    end
    object Table1Continent: TStringField
      DisplayWidth = 18
      FieldName = 'Continent'
      Size = 24
    end
    object Table1Area: TFloatField
      DisplayWidth = 12
      FieldName = 'Area'
    end
    object Table1Population: TFloatField
      DisplayWidth = 12
      FieldName = 'Population'
    end
  end
end