Populating a ComboBox

I am trying to populate a comboxbox from a table (or query). If I use dataware is only receive the first record, I however want all records in the field I select. I have done this in the following VB code;Private Sub Form_Load()Dim RecEmployees as RecordsetDim MyDB as DatabaseSet MyDB = OpenDatabase _ (“D:database.mdb”)Set Recemployees = MyDB.OpenRecordset(“Employees”)Dim i as integer For i = 0 to RecEmployees.RecordCount – 1 Combo1.AddItem (RecEmployees(1)) RecEmployee.MoveNext Next iEnd SubBut have been unable to duplicate in Object Pascal.Any assistance would be greatly appreciated.Thanks,James

In Delphi, it’s basically the same principle. Here are a couple of function that I’ve written that will seamlessly do the job:

procedure DBLoadListBox( dbSource,             {database name}                         tblSource,            {table name}                         fldName   : String;   {field name to load from}                         const LBox: TStrings);{List Box on Form}var   SourceTbl : TTable;begin  SourceTbl := TTable.Create(Application); {Create an instance of sourceTbl}  with SourceTbl do  begin    Active       := False;    DatabaseName := dbSource;    TableName    := tblSource;    try      Open;      First;      while NOT EOF do begin         LBox.Add(SourceTbl.FieldByName(fldName).AsString);         Next;      end;    finally      Free;    end;  end;end;procedure DBLoadList(tblSource,             {table name}                     fldName    : String;   {field name to load from}                     const List : TStrings); {Any TStrings}var  qry : TQuery;begin  qry := TQuery.Create(nil);  with qry do begin    Active := False;    DatabaseName := ExtractFilePath(tblSource);    SQL.Add('SELECT DISTINCT d."' + fldName + '" ');    SQL.Add('FROM "' + tblSource + '" d');    try      Open;      while NOT EOF do begin        List.Add(FieldByName(fldName).AsString);        Next;      end;    finally      Free;    end;  end;end;

The first function accesses a table directly, while the second uses a query – very useful for when you only want the DISTINCT values from a particular field.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.