if (RecordCount>=2) or (RecordCount=1) and (fieldvalues['RS_id']<>datasource1.DataSet.FieldValues['RS_id']) then
    begin
      showmessage('工号有重复,请更换工号!');
      dbediteh1.SetFocus;
      abort;
      exit;
    end;
1.这是做了一个临时的ADO访问数据库数据集,当输入一个ADO访问控件时有重复工号就会报对话框出来,但是if那几个语句我想不明白,为什么在这个情况下就会重复。procedure TFrmPersonEdit.DBComboBoxEh3Change(Sender: TObject);
begin
  inherited;
  if not (datasource1.DataSet.State in [dsInsert,dsEdit]) then exit;
  if dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.text)<>-1 then
    if dbcomboboxeh4.Items.Count>dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.Text) then
      dbcomboboxeh4.Text:=dbcomboboxeh4.Items[dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.Text)];
end;
2.这是一个当改变一个COMBOBOXEH的ITEMS时另一个也跟着改变.但也是if的那几个语句我想不明白。注:IF语句用法我知道,我想知道为什么在这种情况下加每一句IF语句中的作用是什么?

解决方案 »

  1.   

    第一问:
    (RecordCount>=2) or (RecordCount=1)
    如何不写成 RecordCount>=1)呢?
      

  2.   

    也许你想表达的是:
    if (RecordCount>=2) or
     ( (RecordCount=1) and (fieldvalues['RS_id']<>datasource1.DataSet.FieldValues['RS_id']) )  then //注意多加了一对括号 if not (datasource1.DataSet.State in [dsInsert,dsEdit]) then exit;
     表示如果这个数据集处于编辑或者插入状态,则不能操作,退出子程序;if dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.text)<>-1 表示dbcomboboxeh3的文本在dbcomboboxeh3的下拉列表中,因为你可能用键盘输入改变了dbcomboboxeh3.text,而它不是一个有效值,
    dbcomboboxeh4.Items.Count>dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.Text)
    保证下标不越界,如你定义一个:
    MyValue:array[1..10] of integer;
    你用MyValue[13]:=0;肯定会出错,
    所以有这种写法  if (idx>0) and (idx<=10) then MyValue[idx]:=100;
      

  3.   

    if (RecordCount>=2) or (RecordCount=1) and (fieldvalues['RS_id']<>datasource1.DataSet.FieldValues['RS_id']) then
        应为
    if ((RecordCount>=2) or (RecordCount=1)) and (fieldvalues['RS_id']<>datasource1.DataSet.FieldValues['RS_id']) then
    -----------------------------------------------------------------------------
    if dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.text)<>-1 then
        if dbcomboboxeh4.Items.Count>dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.Text) then
          dbcomboboxeh4.Text:=dbcomboboxeh4.Items[dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.Text)];
    中    if dbcomboboxeh4.Items.Count>dbcomboboxeh3.Items.IndexOf(dbcomboboxeh3.Text) then
          有问题    
      

  4.   

    To rong12345(rong12345) :if ((RecordCount>=2) or (RecordCount=1)) 
    你还这样写呀?不就等价于if (RecordCount>=1)吗?
      

  5.   

    在这此时,我先用一个表格已经装入了同样的数据,双击其才打开这些对话框的,当此对话框一建立,COMBOBOX的ITEMS就已经自动加入了项目值了,它还要这几个IF语句作什么用?