Solved

Combobox of field values

Posted on 2000-02-27
10
325 Views
Last Modified: 2010-04-04
i have got some code that can display the all the values of a particular field in a combobox (see below).
What I need to do is when one of these values (records) is selected i need to show four other fields from this table in another Combobox. The idea is that the user selects show name and the dates are displayed in another combobox, (each date is a seperate field>

procedure TNewBook.FormCreate(Sender: TObject);
var
i:integer;
begin
  table1.Open;
  table2.open;
//fill combo box with field values
for i:=0 to table1.fieldcount -1 do
  Begin
    ComboBox1.items.add(table1.fieldbyname('show name').value);
    table1.next;  
  end;        
end;                                                ]


0
Comment
Question by:SRP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:ahalya
ID: 2563723
Just for youer info:
Both of the following codes work fine on my machine.  They both fill up the combobox. (in D4 & D2 -for D2 change Fields.Count to fieldCount)

repeat
   ComboBox1.items.add(table1.FieldByName('Last Name').value);
   table1.next;
until (Table1.eof) or (ComboBox1.Items.Count > 10);

and ...

for i := 0 to Table1.Fields.Count -1 do
   ComboBox1.items.add(table1.Fields[i].FieldName);
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2563727
just noted a problem in your code:

FieldCount Gives a list of all Fields in the Table.  So when you do
for i := 0 to Table1.FieldCount -1 then your are looping for all the Fields in the table. (My seconds Case).

But to fill the combobox with values of one specific field use code similar to my first case.

Hope it helps.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2563858
hi srp,

i would do it with a query like

select distinct TheField from TheTable [order by TheField [Desc]]
[] means optional
- selects all Values of TheField and discards duplicates [maybe sorted]

u can now use it with this code:

query1.Open;
query1.First;
while not query1.eof do
begin
  ComboBox1.Items.Add(query1.FieldByName('TheField').AsString);
  query1.Next;
end;
query1.Close;

meikl
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
kiky earned 75 total points
ID: 2563927
Hi SRP,
If you want to display all the values of a particular field of table in a combobox, I think the code should be like this :
=====================================
for i:= 1 to table1.recordcount do
begin
   ComboBox1.items.add(table1.fieldbyname('Show_Name').asstring);
   table1.next;
end;
=====================================
I use table1.recordcount because recordcount specifies the number of records in a table, but fieldcount specifies the number of columns in a table.

Then, if you want to display the other field values in another ComboBox when the record is selected, you can use OnChange event in ComboBox1. I think you can write this code for the event :
=====================================
ComboBox2.Clear;
for i := 0 to table1.FieldCount-1 do
  if table1.fields[i].displayname <> 'Show_Name' then             ComboBox2.items.add(table1.fieldbyname
             (table1.Fields[i].DisplayName).asstring);
=====================================
I hope these code work for you..:)
Ratna
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2563943
to kiky,

the use of recordcount is risky in a multiuser-enviroment.

better use
while not table1.eof do . . .

meikl

0
 

Author Comment

by:SRP
ID: 2564364
Kiky - this code does the trick but has one problem it does not change the field values according to the 'Show Name' selected.

I have used:
ComboBox2.Clear;
for i := 0 to table1.FieldCount-1 do
  if table1.fields[i].displayname <> 'Show_Name' then             ComboBox2.items.add(table1.fieldbyname
             (table1.Fields[i].DisplayName).asstring);

But the fields it shows remain with the same values, what changes them when the show name is changed so the values of that same record are shown?
0
 

Author Comment

by:SRP
ID: 2566357
Adjusted points to 75
0
 

Expert Comment

by:kiky
ID: 2567336
Hi again SRP :)
I think the problem is because you don't set the pointer table to the record shown in ComboBox1, so the values remain the same in ComboBox2.
If the table is indexed by this particular field (Show_Name), you can add
these code above the line of 'ComboBox2.Clear;'
=====================================
table1.setkey;
table1.fieldbyname('CustomerID').asstring := combobox1.text;
table1.gotokey;
=====================================
But, if the table is indexed by other fields, then you should use a query and
change the code to these :
=====================================
s:='select * from TableName where Show_Name = :Name ';
Query1.close;
Query1.SQL.Clear;
Query1.SQL.Add(s);
Query1.prepare;
Query1.Params[0].AsString := ComboBox1.Text;
Query1.open;
ComboBox2.Clear;
for i := 0 to Query1.FieldCount-1 do
  if Query1.Fields[i].DisplayName <> 'Show_Name' then ComboBox2.items.add(Query1.fieldbyname(Query1.Fields[i].DisplayName).asstring);
=====================================
In a multiuser-environment, I think you'd better use a query.

Hope my comment solve your problem...:)
Kiky
0
 

Author Comment

by:SRP
ID: 2568619
Since I'm trying to keep it simple and I don't know much SQL I used
table1.setkey;
table1.fieldbyname('CustomerID').asstring := combobox1.text;
table1.gotokey;
It works v. well.

Thanks very much for that.
0
 

Author Comment

by:SRP
ID: 2568628
(Please also see comment by Kiky on Tuesday, February 29 2000 - 02:13AM GMT )

Kiky - thanks, great anwser works fine.


Cheers
SAM
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question