473,625 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15928
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
3572
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
37546
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 these records how can find out which order ID was the last inserted, so that I can attach the subsesquent row items to a proper order.
1
656
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 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?
8
3841
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 the next
4
5327
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 used by the user. Thanks in advance. Regards Sameh
2
3084
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 class and than use the custon functions for query, connection etc. At some places i also use direct php mysql functions to connect and query. My problem is that on my web site sometime i dont get last insert id of an insert query. Is it because...
0
1001
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 SqlCommand("SELECT @@IDENTITY", e.Command.Connection);
0
1914
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 insert/update/delete happen agaisnt the table? Is there any catalog table that we can query for those info? Thanks in advance Dave
3
1614
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 have new information inputted (which I dont want). How do I hide this last line? Thanks in advance!
0
8692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1499
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.