Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Ms Access and win32 ole

by sumit07 (Novice)
on Oct 25, 2012 at 10:51 UTC ( #1000819=perlquestion: print w/ replies, xml ) Need Help??
sumit07 has asked for the wisdom of the Perl Monks concerning the following question:

Hi perl monks, I badly need your help with this. I want to get the custom properties of ms Access files. I am able to open .mdb files but not .accdb files. Can any one help me with this.
my $filename = "database.accdb"; # $ARGV[0]; print $filename."\n"; $oAccess = Win32::OLE->new('Access.Application') or die qq{Couldn't st +art new Access instance!}; # Open Access File $oDatabase = $oAccess->DBEngine->Workspaces(0)->OpenDatabase($filename +);
When I tryto open .accdb files it says
OLE exception from "DAO.Workspace": Could not find file 'database.accdb'. Win32::OLE(0.1709) error 0x800a0bd0 in METHOD/PROPERTYGET "OpenDatabase" at accessTest.pl
I also tried this code
my $filename = "database.accdb"; # $ARGV[0]; print $filename."\n"; $oAccess = Win32::OLE->new('Access.Application') or die qq{Couldn't st +art new Access instance!}; # Open Access File $oDatabase = $oAccess->OpenCurrentDatabase($filename);
But with this also it says file not found.

Do I need to install any drivers for ms access or any thing like that.

Comment on Ms Access and win32 ole
Select or Download Code
Re: Ms Access and win32 ole
by rpnoble419 (Pilgrim) on Oct 25, 2012 at 11:17 UTC
    What is it that you are trying to access? Can you provide a sample of the custom property you want to read?
Re: Ms Access and win32 ole
by sundialsvc4 (Abbot) on Oct 25, 2012 at 13:19 UTC

    Hmm, when did they change that file-extension?   It’s been .mdb forever.

      They created a new file extension, they didn't change it. Office / Access 2007 introduced the accdb file extension and database type.

Re: Ms Access and win32 ole
by Sinistral (Prior) on Oct 25, 2012 at 13:55 UTC

    Here are some questions that may help:

    1. Are your using Access 2007 or 2010? If you're using an older version, it won't open accdb files.
    2. Can you run Access the normal way (interactively) and open the file successfully?
    3. If you can open it when using the interactive method, what happens if you specify the complete path to the accdb file in your OpenCurrentDatabase call? It's very likely that without the full path Access' current working directory won't be the one where your Perl script and accdb file are located.
      Thank you for the hint Sinistral I was not giving the whole path. You really helped me. Thanks a ton :)

      Hi Sinistral

      I am now trying to open using this code
      $oDatabase = $oAccess->DBEngine->OpenCurrentDatabase($filename);
      It says invalid argument.Do we need to provide other arguments also.

      With this code $oDatabase = $oAccess->DBEngine->OpenDatabase($filename);

      It opens fine but the custom properties doesnot get added to the file's custom tab in the database properties.
      Is there any difference between OpenDatabase and OpenCurrentDatabase.
      
      my $filename = 'C:\Documents and Settings\551970\Desktop\database.accd +b'; # $ARGV[0]; print $filename."\n"; $oAccess = Win32::OLE->new('Access.Application') or die qq{Couldn't st +art new Access instance!}; # Open Access File $oDatabase = $oAccess->DBEngine->OpenDatabase($filename); my $new_property = $oDatabase->Containers->Databases->Documents->User +Defined->CreateProperty("Test1",12,"Test"); $oDatabase->Containers->Databases->Documents->UserDefined->Propertie +s->Append($new_property); foreach (in $oDatabase->Containers->Databases->Documents->UserDefined- +>Properties) { #print Dumper($_); print "prop ".$_->Name."\n"; } undef $oDatabase; #undef $oAccess; $oAccess->Quit();

        Most of my Win32::OLE experience has been with Excel, not Access, but a little Googling found this link: http://msdn.microsoft.com/en-us/library/office/bb238012(v=office.12).aspx which specifically says that OpenCurrenDatabase is for manipulating Access and opening files, while OpenDatabase returns a Database variable but doesn't actually open the file in the Access window. So for you, you want OpenCurrentDatabse. Beyond that, the best thing I can offer is to check out the documents I linked to, which are the Automation docs for Access 2007 (switch to 2010 if that's what you're using).

        The other tip I've seen is to record a macro of the task you're trying to achieve and examine the VBA code that Access creates. It looks like you're on your way already to knowing the Perl Win32::OLE to VBA equivalent

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1000819]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-09-24 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (246 votes), past polls