Solved

Combobox of field values

Posted on 2000-02-27
10
314 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to draw animated progressbar in Tvirtualstringtree 5 117
ddeman not working in activex 3 93
delphi prevent click fast 2 191
Convert a string into a TDateTime 5 50
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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now