📄 tconedtunit.pas
字号:
////////////////////////////////////////////////////////////////// 模板名称:通用添加模块(T010) //// 模板功能;在继承B000模板的基础上,完成数据的添加与修改功能 //// 模板接口:继承 //// 输入参数:tag=0 表示添加,tag=1 表示修改 //// 输出参数:无 //// 编写人: 陈宇 //// 编写时间:2001年12月20日 //// 备注: 使用此模板需要重载三个函数 //// GotoDefaultRecord()virtual: //// SaveOtherData():virtual; //// LoadOtherData():virtual; //////////////////////////////////////////////////////////////////unit TConEdtUnit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, TbasUnit, StdCtrls, DB, ADODB, ImgList, ComCtrls, ToolWin, Buttons;type TTConEdt = class(TTBas) DtObject: TDataSource; AQeyObject: TADOQuery; CoolBar1: TCoolBar; SBExit: TSpeedButton; SBSave: TSpeedButton; SBAbort: TSpeedButton; procedure FormShow(Sender: TObject); procedure SBExitClick(Sender: TObject); procedure SBSaveClick(Sender: TObject); procedure SBAbortClick(Sender: TObject); private { Private declarations } protected procedure GotoDefaultRecord();virtual; // 通过主键数组定位当前待修改记录 function checkOtherData():boolean;virtual; //校验不能自动校验的数据 procedure SaveOtherData();virtual; // 保存不能自动保存的数据 procedure LoadOtherData();virtual; // 装载不能自动装载的数据 public { Public declarations } FtstrPrimary:TStrings;//主键名数组 FtstrKeyInfo:TStrings; // 主键对应的值,以偶对的方式存入, end;var TConEdt: TTConEdt;implementationuses DataLinkUnit,GlobalParaUnit;{$R *.dfm}procedure TTConEdt.GotoDefaultRecord();var I:integer; strName:string; varKeyValue:Variant; strPosion:string; test1,test2,test3:string;begin for I:=0 to FtstrPrimary.Count -1 do begin strName:= strName+strPosion+FtstrPrimary[I]; strPosion:=';'; end; case FtstrPrimary.Count of 1:varKeyValue := FtstrKeyInfo[0]; 2:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1]]); 3:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2]]); 4:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2], FtstrKeyInfo[3]]); 5:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2], FtstrKeyInfo[3], FtstrKeyInfo[4]]); 6:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2], FtstrKeyInfo[3], FtstrKeyInfo[4], FtstrKeyInfo[5]]); 7:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2], FtstrKeyInfo[3], FtstrKeyInfo[4], FtstrKeyInfo[5], FtstrKeyInfo[6]]); 8:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2], FtstrKeyInfo[3], FtstrKeyInfo[4], FtstrKeyInfo[5], FtstrKeyInfo[6], FtstrKeyInfo[7]]); 9:varKeyValue := Vararrayof([FtstrKeyInfo[0], FtstrKeyInfo[1], FtstrKeyInfo[2], FtstrKeyInfo[3], FtstrKeyInfo[4], FtstrKeyInfo[5], FtstrKeyInfo[6], FtstrKeyInfo[7], FtstrKeyInfo[8]]); end; AQeyObject.Locate(strName,varKeyValue,[]);end;procedure TTConEdt.SaveOtherData();beginend;procedure TTConEdt.LoadOtherData();beginend;procedure TTConEdt.FormShow(Sender: TObject);begin inherited; // 激活数据集 AQeyObject.Open; case tag of 0: begin Self.Caption:='修改'; GotoDefaultRecord; AQeyObject.Edit; LoadOtherData; end; 1: begin Self.Caption:='添加'; AQeyObject.Append; LoadOtherData; end; end;end;procedure TTConEdt.SBExitClick(Sender: TObject);begin inherited; close;end;procedure TTConEdt.SBSaveClick(Sender: TObject);begin inherited; // 保存数据 try if not checkOtherData then Exit; SaveOtherData; AQeyObject.Post; except On E:Exception do begin if (Pos('Key violation',E.Message)<>0) or (Pos('重复的值',E.Message)<>0) then begin Application.MessageBox('已存在,请检查输入内容','错误框',mb_iconstop); Exit; end else if (Pos('Dataset not in edit or insert mode',E.Message)<>0) or (Pos('不在修改状态或增加状态',E.Message)<>0) then begin Application.MessageBox('数据不在修改状态或增加状态','错误框',mb_iconstop); Exit; end Else if (Pos('must have a value',E.Message)<>0) or (Pos('字段不能为空',E.Message)<>0) then begin Application.MessageBox('其中有些字段不能为空','错误框',mb_iconstop); Exit; end Else begin Application.MessageBox(PChar(E.Message),'错误框',MB_ICONStop); Exit; end; end; //Application.MessageBox(ERR_POST,PROMPT_ERROR,MB_OK+MB_DefButton1+MB_IconInformation+MB_ApplModal); //Exit; end; // 默认的新操作 try case tag of 0:begin // 修改 AQeyObject.Edit; end; 1:begin // 添加 AQeyObject.Append; LoadOtherData;//? end; end; except //Application.MessageBox(ERR_DATABASE,PROMPT_ERROR,MB_OK+MB_DefButton1+MB_IconInformation+MB_ApplModal); //except On E:Exception do begin if Pos('Key violation',E.Message)<>0 then begin Application.MessageBox('主键重复','错误框',mb_iconstop); Exit; end else if Pos('Dataset not in edit or insert mode',E.Message)<>0 then begin Application.MessageBox('数据不在修改状态或增加状态','错误框',mb_iconstop); Exit; end Else if Pos('must have a value',E.Message)<>0 then begin Application.MessageBox('其中有些字段不能为空','错误框',mb_iconstop); Exit; end Else if Pos('Dataset not in edit or insert mode',E.Message)<>0 then begin Application.MessageBox('数据不在修改状态或增加状态','错误框',mb_iconstop); Exit; end else begin Application.MessageBox(PChar(E.Message),'错误框',MB_ICONStop); Exit; end; end; end;end;procedure TTConEdt.SBAbortClick(Sender: TObject);begin inherited; try // 取消对数据的修改 AQeyObject.Cancel; case tag of 0: // 修改 AQeyObject.Edit; 1: // 添加 AQeyObject.Append; end; except //Application.MessageBox(ERR_DATABASE,'提示',MB_OK+MB_DefButton1+MB_IconInformation+MB_ApplModal); On E:Exception do begin if Pos('Key violation',E.Message)<>0 then begin Application.MessageBox('主键重复','错误框',mb_iconstop); Exit; end else if Pos('Dataset not in edit or insert mode',E.Message)<>0 then begin Application.MessageBox('数据不在修改状态或增加状态','错误框',mb_iconstop); Exit; end Else if Pos('must have a value',E.Message)<>0 then begin Application.MessageBox('其中有些字段不能为空','错误框',mb_iconstop); Exit; end Else if Pos('Dataset not in edit or insert mode',E.Message)<>0 then begin Application.MessageBox('数据不在修改状态或增加状态','错误框',mb_iconstop); Exit; end else begin Application.MessageBox(PChar(E.Message),'错误框',MB_ICONStop); Exit; end; end; end;end;function TTConEdt.checkOtherData: boolean;begin
result:=true;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -