home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk15 / insquery.pak / MAIN.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1995-08-24  |  1.4 KB  |  61 lines

  1. { Demos the use of separate queries to do insertions and deletions. This
  2.   demo is "correct" but inefficient; it re-executes the query after insertions
  3.   and deletions. }
  4. unit Main;
  5.  
  6. interface
  7.  
  8. uses WinTypes, WinProcs, SysUtils, Classes, Graphics, Forms, Dialogs, Controls,
  9.   DB, DBGrids, StdCtrls, DBTables, Grids, ExtCtrls;
  10.  
  11. type
  12.   TMainForm = class(TForm)
  13.     GridQuery: TQuery;
  14.     DataSource1: TDataSource;
  15.     Grid: TDBGrid;
  16.     DeleteQuery: TQuery;
  17.     Panel1: TPanel;
  18.     Insert: TButton;
  19.     Delete: TButton;
  20.     procedure InsertClick(Sender: TObject);
  21.     procedure DeleteClick(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   MainForm: TMainForm;
  26.  
  27. implementation
  28.  
  29. uses InsForm;
  30.  
  31. {$R *.DFM}
  32.  
  33. procedure TMainForm.InsertClick(Sender: TObject);
  34. begin
  35.   InsertForm.Clear;
  36.   if InsertForm.ShowModal = mrOK then
  37.   begin
  38.     GridQuery.Close;        { re-execute query to pick up insert }
  39.     GridQuery.Open;
  40.   end;
  41. end;
  42.  
  43. procedure TMainForm.DeleteClick(Sender: TObject);
  44. var
  45.   Key: string;
  46. begin
  47.   Key := Grid.Fields[0].AsString;
  48.   if MessageDlg(Format('Delete "%S" from the Country table?', [Key]),
  49.     mtConfirmation, mbOKCancel, 0) = mrOK then
  50.   begin
  51.     DeleteQuery.Prepare;
  52.     DeleteQuery.Params[0].AsString := Key;
  53.     DeleteQuery.ExecSQL;
  54.     GridQuery.Close;        { re-execute query to eliminate deletion }
  55.     GridQuery.Open;
  56.   end;
  57. end;
  58.  
  59. end.
  60.  
  61.