On Tuesday, March 18, 2003 6:41 PM, [EMAIL PROTECTED]  wrote:

> Hi all,
> is there any module/example to create a new MS-Access Database from
> Perl?
>

Someone posted an example of how to do this using ADO/ADOX in VB about a
year or so ago. The code below is my translation test script. Also in
the ActiveState HTML documents under Win32 -> OLE -> TPJ -> MICROSOFT
ACCESS, Jan Dubois demonstrates how to do this using DAO.

A good soarce for further info are the ADO and DAO .chm files that may
already be on your machine. Look in "C:\Program Files\Common
Files\Microsoft Shared\Office10\1033\" or your equivalent.

Another place to look is the Microsoft Platform SDK under Data Access.


[code]
###########
#! perl -w
#  D:\users\John\Documents\tmp\mkaccess-db.pl
#  create MicroSoft Access database file using ADOX (???)
#  requires MDAC dlls
#  translated from VB
########################################

use strict;
use Win32::OLE qw(with in);
use Win32::OLE::Const;

# specify new database
my $dbname = 'D:/users/John/Documents/tmp/mk-db/new.mdb';

#
if (-e $dbname) {
   unlink $dbname
}


# call create database routine
&CreateAccessDatabase($dbname) ;
&CreateAccessTable($dbname) ;

# BeginCreateDatabseVB
sub CreateAccessDatabase() {
   my $db = shift;
   my $catalog = Win32::OLE->new('ADOX.Catalog');
   $catalog->Create('Provider=Microsoft.Jet.OLEDB.4.0;'.
                'Data Source='.$db)
                || die "unable to create database -
".Win32::OLE->LastError;

   #     'Clean up
   $catalog = undef;
}


sub CreateAccessTable() {
   my $strDBPath = shift;
   my $catDB =  Win32::OLE->new('ADOX.Catalog');
   use Win32::OLE::Const ('Microsoft ADO Ext. 2.7 for DDL and
Security');

   # Open the catalog.
   $catDB->ActiveConnection = 'Provider=Microsoft.Jet.OLEDB.4.0;' .
                              'Data Source=' . $strDBPath;

   my $tblNEW = Win32::OLE->new('ADOX.Table');
   # First Create an Autonumber column, called ID.
   # This is just for demonstration.
   # I could have done this below with all the other columns as well
   my $col = Win32::OLE->new('ADOX.Column');
   with ($col,
      ParentCatalog => $catDB,
      Type => 'adInteger',
      Name => "ID",
      Properties("Autoincrement") = 'True'
   );

   my $columns = $tblNEW->{Columns};
   $columns->Append($col);

   # Now add the rest of the columns
   $tblNEW->{Name} = "Contacts";
      # Create fields and append them to the
      # Columns collection of the new Table object.
      #with( $columns,
         $columns->Append("aNumberColumn", 'adInteger');
         $columns->Append("FirstName",     'adVarWChar');
         $columns->Append("LastName",      'adVarWChar');
         $columns->Append("Phone",         'adVarWChar');
         $columns->Append("Notes",         'adLongVarWChar');
      #);

      my $adColNullable;       # Is not defined in adovbs.inc,
      # so do it by and.
      # Other option is:
         # adColFixed with a value of 1
         $adColNullable = 2;
      #$columns("FirstName")->{Attributes} = $adColNullable;

   #); # End With $tblNEW

   # Add the new Table to the Tables collection of the database.
   $catDB->Tables->Append($tblNEW);
   $col = undef;
   $tblNEW =  undef;
   $catDB = undef;
}

###########
[/code]



--
Regards
       John McMahon  (mailto:[EMAIL PROTECTED])




_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to