By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

get value of autonumber-field

P: n/a
Hi all,

I use (something like) following code to add new records to a database,
but it doesn't work!!!

I need to know the value of an AutoNumber-field when I add the record.

Why doesn't it work and how can I make it work???
I think it should be easy, but I can't figure it out!

Thanks in advance...
Jan H.

<%option explicit
dim cn, rs, v1, v2, v3
set cn=server.createobject("ADODB.Connection")
set rs=server.createobject("ADODB.Recordset")
cn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" &
server.mappath("./mdb/MyMDB.mdb")
rs.open "SELECT * FROM tblTest",cn,3,3
rs.addnew
rs("Naam")="Some new value"
v1=rs("ID")
rs.update
v2=rs("ID")
rs.moveprevious 'this works,
rs.movenext 'but only when
v3=rs("ID") 'not first record!!!!
rs.close
cn.close
set rs=nothing
set cn=nothing%>
<html>
</head>
<body>
*** <%=v1%> *** <%=v2%> *** <%=v3%> ***
</body>
</html>
Sep 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jan Hendrickx wrote:
Hi all,

I use (something like) following code to add new records to a
database, but it doesn't work!!!

I need to know the value of an AutoNumber-field when I add the record.

Why doesn't it work and how can I make it work???
I think it should be easy, but I can't figure it out!

Thanks in advance...
Jan H.

http://www.aspfaq.com/show.asp?id=2174
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Sep 20 '05 #2

P: n/a
Perhaps change this:

rs.open "SELECT * FROM tblTest",cn,3,3

To be more like this instead:

rs.open "SELECT * FROM tblTest",cn,1,3

And here is a memo I previously wrote to myself about the issue:

Use an autonumber in a table whenever can't get a unique record from
just one field. This is needed in subquery on "many" pages and will
make things easier if just have to check one field for the cur rec etc.

And's some sample code I use WHEN FIRST OPENING the recordset (notice
the CursorType) so that later I can easily get the new Autonumber:

' Open rs.
Set objRS = Server.CreateObject("ADODB.Recordset")
' (1=CursorType of adOpenKeyset in case ever want to get an autonumber
of new rec,
' 3=LockType of adLockOptimistic because updating.)
objRS.Open strSQL, objConn, 1, 3

Best regards,
J. Paul Schmidt, Freelance ASP Web Developer
http://www.Bullschmidt.com
ASP Design Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...

<<
Hi all,

I use (something like) following code to add new records to a database,
but it doesn't work!!!

I need to know the value of an AutoNumber-field when I add the record.

Why doesn't it work and how can I make it work???
I think it should be easy, but I can't figure it out!

Thanks in advance...
Jan H.

<%option explicit
dim cn, rs, v1, v2, v3
set cn=server.createobject("ADODB.Connection")
set rs=server.createobject("ADODB.Recordset")
cn.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" &
server.mappath("./mdb/MyMDB.mdb")
rs.open "SELECT * FROM tblTest",cn,3,3
rs.addnew
rs("Naam")="Some new value"
v1=rs("ID")
rs.update
v2=rs("ID")
rs.moveprevious 'this works,
rs.movenext 'but only when
v3=rs("ID") 'not first record!!!!
rs.close
cn.close
set rs=nothing
set cn=nothing%>
<html>
</head>
<body>
*** <%=v1%> *** <%=v2%> *** <%=v3%> ***
</body>
</html>


*** Sent via Developersdex http://www.developersdex.com ***
Sep 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.