Solved

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

Posted on 2006-10-27
8
628 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

18 Experts available now in Live!

Get 1:1 Help Now