• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

retrieving autonumber field from MSAccess

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all..

i have a table in MSaccess with the one of the fields as-
SSNO (AutoNumber)

i want to retrieve this value as a new row gets inserted into the table.
i have tried last_insert_id() -> it gives "Driver Does not support this function"

Please help me..
Thank You...


 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha,
Welcome to JavaRanch!

Not all databases (or drivers) support retrieving the last record. MS Access is known for not supporting everything. Maybe you could write a query to get this number yourself?
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can get that by firing select max(yourautonumberfieldname) from yourtable query.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ujjwal soni wrote:Hi,

You can get that by firing select max(yourautonumberfieldname) from yourtable query.

This might not give you your latest id. It will only guarantee to give you the highest value of ourautonumberfieldname available in the table. And it might result in a table lock.
 
neha arora
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey.. thank you all..

i have finally used
count=0;
while(rs.next())
count++;

and again moved rs.first() by making rs as ResultSet.TYPE_SCROLL_SENSITIVE
it worked fine...
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect that your code will not work if someone else inserts in between the original insert and when your code finishes its loop. It looks like the @@identity variable exists in access per the following from microsofts site. I don't use access so you should look into this further.

After an insert a query like 'select @@identity' will return the last identity used by that connection. So even if another user makes an insert it won't affect the return variable. Plus it will be much faster than the other approach.

However, Microsoft Access 2000 or later does support the @@IDENTITY property to retrieve the value of an Autonumber field after an INSERT. Using the RowUpdated event, you can determine if an INSERT has occurred, retrieve the latest @@IDENTITY value, and place that in the identity column of the local table in the DataSet.

 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic