Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi 4 - Populating a ListBox with the results of a Query.Open

Posted on 2006-10-27
8
Medium Priority
?
689 Views
Last Modified: 2010-04-05
I have the below code:

QuerySelect.SQL.Add('SELECT * FROM database_updater_tbl ORDER BY report_lastrun');
QuerySelect.Open;


I have several ListBoxes and I want each individual Listbox to be filled with the associated column in a SQL table.

So I want ListBoxDBNAME to just fill with database_updater_tbl/DBNAME

How would I go about this?
0
Comment
Question by:wbstech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 1500 total points
ID: 17817807
Use a DBListbox, set the ListSource to the DataSource that is pointing to the query and assign the lookup fieldname to the ListField.
0
 
LVL 4

Author Comment

by:wbstech
ID: 17817880
Could you give me a bit more detail on how to do that?

I got lost at step 1, I see no ListSource property for my DBListbox.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 17818027
Oops. I meant: DBLookupListbox. :-) My mistake...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17818044
I think Workshop Alex means a TDBLookupListBox instead of a normal TListBox.
Then set following properties :
   - ListSource = DataSource, linked to your Table/Query
   - ListField = Field you want to be displayed.
   - KeyField = same as ListField (or better : the Primary Key-Field).

Best regards,

The Mayor.
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17818048
Again, too slow ;-)

Btw the points should go to WorkShop Alex, since he gave you the exact anwser.
I just added some extra info of setting the properties.
0
 
LVL 4

Author Comment

by:wbstech
ID: 17818077
I did it a different way in the end, using just a ListBox as I was trying before.

procedure TForm1.btnRefreshClick(Sender: TObject);
begin
QuerySelect.SQL.Add('SELECT * FROM database_updater_tbl');
QuerySelect.Open;

While not QuerySelect.Eof = True do
begin
QuerySelect.Next;
ListDatabase.Items.Add(QuerySelect.FieldByName('db_name').AsString);
ListUpdater.Items.Add(QuerySelect.FieldByName('db_updater').AsString);
ListLastRun.Items.Add(QuerySelect.FieldByName('report_lastrun').AsString);

end;

end;
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17818157
Why should you use code if it can be done with the simple use of an existing Component ?
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 17818171
Hm, I checked you code, but I guess you placed the Next-Method on the wrong place.
By starting your while-loop with the next, I think you skip the first record and goe directly to the second record in your dataset (query).


So, instead of :
While not QuerySelect.Eof = True do
begin
    QuerySelect.Next;
    ListDatabase.Items.Add(QuerySelect.FieldByName('db_name').AsString);
    ListUpdater.Items.Add(QuerySelect.FieldByName('db_updater').AsString);
    ListLastRun.Items.Add(QuerySelect.FieldByName('report_lastrun').AsString);
end;


better use :
While not QuerySelect.Eof = True do
begin
    ListDatabase.Items.Add(QuerySelect.FieldByName('db_name').AsString);
    ListUpdater.Items.Add(QuerySelect.FieldByName('db_updater').AsString);
    ListLastRun.Items.Add(QuerySelect.FieldByName('report_lastrun').AsString);
    QuerySelect.Next;
end;

or optimized code :
With QuerySelect do
begin
    While not Eof do
    begin
        ListDatabase.Items.Add( FieldByName('db_name').AsString );
        ListUpdater.Items.Add( FieldByName('db_updater').AsString );
        ListLastRun.Items.Add( FieldByName('report_lastrun').AsString );
        Next;
    end;
end;

0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 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