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

Fellow Monks,
I have a very simple question which unfortunately is giving me problems. I wish to display an Access database. That's it. I don't need to perform queries or retrieve data; all I need is to open and display the database. Is there a way to do this with OLE instead of 'system' or 'exec'? I know how to do this in Excel:
use strict; use OLE; my $excel = CreateObject OLE "Excel.Application"; my $wrkbk = $excel -> Workbooks -> Open("C:/File.xls"); $excel -> {Visible} = 1;
And I know how to do this in Word:
use strict; use OLE; my $word = CreateObject OLE "Word.Application"; my $doc = $word -> Documents -> Open("C:/File.doc"); $word -> {Visible} = 1;
What is the equivalent for Access? Thanks.

Replies are listed 'Best First'.
Re: How to display an Access database
by GrandFather (Saint) on Jun 18, 2007 at 21:15 UTC

    Do you need to use OLE? Access is a "database" so the natural way to access it in Perl is using DBI.

    To open an Access database use:

    # Open the database: my $dbh = DBI->connect ('dbi:ADO:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' . $db_file) +;

    DWIM is Perl's answer to Gödel
      I prefer OLE because it's what I know. I don't know anything about DBI (I'm trying to learn it currently).

      My question then is will this simply open and display the Access file? What I need is the same thing a system call does. I don't want to open it to send/receive data.

        The snippet I gave will open a database handle on the database file. It is almost exactly like using open to open a file handle.

        I'd be inclined to just use system or backticks to run the Access application if that is all you want to achieve.


        DWIM is Perl's answer to Gödel
Re: How to display an Access database
by Util (Priest) on Jun 19, 2007 at 01:02 UTC

    FYI, here are the first two lines from site/lib/OLE.pm:

    # Compatibility layer for applications using the old toplevel OLE.pm. # New code should use Win32::OLE

    The code below is very suspect. I wrote it in 2004 as part of a hurried (and failed) effort to dump all the details of the Query and Report objects from an Access database. The program was never completed or documented, and I don't recall any of the reasoning behind its peculiarities, such as why not to set Visible if Visible is already set. Hopefully it will be enough to solve your problem.

    Finally, I am adding the code below, which uses the command-line method you said you did not want, just in case you eschewed the simpler method from some false pre-conceived idea.

Re: How to display an Access database
by planetscape (Chancellor) on Jun 19, 2007 at 13:21 UTC
    use strict; use warnings; use Win32::OLE; my $access = Win32::OLE->new('Access.Application') or die "oops\n"; my $database = Win32::OLE->GetObject("C:\\Northwind.mdb");

    HTH,

    planetscape