Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to load data from a table into a TListBox Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi All Versions
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas

How can I create a generic procedure that will load data from a table into a 


Actually, this tip pertains to any control that has a property such as an Items 
property of type TStrings or is a descendant of TStrings. For example, the 
following controls have properties of type TStrings: TComboBox, TMemo, TQuery (SQL 
Property), TDBRadioGroup, TDirectoryListBox, TDriveComboBox, TFileListBox, 
TFilterComboBox, TListBox, TRadioGroup.

I should say that TStrings is actually an abstract class, and though you will see 
the help file list properties such as Items as TStrings, they're actually 
descendants of TStrings. More commonly, you will see something like the TStringList 
being used almost interchangeably (at least in concept) with TStrings. Essentially, 
a TStrings object is a list of strings, with array-like properties. The advantage 
of a TStrings object over an array is that you don't have to worry about allocating 
and deallocating memory to add and subtract items (which is why they're ideal when 
working with a loose collection of strings).

To load data from a column in a table into something like a TListBox is easy. All 
it takes is stepping through the table, picking out the values you want and adding 
them to the TStrings object - all in one line of code.
2   {Loads a list using values from a specific field from an open table}
4   procedure DBLoadList(srcTbl: TTable; srcFld: string; const lst: TStrings);
5   {srcTbl: Source Table, srcFld: Source Field, lst: Target List}
6   begin
7     with srcTbl do
8       if (RecordCount > 0) then
9       begin
10        {Don't bother if there are no records}
11        lst.Clear;
12        while not EOF do
13        begin
14          lst.Add(FieldByName(srcFld).AsString);
15          next;
16        end;
17      end;
18  end;

The code above assumes you have an open TTable whose values you can access. The 
real workhorse of the procedure is the while loop which steps through the table and 
grabs the value from the specified field and assigns it to a new entry into the 
TStrings object.

Operations such as the one above can be done any of those objects because the properties descend from a common ancestor.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC