例14.7 在這個例子中,我們在CustNo.DB表中插入和刪除記錄,都是在程序中完成這類操作的,而不再是使用DBD或數據浏覽部件完成。
插入/刪除記錄
程序清單:
unit tt;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
StdCtrls, Forms, DBCtrls, DB, DBGrids, Buttons, DBTables, Grids,
ExtCtrls,Mask,Dialogs;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DBNavigator: TDBNavigator;
Panel1: TPanel;
DataSource1: TDataSource;
Panel2: TPanel;
customerTable: TTable;
BitBtn1: TBitBtn;
Label1: TLabel;
Label2: TLabel;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
CustNoEdit: TEdit;
CompEdit: TEdit;
procedure FormCreate(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure ForMactivate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
customerTable.Open;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
If (Length(CustNoEdit.text)=0)and
(Length(CompEdit.text)=0)
then
MessageDlg('沒有輸入新記錄的字段值!',mtError,[mbCancel],0)
else
with customerTable do
begin
IndexFIEldNames:='CustNo';
If FindKey([CustNoEdit.text]) then
MessageDlg('已經存在這條記錄!',mtError,[mbCancel],0)
else
InsertRecord([StrToInt(CustNoEdit.text),CompEdit.text,nil]);
CustNoEdit.text:=' ';
CompEdit.text:=' ';
end;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
If (Length(CustNoEdit.text)=0)and
(Length(CompEdit.text)=0)
then
MessageDlg('沒有輸入刪除的記錄的字段值!',mtError,[mbCancel],0)
else
with customerTable do
begin
IndexFIEldNames:='CustNo';
If FindKey([CustNoEdit.text]) then
begin
If MessageDlg('你確定要刪除這條記錄嗎?',mtConfirmation,
[mbYes,mbno],0)=mrYes then Delete;
end
else
MessageDlg('沒有你要刪除的記錄!',mtError,[mbCancel],0);
CustNoEdit.text:=' ';
CompEdit.text:=' ';
end;
end;
procedure TForm1.ForMactivate(Sender: TObject);
begin
CustNoEdit.setfocus;
end;
end.