请问我的dbgrid连接了一个ADOTABLE,当我用ADOTABLE.append添加一条记录后(还未保存),光标焦点移动到其它记录上去的时候,它总是帮我自动保存了该新记录。本来也没问题,但我想在保存该记录前,先搜索一下数据库,看会不会索引重复,如索引重复,我想光标焦点还是定位到那条新记录上继续编辑,请问该如何解决?

解决方案 »

  1.   

    在AdoTable的BeforePost事件中检查索引是否重复,如:
    procedure AdoTable1BeforePost(DataSet: TDataSet);
    begin
      ...... 
      if 索引重复=true then
      begin
        Raise Exception.Create('此内容已经存在!');
      end;
    end;
      

  2.   

    按照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兄这个思路想办法
      

  3.   

    我的代码如下,但我用的是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. 出来,程序就退出来了。我是想要出现主键重复了,程序可以恢复焦点到编辑那条新记录的状态啊
      

  4.   

    啊!解决了,原来我是在delphi中运行程序才出错,谢谢 ourlin兄和zhangl_cn(不做和尚了!)了。