home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0210_Adding fields at run-time.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-03-04  |  1.9 KB  |  85 lines

  1.  
  2. unit Unit1;
  3. { This program allows fields to be added on the fly.
  4. }
  5.  
  6. interface
  7.  
  8. uses
  9.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   DB, DBTables, Grids, DBGrids, StdCtrls;
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     Table1: TTable;
  15.     DataSource1: TDataSource;
  16.     DBGrid1: TDBGrid;
  17.     Button1: TButton;
  18.     Edit1: TEdit;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     Memo1: TMemo;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.   private
  25.     { Private declarations }
  26.   public
  27.     { Public declarations }
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.DFM}
  36.  
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. begin
  40.      Memo1.Clear;
  41.      if StrComp(PChar(Edit1.Text), PChar(''))= 0 then
  42.      begin
  43.           Memo1.Lines.Add('Invalid field name');
  44.           Exit;
  45.      end
  46.      else begin
  47.                with table1 do
  48.                begin
  49.                     Close;    // table must be closed !!!
  50.                     with FieldDefs do
  51.                     begin
  52.                          try
  53.                             Add(Edit1.Text, ftInteger, 0, False);
  54.                          Except On EDatabaseError do
  55.                                 begin
  56.                                      Memo1.Lines.Add('Error adding field ' +
  57. Edit1.Text + ' to table.');
  58.                                      Memo1.Lines.Add('Maybe ''' + Edit1.Text
  59. + ''' already exists as a field name');
  60.                                 end;
  61.                          end;
  62.                          CreateTable;
  63.                          Open;
  64.                     end;
  65.                end;
  66.      end;
  67. end;
  68.  
  69. procedure TForm1.FormCreate(Sender: TObject);
  70. begin
  71.      Table1.Open;
  72. end;
  73.  
  74. end.
  75. Ronan van Riet
  76.  
  77. Graaf Florishof 4
  78. 3632 BS Loenen a/d Vecht
  79. The Netherlands
  80. 0294-233563
  81.  
  82. vanriet@worldaccess.nl (private)
  83. 4921781@ibk.fnt.hvu.nl (Utrecht Polytechnic, the Netherlands)
  84.  
  85.