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
Solved

Combobox of field values

Posted on 2000-02-27
10
318 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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