Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 18th, 2002, 08:13 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Last inserted ID in MS access

Hi. I am developing a site for a gentleman and have to use access as the DBMS. I need to insert some data into the database. Usually when I have one main table (e.g., Users) and need to also insert data into some dependant tables (e.g., Orders) where the ID from Users is a foriegn key of the dependant table, I use the mysql function mysql_insert_id to get the last inserted ID.

I was wondering if anyone had an equivelent way to obtain the last inserted ID from a Jet Access DB? I could just insert it and turn around and do a select getting the largest ID, but that is risky if another gets inserted by another user. Any ideas?

Thanks a lot.

Reply With Quote
  #2  
Old March 19th, 2002, 02:36 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
^^bump

Reply With Quote
  #3  
Old March 19th, 2002, 02:47 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,381 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 7 h 28 m 9 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
Don't do that bump crap around here.

If no one is answering your question, then no one knows the answer.

Can you lock the tables in Access? lock the table, insert, select max(id), release lock.

Maybe check out this site for any info:

http://www.access-programmers.co.uk/

---John Holmes...

Reply With Quote
  #4  
Old March 19th, 2002, 05:57 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Thanks for the reply.

The reason I bumped it was because I know this forum moves very quickly, as I have contributed to it in the past. Considering that others probably have more of a life than you must and don't hover over these forums every hour of the day, I thought I would make it visible to the ones that might be able to look at it in the daytime rather than at night, which is when I originally posted it.

You seem awfully arrogant that if you don't know the answer, then no one does. My guess is that there are some knowledgable people that frequent this forum that aren't on even every day. Something to think about.

Anyway, thanks for your reply. I will check out your link.

Reply With Quote
  #5  
Old March 19th, 2002, 07:24 PM
thedude thedude is offline
The Dude Abides
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2000
Location: grass valley,ca
Posts: 1,063 thedude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 4 m 29 sec
Reputation Power: 10
John has every right to do what he did, he is the moderator. And he is right, if no one answers you then no one knows the answer.

This really isn't the area to post a question like that anyhow, these forums are for mysql and php, not access/jet. You would probably get a better answer on forums dedicated to those databases.
__________________
The Dude
I'm the Dude. So that's what you call me.
That, or Duder, His Dudeness, Or El Duderino.
If, you know, you're not into the whole brevity thing

Reply With Quote
  #6  
Old March 19th, 2002, 07:42 PM
Sepodati's Avatar
Sepodati Sepodati is offline
Banned
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Dec 1999
Location: Afghanistan
Posts: 14,381 Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)Sepodati User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate FolderFolding Points: 70626 Folding Title: Intermediate Folder
Time spent in forums: 2 Months 4 Weeks 7 h 28 m 9 sec
Reputation Power: 1784
Send a message via ICQ to Sepodati Send a message via Yahoo to Sepodati
All I made is a simple request. If you do frequent these boards enough, you'll know that questions get answered if people know the answer.

Since this is a Access question and not PHP, I'm moving this to the General Database forum. I guess you could be using PHP and ODBC to access the database, but since there is no equivilent to mysql_insert_id() in the ODBC functions, you'll have to find an Access solution.

Check out this, too

http://www.php.net/manual/ro/faq.databases.php#AEN78840

it talks about some alternative ways to use Access and PHP.

---John Holmes...

Reply With Quote
  #7  
Old March 20th, 2002, 12:36 AM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 53
1. Start a transaction by disabling Autocommit, with odbc_autocommit(false) or odbc_autocommit(0).

2. "SELECT MAX(id) etc..." or whatever your primary key column is, so you get the most recently used ID.

3. Increment that number by one, and use it to explicitly set your next id for insertion.

4. Run your insert query using that incremented value. If the INSERT query fails, then do an odbc_rollback(). If it succeeds, then do odbc_commit.

Now you have inserted the record, and you have the insert ID from part 3 above. Since we used a transaction, there is very little chance for another user to "sneak in" and insert another record with the same ID, but if so, the insert will fail, so you can just do a rollback, and increment the number one more time, and try again.

Enclose your whole series of related INSERTs in this transaction, so that you don't accidentally insert a related record with an id of 0.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #8  
Old December 8th, 2003, 11:28 PM
Vee Vee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 Vee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
PHP Code:
 $cur0odbc_exec$cnx"select * from Person" ); 
$nbrows=0;
while( 
odbc_fetch_row$cur0 ) ) { 
$nbrows++; 
$PersonIDodbc_result$cur0); } 


that's after establishing a connection with odbc

and the ,1 is the personID assuming its the first field or alternatively putting the name of the field there

eg.

PHP Code:
 $PersonID=odbc_result($cur0"PersonID"); 

Reply With Quote
  #9  
Old December 9th, 2003, 03:44 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,773 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 14 h 30 m 5 sec
Reputation Power: 37
Stay away from select max.

Access supports the syntax

select @@identity

That query can be used after an insert statement to get the latest counter value.
Comments on this post
R++ agrees: wow

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Last inserted ID in MS access


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway