Solved

Open a table through dropdown combo

Posted on 2002-04-27
11
244 Views
Last Modified: 2010-04-04
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
Comment
Question by:Enej
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 6973107
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
 

Author Comment

by:Enej
ID: 6973141
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
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 6973414
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6973818
Hi, Enej.

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

Thank you,

Moondancer - EE Moderator
0
 
LVL 4

Expert Comment

by:nestorua
ID: 6981746
HI,
First use GetTableNames method of TSession to fill your Combo,
then choose the Table Name from that list.
Sincerely,
Nestorua.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Enej
ID: 6982642
Mind detailing nestor????
0
 
LVL 6

Accepted Solution

by:
Stuart_Johnson earned 100 total points
ID: 6982696
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
 
LVL 4

Expert Comment

by:nestorua
ID: 6986169
HI,
I have nothing to add after Stuar_Johnson comment.
Sincerely,
Nestorua.
0
 

Author Comment

by:Enej
ID: 7736429
OK !Thanks !
0
 
LVL 1

Expert Comment

by:SJohnson
ID: 7736470
Wow!  Almost a year to finalise a question :)
0
 

Author Comment

by:Enej
ID: 7736771
I forgot about this Q really ....
Sorry !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now