Here is how to do it when the field is not a string: (In this case, it is a date field.)
var
QDate: TTable;
begin
listbox1.Clear;
QDate := TTable.create(application);
with QDate do begin
try
databasename := 'DBDEMOS';
TableName := 'orders.db';
open;
First;
Repeat
listbox1.Items.Add(FieldByName('SaleDate').AsString);
Next;
until EOF;
Close;
finally
free;
end;
end;
end;
Here's one way of populating a list box with your fields ID and Name:
with MyQuery do
begin
{ this is to prevent flicker in any controls using this dataset }
DisableControls;
try
First;
while not EOF do
begin
{ Here's the actual statement you
asked for... }
with MyListBox.Items do
Objects[ Add(
FieldByName('Name').AsString ) ] :=
TObject( PChar( FieldByName('ID').AsInteger ) );
Next;
end;
finally
EnableControls;
end;
end;
And here's how you can extract the ID from the currently selected item in the listbox:
with MyListBox do
MyInteger := Longint( Items.Objects[ ItemIndex ]
);