Solved

Records in TDBComboBox

Posted on 1998-03-19
10
365 Views
Last Modified: 2010-05-18
I want see all records of one field using TDBComboBox.
How can I do that?

Thanks....
0
Comment
Question by:sergio_2001
  • 4
  • 4
  • 2
10 Comments
 

Expert Comment

by:bijoyn
ID: 1360632
Hi There,

If I have understood your question right then I am giving u a working e.g. In the e.g I have a TDBcomboBox which is linked to a particular Datasource. There is another datasource fropped to access all the records from the desired table. I have defined one query to access the desired field from the desired table. On the dropdown event of the the Combobox I am adding each value to the ComboBox. U can do even on formshow or some other suitable place. The only thing u will have to check is that It should execute the code only once in the form. Right now it will execute everytime u dropdown. By setting a flag u can easily achieve it. I hope u find the solution useful.

---------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, DBCtrls, DB, DBTables;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Query1: TQuery;
    DBComboBox1: TDBComboBox;
    DataSource2: TDataSource;
    Query2: TQuery;
    procedure DBComboBox1DropDown(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBComboBox1DropDown(Sender: TObject);
begin
   Query1.First;
   while not(Query1.Eof) do
   begin
      DbComboBox1.Items.Add(Query1.Fields[0].AsString);
      Query1.Next;
   end;
end;

end.

-------------------------------------------------
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360633
Hi sergio_2001,
Yo Bijoyn!

If you don't have enough information with Bijoyn's example, then you can read this :

Filling dblistboxs and dbcomboboxs

Most of Delphi's data aware components will  populate
themselves after they are wired up to a open dataset.  However
DbListboxs and DbComboboxs do not display this characteristic.  
These two components are not for displaying your datasets, but
filling them.  Use of these components is straight forward.  
When you update your table, the value of the DbListbox or
DbCombobox will be posted in the appropriate field.

Filling the DbCombobox or DbListbox the same as filling normal
comboboxs or listboxes.  The lines of text in a listbox or
combobox are really a tstring list.  The "Items" property of
the given component holds this list.  Use the "Add" method for
adding items to a tstring. If you want to use data  types other
than strings they must be converted at run time. If your list
has a blank line at the end, consider setting the
"IntegralHeight" property to True.


Filling a DbListbox with 4 lines programmatically might look
similar to this:

     DbListbox1.items.add('line one');
     DbListbox1.items.add('line two');
     DbListbox1.items.add('line three');
     DbListbox1.items.add('line four');

Filling a DbListbox at design time requires using the object
inspector.  By double clicking on the components "Items"
property, you can bring up the "String List Editor" and input
the desired rows.

Unfortunately, if a combobox is filled this way, there is not
default value.  Setting a DbComboboxs "text" property will
achieve this result.  (the "text" property is not available in
the object inspector, so it must be set programmatically).  
Setting the default value to the first value in the
DbCombobox's list looks like this:        

DbCombobox1.text := DbCombobox1.items[0];

Often it is useful to fill a DBListBox from a dataset.  This
can be done using loop:

procedure TForm1.FormCreate(Sender: TObject);
begin
  with table2 do begin
    open;
    while not EOF do
    begin
      DBlistbox1.items.add(FieldByName('name').AsString);
      next;
    end;
  end;
end;

Have a nice day,
Regards Zif.
0
 

Author Comment

by:sergio_2001
ID: 1360634
Hi all,

I'll evaluate both answers.

Thanks.....
0
 

Author Comment

by:sergio_2001
ID: 1360635
Hi all,

All the answers solve. But there are two problems:
- When I open the dbcombobox there are um blank space in the first item(0).
- I can't select one other item than the current item.

Points increased.

Thanks....

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360636
The state of the underlying datafield  is not readonly?
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.

 

Expert Comment

by:bijoyn
ID: 1360637
Hi Sergio, Yo Zifnab,

If the first record in the combobox is showing blanks then please check if the records in the source table is correct. As for the selection the dataset to which your ComboBox points to, must be edtiable ( ReadOnly = False as Zif as put in the above comment ).

Bijoy
0
 

Author Comment

by:sergio_2001
ID: 1360638
Hi bijoyn , Zifnab,

- The dataset of records looks good...
- I have the readonly property set to false...

but doesn't works...

Please send your comments...

Thanks...
0
 

Expert Comment

by:bijoyn
ID: 1360639
Hi Sergio,

I think I know what the problem is. Firstly go to the object inspector for your DBComboBox, Click on the items property, U might find a blank line in it. If its there then thats what results in the first blank record in the comboBox. Secondly check the dataset associated with your DbCombobox. It might have its RequestLive property set to false, make it true. If u do both of these things then ur problem will be solved.

Cheers,

Bijoy
0
 

Author Comment

by:sergio_2001
ID: 1360640
Hi all,

Bijoy....

- You are right... I deleted the first item from items property in the object inspector and it works fine....

- About the dbcombobox.... I'm using um ttable dataset... so I don't have the RequestLive property.... I'm using ReadOnly := false... but I can't select and change the record yet...

- But it's good at this moment... send-me the answer....

Thanks.....
0
 

Accepted Solution

by:
bijoyn earned 20 total points
ID: 1360641
The question is anwsered.
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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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

18 Experts available now in Live!

Get 1:1 Help Now