Solved

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

Posted on 2006-10-27
8
625 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Oops. I meant: DBLookupListbox. :-) My mistake...
0
 
LVL 6

Expert Comment

by:wimmeyvaert
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:wimmeyvaert
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

7 Experts available now in Live!

Get 1:1 Help Now