473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getting autonumber from last INSERT?

Hey folks -

I have some VBA code for creating a row and related sub-rows from a 'wizard'
form.

I'm using sql 'INSERT' statements to create the rows.
My problem is that I need to get the autonumber id field from the parent
table to properly insert the rows in the subtable. PHP has a function for
MySQL called mysql_last_id(). Is there something similar in VBA?

Currently I am using a "SELECT Max(id)" query to get what should be the id #
of the latest record. However, it doesn't seem to work -- it's giving me the
second-latest id, not from the most recently executed query.

Can I make this work with SQL, or should I use DAO recordsets?

Steve Lefevre
Nov 12 '05 #1
1 15880
Steve Leferve wrote:
Hey folks -

I have some VBA code for creating a row and related sub-rows from a 'wizard'
form.

I'm using sql 'INSERT' statements to create the rows.
My problem is that I need to get the autonumber id field from the parent
table to properly insert the rows in the subtable. PHP has a function for
MySQL called mysql_last_id(). Is there something similar in VBA?

Currently I am using a "SELECT Max(id)" query to get what should be the id #
of the latest record. However, it doesn't seem to work -- it's giving me the
second-latest id, not from the most recently executed query.

Can I make this work with SQL, or should I use DAO recordsets?

Steve Lefevre

Using a DAO recordset you'd do something like:

with rst
.AddNew
!Foo = bar
.update
.Move 0, .LastModified ' [1]
' you are now on the record you just inserted
end with

with ADO I think you're on the new record as soon as insert it but I've
seen inconsistencies here between ADO performed in VB (VB and VBA) and
in VBScript (ASP) so I wouldn't rely on that. If you open the recordset
"where false" so no records are returned then insert one then yours
should be the only one there so a .movelast or .movefirst should get you
onto that record but that may depend on your recordset being able to see
updates from other users (e.g. dbSeeChanges in DAO[2]).

Using SQL you're pretty much stuck unless going against a SQL Server in
which case SCOPE_IDENTITY() [3] is your friend.

[1] Check that against .LastUpdated, I may have mixed them up.
[2] To update a DAO recordset on SQL Server you need dbSeechanges so
this method would be NFG, use an SP or pass-through query and see next
footnote.
[3] There is a @@IDENTITY system variable in SQL Server but that may be
affected by inserts performed by insert triggers.

--
Error reading sig - A)bort R)etry I)nfluence with large hammer
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jdonnell | last post by:
I'm not very familiar with sql server or adodb. I'm writing a python script that uses adodb as described at http://www.ecp.cc/pyado.html, but I can't figure out how to get the id of my last insert.
1
by: George Hill | last post by:
Hi I am trying to import several master detail records from files to ms sql server. I have orders file and order_items file that has several rows for each order. If I insert programmatically...
1
by: Steve Leferve | last post by:
Hey folks - I have some VBA code for creating a row and related sub-rows from a 'wizard' form. I'm using sql 'INSERT' statements to create the rows. My problem is that I need to get the...
8
by: jiversen | last post by:
Hi all, I'm trying to get a newline into preprocessed output and I'm not sure how to do it, if it's at all possible. For example: #define _TEST i want this on one line / and this on...
4
by: Sameh Ahmed | last post by:
Hello there Is there a way to get the last control that was in focus before the current control? please note that I don't want the next or previous control in the tab order, I need the last one...
2
by: saifmsg | last post by:
Hello everyone, I am using a custom php class where i have wrapped all the mysql connection settings and query functions. Instead of calling direct mysql functions i creat an instance of this...
0
by: Sagaert Johan | last post by:
hi 2 questions This code is in the mda.Updated event ; why does the ExecuteScalar returns a decimal while in the table definition the keyfield is declared as int ? SqlCommand Cmd = new...
0
by: saidev | last post by:
Hi All, One of the table has few data until yesterday and today when i query that table there are no rows. Can please anyone explain how to identify which user has delete and when last...
3
by: PNY | last post by:
Hi all, I've made a form where it lists records sorted by their associated autonumber ID. The last line of this form displays a blank line with "AutoNumber" shown in its respective cell as if to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.