Based on the help I had with a question last week, heres how to get the last insert ID from an access database using the recordset. This is Windows only.
If you are ever unfortunate enough to have to use Access then this should help you no end (it has for me :P)
use Win32::OLE;
use constant adOpenKeySet => 1;
use constant adLockPessimistic => 2;
use constant adCmdTable => '&H0002';
my $db_connection = new Win32::OLE('ADODB.Connection');
# Set the database connection details
my $db_datasource = 'Driver={Microsoft Access Driver (*.mdb)};
+';
$db_datasource .= 'DBQ=classifieds.mdb';
# Connect to the database and tie the recordset object to the
+database.
$db_connection->Open($db_datasource);
my $rs = new Win32::OLE("ADODB.Recordset");
# you're going to have to define your consts that
# you're using in the vbscript example
$rs->Open('ADS', $db_connection, adOpenKeySet, adLockPessimist
+ic, adCmdTable);
$rs->AddNew();
$rs->{'title'}{'Value'} = 'Ad title';
$rs->{'posted_by'}{'Value'} = 'Simon Proctor';
$rs->{'date_authorised'}{'Value'} = 0;
$rs->Update();
my $last_id = $rs->{'ID'}{'Value'};
$rs->close();
$db_connection->close();
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.