• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

DBLookupComboBox

Hi there,

There is table (mysql)

levelID - integer
levelName - Varchar.

I'm trying to build a form which has one DBLookupComboBox. My intention is list all the LevelName values and when the user chooses one of them it ShowMessage the levelID.

any suggestion?
0
bilgehanyildirim
Asked:
bilgehanyildirim
  • 5
  • 3
  • 2
2 Solutions
 
wimmeyvaertCommented:
I think you should set following properties :

Listsource : DataSource which links to your Dataset (Query/Table/...)
ListField  set to levelName
KeyField set tot levelID
0
 
bilgehanyildirimAuthor Commented:
nothing happens....
0
 
wimmeyvaertCommented:
A little bit more detailled explanation :

Drop a TDBLookupComboBox and a TDataSource on your Form.

Make sure DataSource.DataSet is linked to you quer to retrieve the records from your table (this could be a Query or a Table).

Then set following Properties of the TDBLookupComboBox at design time :
     - Listsource : DataSource which links to your Dataset (Query/Table/...)
     - ListField    : levelName
     - KeyField    : levelID

Now make sure your Table or Query is open when the form is open (i.e. in the OnCreate you could set : DataSource1.DataSet.Open;

Now you could place the following code into the OnCloseUp-Event of the DBLookupComboBox :
  { This shows the levelId of the coosen levelName in a Dialog }
  ShowMessage( IntToStr( DBLookupComboBox1.KeyValue ) );

If you have questions, please feel free to ask. I can also make a little demo if you want.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
wimmeyvaertCommented:
I'm leaving the company now. If you have questions I'll be happy to answer them tomorrow.
0
 
geobulCommented:
Hi,

I would use TComboBox instead because it has OnChange event which is more useful IMHO. The reason is that the user might enter a value directly in the edit field or select next/previous items by using arrow keys (i.e. without expanding the listbox part of the combobox). But you'll need some code to implement that:

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ADOQuery1: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // open the dataset and fill the combobox
  with ADOQuery1 do begin
    SQL.Clear;
    SQL.Add('SELECT Account_ID, Account_Name FROM Accounts');
    Open;
    ComboBox1.Items.Clear;
    ComboBox1.Text := '';
    ComboBox1.ItemIndex := -1;
    while not EOF do begin
      ComboBox1.Items.Add(FieldByName('Account_Name').AsString);
      Next;
    end;
  end;
end;

// search for a corresponding record and show its ID if found
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if ADOQuery1.Locate('Account_Name', ComboBox1.Text, []) then
    ShowMessage(ADOQuery1.FieldByName('Account_ID').AsString);
end;

end.

Regards, Geo
0
 
wimmeyvaertCommented:
Hi Geobul,

With a TDBLookupComboBox you can also type in a characterstring. It automatically positions to the first record that meets your input.

The only disadvantage is that you don't see what you are typing.

I made a little example (using the DBDEMOS-database). It only has 3 lines of code in it (connect to db, open table and make sure first item in combo is showed).
It has a LookupCombo and a DBText on the Form. When the user selects an item from the list, the ID of the record is automatically shown in the DBText.

So I think the best solution is just use the TDBLookupComboBox instead of a normal (non db-aware) TComboBox.


To bilgehanyildirim : I can send you the little demo-app I made to show you what a TDBLookupComboBox is capable of.

Best regards,

The Mayor

PS : Thanks for the points anyway.
0
 
bilgehanyildirimAuthor Commented:
wonderfull!!!! could you send it to main.ebay@gmail.com, if possible?
0
 
geobulCommented:
>With a TDBLookupComboBox you can also type in a characterstring. It automatically positions to the first record that meets your input.

But OnClose event is not fired then and you simply can't use ShowMessage. I agree that using DBEdit binded to levelID field in the same dataset will display the number but there is no event that will be fired and that can be used for some purposes. Am I right?
0
 
wimmeyvaertCommented:
hi bilgehanyildirim,

I just sent you the little demo-app I made.

Geobul,
Can't you make use of the Datasource's Ondatachange-Event to do you showmessage then ?
0
 
geobulCommented:
I don't think so. That event has been fired many times in one down-arrow keypress, for instance. Try this:

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  if Field = nil then ShowMessage('Change');
end;

Regards, Geo
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now