Solved

Easy ...

Posted on 2000-05-02
7
162 Views
Last Modified: 2010-04-04
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...
0
Comment
Question by:srbenavrbe
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:daniel_c
ID: 2771628
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
0
 
LVL 7

Expert Comment

by:daniel_c
ID: 2771629
If you want to change the table at the runtime, just change the LookupField, LookupSource and LookupDisplay of TDBLookupCombo from your code.
0
 

Author Comment

by:srbenavrbe
ID: 2772147
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?

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:Roza
ID: 2772467
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.
 
0
 

Author Comment

by:srbenavrbe
ID: 2772729
>>>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 ...:-) ...
0
 
LVL 2

Accepted Solution

by:
Roza earned 100 total points
ID: 2772832
Ok srbenavrbe... let's see what we can do...
You have a table with two fields 'username' and 'password'.

Now place DataSource and Table on your form.
Set DataSource's DataSet property to Table1.
Set Table's DatabaseName:=path where your table is stored (C:\Delphi\DB) and TableName to users.db  and set Active to True;

Now place DBLookUpComboBox on a form and set ListSource:=DataSource1
and KeyField:=username.

Then place a two labels and a button on your form, duble click it and add following lines:

  if DBLookUpComboBox1.Text<>'' THEN
  BEGIN
    Label1.Caption:=Table1.FieldByName('Username').AsString;
    Label2.Caption:=Table1.FieldByName('Password').AsString;
  END;

Now when you select username (from dblookupcombo) and you click to button you'll get username displayed to label1 and password to label2.

You could also place lines to Table's AfterScroll event.

Hope this is will work?
0
 

Author Comment

by:srbenavrbe
ID: 2772922
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 !
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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