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; //关闭表单 |