Solved

Records in TDBComboBox

Posted on 1998-03-19
10
373 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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
 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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