Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > get value of autonumber-field

Reply
Thread Tools

get value of autonumber-field

 
 
Jan Hendrickx
Guest
Posts: n/a
 
      09-20-2005
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>


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      09-20-2005
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"


 
Reply With Quote
 
 
 
 
Bullschmidt
Guest
Posts: n/a
 
      09-21-2005
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 ***
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stl map: get the <key,value> pair which has the minimum value Rui Maciel C++ 2 12-01-2009 11:21 PM
how do I get the value for the value in the radio buttons inside t Eskimo ASP .Net Web Controls 0 02-07-2005 05:07 PM
how to get value from page.asp?value=myvalue Mike Brearley ASP General 1 05-14-2004 07:54 PM
get a value in the servlet with get, but null with post Shiv Java 3 10-13-2003 01:58 PM



Advertisments