Link to home
Start Free TrialLog in
Avatar of srbenavrbe
srbenavrbe

asked on

Easy ...

Can someone give me an example how a DBLookupComboBox
displays data from a table at runtime?
New to programming so detailed instructions are most wellcome...
Avatar of daniel_c
daniel_c

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl, ExtCtrls, Db, DBTables, DBLookup;

type
  TForm1 = class(TForm)
    DBLookupCombo1: TDBLookupCombo;
    DataSource1: TDataSource;
    Table1: TTable;
    procedure FormCreate(Sender: TObject);
    procedure DBLookupCombo1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

1. Create New Application
2. Add TDBLookupCombo component to the form
3. Add TDataSet component to the form
4. Add TTable component to the form

I have table called 1.DBF and store it in C:\. The structure is
FIELDNAME TYPE LENGTH
CHAR       C     10
NO         C     10
So, make sure you make the table first (you can create it by using database desktop).
After that, from Object Property:
1. TTable
   * Set databasename to C:\
     (you may set it to the other folder
      where the table exist).
   * Set tablename to 1.DBF (or other
     tablename you want).
2. TDataSet
   * Set DataSet to Table1 (component
     name for TTable)
3. TDBLookupCombo
   * Set LookupSource to DataSet1
   * Set LookupField to CHAR
   * Set LookupDisplay to CHAR

- On Form Event Create put this code:
procedure TForm1.FormCreate(Sender: TObject);
begin
  Table1.Active:= True;
end;

- On DbLookupCombo Event Change put this code:
procedure TForm1.DBLookupCombo1Change(Sender: TObject);
begin
  ShowMessage(DBLookupCombo1.LookupSource.DataSet.FieldByName('CHAR').AsString);
end;

So, you can understand by now how to get the value from TDBLookupCombo.
Any other problems, just let me know.


Here's the whole code:
-----------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, FileCtrl, ExtCtrls, Db, DBTables, DBLookup;

type
  TForm1 = class(TForm)
    DBLookupCombo1: TDBLookupCombo;
    DataSource1: TDataSource;
    Table1: TTable;
    procedure FormCreate(Sender: TObject);
    procedure DBLookupCombo1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Table1.Active:= True;
end;

procedure TForm1.DBLookupCombo1Change(Sender: TObject);
begin
  ShowMessage(DBLookupCombo1.LookupSource.DataSet.FieldByName('CHAR').AsString);
end;

end.


Hope it will help,

Regards,
Daniel
If you want to change the table at the runtime, just change the LookupField, LookupSource and LookupDisplay of TDBLookupCombo from your code.
Avatar of srbenavrbe

ASKER

I dont know what version of Delphi you are using (I use D5) but
I do not have TDataSet component.
Also I think that you do not need code to have the contents displayed
at runtime (Table1.Active :=true;).It can be done by selecting its property.
That goes for the ComboBox too...(List field property)...
I think I must have:
TTable and TDataSource on the form.
Shouldnt the database component be there also?
Also should not BDE be configured?
Anyway ...
For the TTable I have :
Database name : C:\Delphi\DB (where my table is stored)
Table name : Users.db (it is selectable)
Active : True
For the DataSource (TDataSource):
DataSet : Table1
DBLookupComboBox1 :
ListSource : dataSource1
List field : Name (selectable)
Data field : Name (selectable)

And when I compile ...nothing!
What am I doing wrong?

Hi Srbenavrbe,

In DBLookUpComboBox you must specify following values to work:
DataSource-Source where selected values will be written to.
DataField-Field where values will be written

ListSource-Source from whitch will you read values
KeyField-A field from whitch values will be read

And optional ListField-If you want to display other values than will be transferd.

You need two tables&dataSet's one for reading values and other for storing values. DataField and KeyField should have the same values.

In general you use DBGrid for displaying values and DBLookUpComboBox for transfering values from one to another table.

So in your example up.. you should use keyfield and another table to look from it.

BDE should only be configured if you use aliases and not path to tables.

And you don't need database component.

If anything is missing feel free to ask.
 
>>>DataSource-Source where selected values will be written to...
Is not data source where the app. reads data FROM?
Also,I just want to see data as I am a newbie and learn one step at a time.
I am trying to select a user from a table for logon purposes,so in my table i just have Name and password...
So I want to look in Combo current users...Selection type...
So please give me info. that I can understand and apply,step at a time...
Simple table - username and password -
and combo to see usernames...
So I wouild appreciate detailed instructions,after which the thing will work ;-) ...
I am a disaster in programming so please be patient ...:-) ...
ASKER CERTIFIED SOLUTION
Avatar of Roza
Roza
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This finally works...
I do not need nothing displayed on click,I was just learning on how to display data from a table in a combobox.That is all!You know ....at runtime I click on combo and have data displayed...this is all.
later on I will try and figure out
how to obtain passwords in TEdit from the same table and etc. ....
But this will be  another point Q...
right now just figuring out basics!
Thank you for your time !