Solved

Combobox of field values

Posted on 2000-02-27
10
316 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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