Tiny
Tiny

Reputation: 419

DELPHI Fill a listbox with the results of ADO Query

have been reading through all associated threads but no solution posted to help me with Delphi.

Quite simply, i have a MySQL table called Story and i want to extract specific fields from this - and hence populate list box.

From other posts, i've used the following...

adoqMenu.Close;
adoqMenu.SQL.Text := 'SELECT StoryID, Story Description, Completion Date FROM Story';
try
  adoqMenu.Open;
  ListBox1.Items.Clear;
  while not adoqMenu.Eof do
  begin
    ListBox1.Items.Add(adoqMenu.Fields[0].AsString);
    adoqMenu.Next;
  end;
finally
  adoqMenu.Close;
end;

This only gives me the first field...grr. Quite simply, how can i change this so that the fields stated in the SELECT clause appear in the listbox as is?

Thanks

Upvotes: 1

Views: 2925

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596206

You only see one field because you are only reading out one field (adoqMenu.Fields[0]). Simply read out the other fields as well:

adoqMenu.Close;
adoqMenu.SQL.Text := 'SELECT StoryID, Story Description, Completion Date FROM Story';
adoqMenu.Open;
try
  ListBox1.Items.Clear;

  while not adoqMenu.Eof do
  begin
    Value1 := adoqMenu.Fields[0].AsString;
    Value2 := adoqMenu.Fields[1].AsString;
    Value3 := adoqMenu.Fields[2].AsString;

    // use Values as needed.  Format the ListBox text however
    // you want to show all three values...
    ListBox1.Items.Add(...);

    adoqMenu.Next;
  end;
finally
  adoqMenu.Close;
end;

Depending on your actual needs (which you did not explain), a multi-column TListView in vsReport mode may be a better choice than a TListBox:

adoqMenu.Close;
adoqMenu.SQL.Text := 'SELECT StoryID, Story Description, Completion Date FROM Story';
adoqMenu.Open;
try
  ListView1.Items.Clear;

  while not adoqMenu.Eof do
  begin
    Item := ListView1.Items.Add;
    Item.Caption := adoqMenu.Fields[0].AsString;
    Item.SubItems.Add(adoqMenu.Fields[1].AsString);
    Item.SubItems.Add(adoqMenu.Fields[2].AsString);
    adoqMenu.Next;
  end;
finally
  adoqMenu.Close;
end;

Upvotes: 4

Related Questions