I'm trying to connect to an Access database file on my local machine. I did this long ago with an older version of Access with no troubles but I can't seem to get it to work this time.

Here's the code:
use strict; use DBI; use Data::Dumper qw(Dumper); my $dsn = 'dbi:ODBC:driver=Microsoft Access Driver (*.mdb,*.accdb);' . 'dbq=C:\Database2.accdb'; my $dbh = DBI->connect($dsn, '','') or die DBI->errstr(); my $query = "SELECT * FROM Table1 LIMIT 1"; my $sth = $dbh->prepare($query); $sth->execute() or die $sth->errstr(); my $row = $sth->fetchrow_hashref(); print Dumper($row);

But all I get is "Data source name not found and no default driver specified (SQL-IM002)". I google this error and get basically that the file or the driver is wrong. But I've verified I have this driver (I've also tried it as Microsoft Access Driver (*.mdb) and the file is definitely there.

Basically I've wasted many hours on this now and I'm hoping someone has a solution!

Thanks in advance

Update

I got it to work using a DSN in the Data Sources console. Still not exactly what I want since reading the docs shows I should be able to do it without a DSN. Good enough for now, but if someone has done it without a DSN, please reply and let me know what I'm doing wrong. Thanks

In reply to Connect with DBI to Access 2007 by cfreak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.