Solved

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

Posted on 2006-10-27
8
649 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
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
Industry Leaders: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 168
Delphi: Connect to running MS Outlook 4 165
tvirtualstringtree freeze when load too manny images 10 81
CheckListBox usage 3 83
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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