我想不重复的将字段的值取出来,用下面的代码实验adotable1.first;
while not adotable1.eof do
begin
combobox1.Items.Add(adotable1['籍贯']);
adotable1.Next ;
end;会出现重复的值。有办法吗?

解决方案 »

  1.   

    用query,select distinct 籍贯 FROM 表
      

  2.   

    adotable1.sql.add(select distinct 籍贯 FROM 表);
    adotable1.open;
    adotable1.first;
    while not adotable1.eof do
    begin
    combobox1.Items.Add(trim(adotable1.fields[1].asstring));
    adotable1.Next ;
    end;
      

  3.   

    procedure TFrmGuDZC.refreshcombo(combobox:tcombobox;zdm:string);
    var dquery:tquery;
    begin
        dquery:=tquery.Create(self);
        dquery.active:=false;
        dquery.DatabaseName := vPsDbName;
        dquery.SQL.Clear;
        dquery.SQL.add('select distinct '+zdm+' from  JG_DangA_View');
        dFbTableToCom(dquery,combobox,zdm);
        dquery.active:=false;
        dquery.Free;
        dquery:=nil;
    end;
    function dFbTableToCom(dbx:tdataset;combo:tcombobox;field_name,keyz:string;var zfc:array of string;addstring:string=''):boolean;overload;
    var i,zs,len:integer;
    begin
      len:=length(zfc);
      i:=0;
      try
        if not (dbx.active) then dbx.active:=true;
        dbx.first;
        combo.items.clear;
        if length(addstring)>0 then combo.items.add(addstring);
        while not dbx.eof do
        begin
            combo.items.add(dbx.fieldbyname(field_name).asstring);
            if i<len then zfc[i]:=dbx.fieldbyname(keyz).asstring;
            i:=i+1;
            dbx.next;
        end;
        result:=true;
      except
        result:=false;
      end;
      dbx.active:=false;
    end;调用实例refreshcombo(combobox1,'籍贯');
      

  4.   

    'select 籍贯 from 表名 group by 籍贯
      

  5.   

    select distinct 籍贯 FROM 表名
      

  6.   

    adotable1.first;
    while not adotable1.eof do
    begin
    combobox1.Items.Add(adotable1['籍贯']);
    adotable1.Next ;
    end;如果你想给拘泥的代码进行修改,那么这样做!!!
    adotable1.first;
    while not adotable1.eof do
    begin
    XXX :=adotable1['籍贯'].AsString;
    If combobox1.Items.IndexOf( XXX ) = -1 Then
    combobox1.Items.Add(adotable1['籍贯'].AsString);
    adotable1.Next ;
    end;