Function Get_item_list(tbname, fdname: String; alist: TStrings): Boolean;
Begin
alist.BeginUpdate;
alist.Clear;
Try
Try
With TADOQuery.Create(Nil) Do
Begin
Try
Close;
LockType := ltReadOnly;
Connection := Form1.ADOConnection1;
Sql.Clear;
SQL.Add(Format('select %s from %s', [fdname, tbname]));
Open;
While Not Eof Do
Begin
alist.Add(FieldByName(fdname).AsString);
Next;
End;
Close;
Finally
Free;
End;
End;
Except On E: Exception Do
Begin
Application.MessageBox(PChar(E.Message), 'msg', 64);
End;
End;
Finally
alist.EndUpdate;
End;
End;
Procedure TForm1.BitBtn2Click(Sender: TObject);
Begin
//ComboBox1.Style := csDropDownList;
Get_item_list('表名称', '字段名称', ComboBox1.Items);
End;
我一般都调用这个过程,比如initComb(ADOQuery1,'select * from User','Name',Combobox1);
//初始化下拉列表
procedure InitComb(Query:TADOQuery;SqlStr:String;Field:String;CombList:TComboBox);
var
i:integer;
begin
combList.Items.Clear;
With Query do begin
Close;
SQL.Clear;
SQL.Add(SqlStr);
Open;
if not isEmpty then begin
First;
for i:=0 to RecordCount-1 do begin
Comblist.Items.Add(FieldByName(Field).AsString);
Next;
end;
end;
end;
end;
使用DBComboBox控件。