Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO FIND THE NEXT AUTONUMBER

Status
Not open for further replies.

dabineri

Programmer
Jun 20, 2001
265
0
0
US
If one has a field in an access file that is an autonumber field, how does one, with VB6 DAO, find the next number to be used when the next record is created? It is not always the next numeric value after the previous one if some records have been deleted in the mean time. I need to be able to know this next number BEFORE actually creating the record.

I hope this questions makes sense and there is someone who knows how to do this.

Thanks for any advice. David Abineri

 
The only way to actually KNOW what the next number will be is to create the record. If you're needing the number to give back to the user then just create a dummy record. Then you can get the newly created ID and use an update statement instead of a insert.
 
Thanks for the response, I was afraid that might be the answer.
David Abineri

 
It's not as bad as it sounds tho. Theres a method you can call when you add the recordset to the database that will give you the Identity field. I don't remember what it is atm, but I'll try to find some old code and get it for you. I got the info from MSDN a while back when I first started working with ADO.
 
Look for @@Identity in SQL books on line, or read the article here:

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
If it is sequential and no rows are ever deleted then you should be able to see the next number by selecting the current MAX and adding one... but if there are multiple clients hitting the DB at the same time they might all get the same answer.
 
... plus ... be careful that the autonumber "type" is just sequential. random and guid won't work for any 'tricky' lookup the last one

mayhap this is obvious ... but ...



MichaelRed


 
David, just picked this up. Following up from MichaelRed's comment, if you care about the value of the autonumber field, you probably shouldn't be using one! It would be better to have a function which creates the next value together with a locking facility if the table is shared.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top