2、在單元Unit1的type部分作相應的說明及變量的定義:
type
TForm1 = class(TForm)
…
procedure FormCreate(Sender: TObject);
procedure Table1BeforeInsert(DataSet: TDataSet);
procedure Table1AfterInsert(DataSet: TDataSet);
procedure Table1PostError(DataSet: TDataSet; E: EDatabaseError; var Action: TDataAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
//以下為自定義過程與函數的說明
function MyRecordScatTo(DataSet0:TDataSet;var MyDynamic_Record0:Variant):boolean;
function MyRecordGatherFrom(DataSet0:TDataSet;var MyDynamic_Record0:Variant):boolean;
procedure My_TablePostError(DataSet: TDataSet; E: EDatabaseError;
var Action: TDataAction;sFIEldName:string);
private
{ Private declarations }
MyDynamic_Record:Variant; //定義動態數組
public
{ Public declarations }
end;
…
3、在表單的OnCreate事件中,加入Table1表的初始設置:
procedure TForm1.FormCreate(Sender: TObject);
var s:string;
begin
s:=ExtractFilePath(Application.ExeName);
Table1.DatabaseName:=s; //設置Table1的源數據路徑
Table1.Open;
end; //初始設置
分別在表Table1的BeforeInsert,AfterInsert,OnPostError事件中加入如下代碼:
procedure TForm1.Table1BeforeInsert(DataSet: TDataSet);
begin
if CheckBox1.Checked //使用"攜帶"功能時,調用"獲取"
then
if not MyRecordScatTo(Table1,MyDynamic_Record) //出錯時,取消"攜帶"
then CheckBox1.Checked:=false;
procedure TForm1.Table1AfterInsert(DataSet: TDataSet);
begin
if CheckBox1.Checked //使用"攜帶"功能時,調用"賦值"
then MyRecordGatherFrom(Table1,MyDynamic_Record);
end; //添加記錄後的數據處理
procedure TForm1.Table1PostError(DataSet: TDataSet; E: EDatabaseError;
var Action: TDataAction);
begin
//調用通用表數據處理模塊:主關鍵字'編號'
My_TablePostError(DataSet,E,Action,'編號');
end;
分別在Button1、Button2的OnClick事件中加入如下代碼:
procedure TForm1.Button1Click(Sender: TObject);
begin
Table1.Insert;
if DBGrid1.CanFocus
then DBGrid1.SetFocus
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Table1.Refresh;
Table1.Close;
Close;
end; //關閉表單