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

Open a table through dropdown combo

Just for learning sake....
How can I open (Database is Access using ADO) a table when I select an entry in dropdown combo?
Example:
I have city names in my strings in combo so when I select a name I get the db table open in db grid and so on...
0
Enej
Asked:
Enej
  • 4
  • 2
  • 2
  • +3
1 Solution
 
Stuart_JohnsonCommented:
If you're using a TADOTable to open the tables, and provided the table names are the same as the city names, then do this:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  ADOTable1.Close;
  try
    ADOTable1.TableName := ComboBox1.Text;
    ADOTable1.Open;
  except
    on E: Exception do
      MessageDlg('Unable to open table ' + Combo1.Text + #13+ E.Message, mtError, [mbOK], 0);
  end;
end;

Hope this helps,

Stu
0
 
EnejAuthor Commented:
First, on ...exception is malfunctioning?
I added nonexistent table name to combo string just to test it but did not get the above msg,only windows warning that it cant open table.(?)

0
 
Tomas Helgi JohannssonCommented:
Hi!

I'm guessing that you are using 1 table with information about cities.
If so you should have index on the city-name field in the table and do the following.

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 ADOTable1.Close;
 try
   ADOTable1.TableName := sTableName; // 1)
   ADOTable1.Open;
   ADOTable1.FindKey([ComboBox1.Text]);
 except
   on E: Exception do
     MessageDlg(E.Message, mtError, [mbOK], 0);
 end;
end;

1) sTableName is a string variable which holds the tablename. This could be set at design time.

I beleive that you know how to connect the table to a datasource and the datasource to a dbgrid. ;)
The information about the selected city should appear in the dbgrid.

Regards,
Tomas Helgi


0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
MoondancerCommented:
Hi, Enej.

I posted comments in two of your old, open questions; please update and finalize them.

Thank you,

Moondancer - EE Moderator
0
 
nestoruaCommented:
HI,
First use GetTableNames method of TSession to fill your Combo,
then choose the Table Name from that list.
Sincerely,
Nestorua.
0
 
EnejAuthor Commented:
Mind detailing nestor????
0
 
Stuart_JohnsonCommented:
Say your combo is called TableListCombo.

Session.GetTableNames('ALIASNAME', '*', false, false, TableListCombo.Items);

This will populate your combo with all the tables in that alias.  That will ensure you don't get the error you mentioned above.

Stu
0
 
nestoruaCommented:
HI,
I have nothing to add after Stuar_Johnson comment.
Sincerely,
Nestorua.
0
 
EnejAuthor Commented:
OK !Thanks !
0
 
SJohnsonCommented:
Wow!  Almost a year to finalise a question :)
0
 
EnejAuthor Commented:
I forgot about this Q really ....
Sorry !
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

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