在AdoTable的BeforePost事件中检查索引是否重复,如: procedure AdoTable1BeforePost(DataSet: TDataSet); begin ...... if 索引重复=true then begin Raise Exception.Create('此内容已经存在!'); end; end;
按照1楼思路: procedure TForm1.ADOQuery1BeforePost(DataSet: TDataSet); var adoquery2:TADOQuery; begin adoquery2:=TADOQuery(self); adoquery2.Connection:=adoconnection1; adoquery2.SQL.Text:='select count(*)as a from stuffinfo where st_id='+''''+dataset.fieldbyname('st_id').AsString+''''; adoquery2.Open; if adoquery2.FieldByName('a').AsInteger<>0 then begin raise Exception.Create('重复'); end;简单测试了一下,没有问题,楼主可以按照ourlin兄这个思路想办法
我的代码如下,但我用的是CXGRID控件。且设置了它为可以具有添加记录功能 procedure Trsjbform.ADODataSet1BeforePost(DataSet: TDataSet); begin adoquery1.Close; adoquery1.SQL.Clear; adoquery1.SQL.Text:='select * from InActService where 工号= ''' + trim(cview2dbcolumn.EditValue) + ''''; adoquery1.Open; if not adoquery1.Eof then begin rase exception.create('已有此记录'); end; end;这样写弹个project project1.exe raised exception class exception with message'已有此记录'process stopped.use step or run to continue. 出来,程序就退出来了。我是想要出现主键重复了,程序可以恢复焦点到编辑那条新记录的状态啊
procedure AdoTable1BeforePost(DataSet: TDataSet);
begin
......
if 索引重复=true then
begin
Raise Exception.Create('此内容已经存在!');
end;
end;
procedure TForm1.ADOQuery1BeforePost(DataSet: TDataSet);
var
adoquery2:TADOQuery;
begin
adoquery2:=TADOQuery(self);
adoquery2.Connection:=adoconnection1;
adoquery2.SQL.Text:='select count(*)as a from stuffinfo where st_id='+''''+dataset.fieldbyname('st_id').AsString+'''';
adoquery2.Open;
if adoquery2.FieldByName('a').AsInteger<>0 then
begin
raise Exception.Create('重复');
end;简单测试了一下,没有问题,楼主可以按照ourlin兄这个思路想办法
procedure Trsjbform.ADODataSet1BeforePost(DataSet: TDataSet);
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Text:='select * from InActService where 工号= ''' + trim(cview2dbcolumn.EditValue) + '''';
adoquery1.Open;
if not adoquery1.Eof then
begin
rase exception.create('已有此记录');
end;
end;这样写弹个project project1.exe raised exception class exception with message'已有此记录'process stopped.use step or run to continue. 出来,程序就退出来了。我是想要出现主键重复了,程序可以恢复焦点到编辑那条新记录的状态啊