locate这个函数发现用起来很麻烦啊。想用locate函数来判断数据库中
是否存在某一条记录时是一个很好的方法,其他的如根据返回记录数
recordcount也可以。当我数据库连的是access的时候,可以有返回值;当
改为sql server时,出了问题,查看返回值时,提示是:EVariantError 
at $797F981。因此,locate下面的语句就没有被执行到了。不知道哪位老大能提供点意见。
程序如下:
  {从数据库中找出用户名字为userslist.items[usersptr]}
  sqlstr:='select users_id from users where users_name='+''''+userslist.Items[userptr]+'''';
  with customerdata.Query1 do begin
       close;
       sql.Clear;
       unprepare;
       sql.Add(sqlstr);
       try
       open;
       except
       end;
       if active=true then begin
        {如果存在users这条记录}
        if locate('users_id',userslist.Items[userptr],[])=false  then
           result:=false;
       end; 

解决方案 »

  1.   

    因为locate返回的是longint,它能等于false 正确的用法, “...=-1”
      

  2.   

    function Locate(const KeyFields: string; const KeyValues: Variant; Options: TLocateOptions): Boolean; virtual;Locate 返回的是 Boolean 型的,EVariantError 错误就应该是 Locate 的参数有问题。
    是不是 KeyValues 参数不对?
      

  3.   

    这里的keyvalues是从TCheckListBox中的某一项直接读取的
      

  4.   

    CheckListBox1.Items.Strings[0];
    這是取回第一個子項的Caption.試試,不要說Locate 不好用啦。它沒什麼錯誤的。
      

  5.   

    Str := CheckListBox1.Items.Strings[CheckListBox1.ItemIndex];
     
    這個就返回當前的子項的Caption;
      

  6.   

    locate 不会有问题的
    你试试用具体的值看看
    locate('UserId','具体值',[])
      

  7.   

    if customerdata.Query1.isempty then
      result:=false
    else
      if customerdata.Query1.locate('users_id',userslist.Items[userptr],[])=false  then
               result:=false;
      end;
    end;