Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - How to retreive the autonumber
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

How to retreive the autonumber

 Post Reply Post Reply Page  <12
Author
maniva View Drop Down
Groupie
Groupie
Avatar

Joined: 04 April 2002
Location: Italy
Status: Offline
Points: 82
Post Options Post Options   Thanks (0) Thanks(0)   Quote maniva Quote  Post ReplyReply Direct Link To This Post Posted: 28 February 2003 at 2:25am

Hi to all,
just to clarify for MADDOG i Know the value of Author_ID what I don't know is the value of ID ( my Key that is an Autonumber field)

And for my friend MorninZ I tryed in different way but it is not retreiving the number. Sorry.
I'm using Access2000 DB.

I followed a suggestion of boRg that suggest me to define an SQL by Author_id and sorted by ID descending and then after insserting the new record and fiels requery the table.

Bye  



Edited by maniva
Ivano Mangiarotti
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 01 March 2003 at 4:45am

If your'e using Access2K, then use the Jet database driver and execute a "SELECT @@Identity" after you create the records and it will return the autonumber:
http://www.adopenstatic.com/Experiments/FastestAutonumber.asp

Cheers,
Andrew

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
maniva View Drop Down
Groupie
Groupie
Avatar

Joined: 04 April 2002
Location: Italy
Status: Offline
Points: 82
Post Options Post Options   Thanks (0) Thanks(0)   Quote maniva Quote  Post ReplyReply Direct Link To This Post Posted: 02 March 2003 at 3:59pm

Hi Bunce,

Thank you wery much your example and documantation are exellent.

Ivano Mangiarotti
Back to Top
Stinger View Drop Down
Newbie
Newbie
Avatar

Joined: 15 February 2003
Location: Italy
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote Stinger Quote  Post ReplyReply Direct Link To This Post Posted: 03 March 2003 at 11:30am

I had the same problem and made this script. I don't have to create a diff recordset this way. It works for me (basically I don't use a SQL statement to create the rs, but I user the AddNew method. After the Update the recordset is still pointing the added row, so u can retrieve the autonumber). Just change connection db name, table name and fields:

Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & server.mappath("dbpath.mdb") & ";")

 Set oRS=CreateObject("ADODB.Recordset")

 oRS.ActiveConnection = oConn
 oRS.CursorLocation = adUseClient
 oRS.CursorType = adOpenKeyset
 oRS.LockType = adLockOptimistic
 oRS.Source = "tablename"
 oRS.Open

 oRS.AddNew
  oRS("Field1") = "Value1"
  oRS("Field2") = "Value2"
 oRS.Update

 intAutoID = oRS("ID")

From your code, if you use just the last line after the Update, you should be able to retrieve the ID.



Edited by Stinger
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 03 March 2003 at 5:03pm

You *are* opening a recordset. As soon as you do oRS.Open.

Unless you wish to work with the recordset, such as displaying or manipulating the output some way, then why bother with the overhead of creating a recordset?

Set based processing (ie. INSERT's, UPDATES etc) is generally the preferred method..

Cheers,
andrew



Edited by Bunce
There have been many, many posts made throughout the world...
This was one of them.
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.