Solved

Records in TDBComboBox

Posted on 1998-03-19
10
362 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi all,

I'll evaluate both answers.

Thanks.....
0
 

Author Comment

by:sergio_2001
Comment Utility
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
Comment Utility
The state of the underlying datafield  is not readonly?
0
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

 

Expert Comment

by:bijoyn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The question is anwsered.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using FastMM4... should I enable MMX option? 9 68
Delphi OLE Error 8 82
Printing problem 2 72
Convert a string into a TDateTime 5 44
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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

6 Experts available now in Live!

Get 1:1 Help Now