Solved

Combobox of field values

Posted on 2000-02-27
10
311 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

21 Experts available now in Live!

Get 1:1 Help Now