Solved

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

Posted on 2006-10-27
8
671 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month3 days, 21 hours left to enroll

630 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