Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lookup Combobox in Delphi

Status
Not open for further replies.

Oppenhiemer

Programmer
Jun 28, 2001
315
0
0
GB
I have asked for help on this subject before and so far cannot solve the problem Im having. The problem revolves around the use of DropDownCombo boxes and how to use them.

What I want to do is not complicated at all really - and anyone who has used databases Such as Microsoft Access will be familiar with what I want to do.

For those interested - here is a link to the previous post :

thread102-292547 example I will give is theoretical - but the priniciple is valid. Lets say I have a table called Orders, when filling out the orders form - I would like to present the user with a drop-down list of values for the "CustCode".

Now in Access - this would be simplicity itself, you would simply insert a dropdown list into the form and set the "RowSource" properties to the appropriate lookup table (or query.)

In Delphi however - it is approching the most complicated thing I have ever tried to do! Perhaps I am trying to use the wrong component - but I am quite frankly at a loss.

Here is what I would intuitively do in the case I just described :

1. Insert a DropdownComboBox
2. Set the "DataSource" to point to the "Orders" Table.
3. The DataField to point the "CustNo" field in Orders Table.
Now comes the lookup part..
4. The listSource points to the Customers table
5. The ListField is the "CustNo" field in the customers table
(and this is the nightmare!)
6. The KeyField points to the CustNo (If I dont set this - I get squat appearing.)

The problem (as far as I can see) is that the KeyField is somehow supposed to link the Orders table with the Customers table ? Now as you may be creating a new order - no corresponding relationship can exist in the customers table (there may be no data in the orders table to link the 2 entities.)

Do I need to create a Lookup Field ? In in that case - how does this tie in with the scenario I just described.

As I say - this is my second post on this subject, and I would not be posting again were it not so important (giving users the utility to select from a pre-defined list of values in a database iq quite important in the whole User-Interface design I think!)

Anyway, I would really appreciate any solutions people can offer to this problem.

Cheers..

Opp.

 
If i got you right, Opp, then you are doing everything right. I just want to add: ListField can be set to any other field like CustName or whatever it's called. In your situation CustNo of ORDERS tables must reffer the field CustNo of the CUSTOMERS table (i hope you know this) so you won't be able to enter into ORDERS table a CustNo that doesn't exist in CUSTOMERS table.

--- markus
 
Hi Markus -

Yes I did realise about the Listfield (can be any number of fields seperated by Semicolons.) I also realise that I cannot select a Custno that does not already exist in the Customers table (as this would break referential rules anyway.)

But this makes me wonder what I am doing wrong - because the thing just wont work. Thanks for you input though Markus.

Opp.
 
Choose only listsource,listfield and keyfield(only).
This will work
 
Er.. dont use the DataSource and DataField ? Thanks cchitano - I know that this will work as I have tried it myself.

Unfortunately - it kid of defeats the whole thing Im trying to do. If I ommit the Datasource connections - I have other problems, in that I have to manually manage the control.

Say for example I put the control on a "Orders " form and the user clicks on an existing order to edit (view) it. If the controls are un-bound - they will not see the information (CustCode) underneath.

I did consider using it as an unbound control, but the problem is - that you cannot programatically set its value to reflect the data underneath (as far as Im aware.)

Plus - is this the extent of the controls use in Delphi ? How crap can that be. I mean - its got to be a useful feature right ? And Delphi cant provide it ??

Anyway, I do appreciate you tring to help. If anyone has any other ideas - they are very welcome.

Cheers..
Opp.
 
Oppenheimer,

You are on the right track as long as you use the DBLookupComboBox. For the DataSource use the datasource tied to the Orders table. For the DataField use Orders.CustNo.

Next go to the ListSource and assign the datasource tied to the Customers table. For ListField use Customers.CustNo. For the KeyField use Customers.CustNo.

This sounds exactly like what you are doing, and this will work without any additional "On Post" code. When you scroll through the records you should see the DBLookupComboBox populate with existing values. When you add a new record, the DBLookupComboBox will be blank until you populate it.

Are you saying it does not post correctly? Or are you saying you don't think it does because they don't seem to be linked on a new record?

Let me know exactly what it is doing on the database side if it is not working.

Cat

 
Hi Cat -
Yes - I have the thing configured just as you desscribed (which is the same as in my original post I believe.) What happens is this..

Firstly - the control does not reflect what is in the underlying field. So I drop a Box (shortened name ;-)) on the form and configure as prescribed. Firstly - the control will not show you the current value of the field (you would expect that on an existing record - the Box would show the existing contents of the underlying field - it does not.)

So the Box is blank for existing records. Secondly - when you try select a value from the drop-down - it allows you to do so. But does not reflect the changes in the BOx itself (again - its blank.) This is even though - the actual contents of the field have changed (I know this - because I placed a duplicate field on the form bound to the field in question.)

So in short, it works so far as it will display the correct values and let you choose from those. it DOES NOT reflect the selection made - or indeed - let you see the current contents of the field in question (in the case of an existing record.)

I probably have not explained things very well - so this may be helpful :


The top image shows the Box in drop-down mode and to the right a control bound to the same field to show changes. The image below this shows how a value has been selected - but not shown in the control itself (its blank!)

The last image sums up the problem - that unless i have 2 controls for the same field (one a drop down box - the other a regular field to show changes and underlying data) neither existing data nor new changes can be seen.

Btw - I am using Interbase and the TIBDataset component as the datasource for the underlying control. I am using a TIBQuery for the lokkup source.

Thanks for you interest - I hope someone has the answer ;-).

Cheers..

Opp.
 
Hi are you using a Datasource just for the look up or is it the datasource of another table?

Try using its own Table and datasource just for the lookup? Kind Regards, Paul Benn

**** Never Giveup, keep trying, the answer is out there!!! ****
 
Hi Paul -

The Datasource and DataField properties are bound to the "CustNo" field on the Orders table (query.) The LookupField,Keyfield etc refer (point) to another datasource (and table.)

Given I was unable to find an answer to the problem - I have took another approach to a solution. My approach involves doing everything programatically (filling COmboBox items from code based on results from TIBSQL components.)

I means I had to do a lot of trapping of Keyboard events etc.. But I think it a reasonable solution. Plus - I could not afford to spend more time on the problem. If these controls are so problamatic - I will simply avoid using them.

Thanks for your interest though..

Opp.

 
Opp-
I had the exact problem! I could get the descriptions in the box from Table1 and highlight the correct choice from Table2 BUT NOTHING EVER DISPLAYED!! I finally just had to use a listbox (which I don't really like - but at least it kinda works). Did you ever get this to work like you wanted? I'd love to know your solution!

Thanks,

leslie Leslie
landrews@metrocourt.state.nm.us
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top