在table或query的beforeinsert事件之前判断一下。

解决方案 »

  1.   

    TIga人您好,首先感谢你的提示,请给源代码好吗?如能够解决问题,马上给你加分!
      

  2.   

    在Onnewrecord的时候先把关键字的值填上,以后就不担心关键字的冲突问题了。
      

  3.   

    第一种方法
    编号:对应字段bianhaio; 姓名:对应xingming字段;
    dbedit1.datasource:=DataSource1;
    dbedit1.datafield:='bianhao';
    dbedit2.datasource:=DataSource1;
    dbedit2.datafield:='xingming';
    这样数据库的连接就建立好了。
    这里我们要求‘编号’为关键字段,不允许重复输入,为了控制输入的编号为唯一,我们在数据库变化时对Table1加入一个Beforepost事件,程序如下:
    procedure TForm1.Table1
    BeforePost(DataSet: TDataSet);
    begin
    with table2 do
    begin
    databasename:='c:\';
    tablename:='renshi.dbf';
    indexfieldnames:='bianhao';
    if not active then open; {判断数据库是否打开}
    Refresh; {刷新数据库}
    setkey; {设置数据库为搜索状态}
    fieldbyname('bianhao').asstring:=dbedit1.text;
    {设置输入的编号 字段内容为搜索的标准}
    gotokey;
    {移动到搜寻的记录上,完成搜索}
    if gotokey then
    {如果找到搜索的记录,则gotokey返回true}
    begin
    showmessage('输入的编号已存在!请重新输入');
    abort; {终止该操作}
    end;
    end;
    end;
      

  4.   

    第二种方法:
    在SQL2000中设计数据表时,在建立关键字时,在Create UNIQUE选项中,选中Index,选中Ignore duplicate key,这样当关键字重复时,会自动忽略根本不会存入数据库,所以也不会报错了