Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Using Win32::ODBC, how can I get a list of the tables and queries in my M$ Access datastore please?

Thanks
  • Comment on Obtaining List of tables from ms access database

Replies are listed 'Best First'.
Re: Obtaining List of tables from ms access database
by Corion (Patriarch) on Dec 03, 2007 at 16:35 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Obtaining List of tables from ms access database
by CountZero (Bishop) on Dec 03, 2007 at 19:48 UTC
    DBD::ADO has a ado_open_schema-method which returns information about the tables in the database.

    use strict; use DBI; use Data::Dump qw/dump/; my $dsn = 'test'; my $dbh = DBI->connect("dbi:ADO:$dsn", '', '' ) or die $DBI::errstr; my $sth = $dbh->ado_open_schema( 'adSchemaTables' ) or die $DBI::errst +r; my $result = $sth->fetchall_hashref('TABLE_NAME'); print dump(\$result);

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Obtaining List of tables from ms access database
by girarde (Hermit) on Dec 04, 2007 at 02:08 UTC
    The author of Win32::ODBC now recommends using DBI instead of his own module. This is a hint to be taken.
You can also try Win32::OLE + native DAO and/or ADO objects and methods
by bdimych (Monk) on Dec 06, 2007 at 15:03 UTC
    not tested, just principles
    my $dao = Win32::OLE->new('DAO.DBEngine.36'); my $db = $dao->OpenDatabase("access_database.mdb"); my $tables = $db->TableDefs; for (my $i = 0; $i < $tables->{Count}; $i++) { print $tables->Item($i)->{Name}, "\n"; } ......... see c:\Program Files\Common Files\Microsoft Shared\OFFICE11\1049\DAO360.CH +M my $ado = Win32::OLE->new('ADODB.Connection'); $ado->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=access_databa +se.mdb"); my $recordset = $ado->OpenSchema(20); # 20 is adSchemaTables until ($recordset->{EOF}) { print $recordset->{Fields}->Item('TABLE_NAME'), "\n"; $recordset->MoveNext; } ......... $recordset = $ado->Execute("SELECT ... FROM ... WHERE ...")->GetRows() +; print $recordset->[0]->[0], "\n"; ......... see c:\Program Files\Common Files\Microsoft Shared\OFFICE11\1049\ADO210.CH +M and c:\Program Files\Common Files\Microsoft Shared\OFFICE11\1049\JETSQL40. +CHM and also see MS Access application objects model c:\Program Files\Microsoft Office\OFFICE11\1049\VBAAC10.CHM