views:

81

answers:

3

So I'm messing around with a new project in Delphi 2009 and the default components that can be dropped onto a form for accessing data consist of a SQLConnection, DataSource and SQLQuery. If I add a simple select to the query component, say:

select name from customers

and then drop a DBComboBox on the form and link it up with the DataSource I get a single record in the combo box. After using Google for half and hour to figure out what I was doing wrong it looks like you have to manually add some code to your project which loops through the dataset and adds all the records to the drop down box. Something like:

while not SQLQuery.eof do
begin
  DBComboBox.items.add(SQLQuery.fieldbyname('name').asstring);
  SQLQuery.next;
end; 

And that actually sort of works, but then you get a list in the drop down which you can't actually select anything from. Regardless of the result though I'm wondering why would you even use a DBComboBox if you have to manually add the result of your query to it? Seems to me that if it doesn't automatically populate the db combo box with the result of the query then we might as well be using a non-data-aware component like tcombobox.

I guess what I'm asking is why does it work this way? Isn't the purpose of data aware drag-and-drop controls to minimize the amount of actual written code and speed development? Is there a method that I'm missing that is supposed to make this easier?

+4  A: 

A TDBComboBox doesn't get its list of values from the database; it gets its current value from the database. Link it to a field in your dataset, and when you change the active record, the combo box's current value will change. Change the combo box's current value, and the corresponding field's value will change.

If you want to get the list of values from the database as well, then use a TDBLookupComboBox.

This is all covered in the help:

Rob Kennedy
Thanks Rob, that clarifies it. Totally unrelated, but my help files somehow got annihilated when I installed MS Visual Studio for another project (in c#) .. haven't figured out how to get my Delphi help files back yet. But that's a discussion for another thread.
Rafe
+2  A: 

I think you want the TDBLookupComboBox because that allows you to lookup from a list of items where the list comes from a dataset.

In the TDBComboBox, the list is just a TStrings manually filled with data.

--jeroen

Jeroen Pluimers
A: 

DbCombox is a dbaware version of the standard combobox component.

net_roamer