Board index » delphi » IndexName and TDBLookupComboBox

IndexName and TDBLookupComboBox


2003-10-20 03:30:21 PM
delphi43
I have a Categories table that contains the following:
Cat Desc
01 Vegetables
02 Fruits
03 Dairy
...
On my form, I have a TDBLookupComboBox control that I want to use to
list the categories. In my main table, I have a field Cat that refers to the
category is the Categories table.
When I run my application, and I drop down the categories, they are sorted
in Cat order, which is normal. How do I sort them by Desc, which would make
more sense to the end user when the ListField is set to Desc? I tried
changin the index to Desc, however, the TDBLookupComboBox does not work
because there is not match for the Cat field from the main table anymore.
 
 

Re:IndexName and TDBLookupComboBox

The only way I know if to do this is to use something other than a
TDBLookupComboBox. I have used a TComboBox, loaded the descriptions into the
Items stringlist and used the Object property of the stringlist item to
store the "value." You update the actual field value in either the OnExit
event of the TComboBox or the OnBeforePost event of the table. Loading the
categories would look something like this in code:
tblCategory.Open; //use the index to order by description
while not tblCategory.EOF do
cbCategories.Items.AddObject(tblCategory.fieldbyname('Desc').AsString,
TObject(tblCategory.fieldbyname('Cat').AsString);
Setting the value of the field will look something like this:
tblMyTable.FieldByName('Cat').AsString :=
String(cbCategories.Items.Objects[cbCategories.ItemIndex]);
I don't guarantee that this code is exactly correct (I haven't done this
type of thing in quite awhile), but it should be enough to get you started.
-Dell
 

Re:IndexName and TDBLookupComboBox

I was hoping there was a way to use a data-aware control, but hey, you're
idea is cool. It is actually more than I expected. Thanks a lot!
"Dell Stinnett" <XXXX@XXXXX.COM>writes
Quote
The only way I know if to do this is to use something other than a
TDBLookupComboBox. I have used a TComboBox, loaded the descriptions into
the
Items stringlist and used the Object property of the stringlist item to
store the "value." You update the actual field value in either the OnExit
event of the TComboBox or the OnBeforePost event of the table. Loading
the
categories would look something like this in code:

tblCategory.Open; //use the index to order by description

while not tblCategory.EOF do

cbCategories.Items.AddObject(tblCategory.fieldbyname('Desc').AsString,
TObject(tblCategory.fieldbyname('Cat').AsString);



Setting the value of the field will look something like this:

tblMyTable.FieldByName('Cat').AsString :=
String(cbCategories.Items.Objects[cbCategories.ItemIndex]);

I don't guarantee that this code is exactly correct (I haven't done this
type of thing in quite awhile), but it should be enough to get you
started.

-Dell