473,789 Members | 2,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get value of autonumber-field

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.creat eobject("ADODB. Connection")
set rs=server.creat eobject("ADODB. Recordset")
cn.open "DRIVER={Micros oft Access Driver (*.mdb)};DBQ=" &
server.mappath( "./mdb/MyMDB.mdb")
rs.open "SELECT * FROM tblTest",cn,3,3
rs.addnew
rs("Naam")="Som e 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
2 2831
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
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.CreateOb ject("ADODB.Rec ordset")
' (1=CursorType of adOpenKeyset in case ever want to get an autonumber
of new rec,
' 3=LockType of adLockOptimisti c 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.creat eobject("ADODB. Connection")
set rs=server.creat eobject("ADODB. Recordset")
cn.open "DRIVER={Micros oft Access Driver (*.mdb)};DBQ=" &
server.mappath( "./mdb/MyMDB.mdb")
rs.open "SELECT * FROM tblTest",cn,3,3
rs.addnew
rs("Naam")="Som e 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
19934
by: Roger Withnell | last post by:
I'm inserting a new record into an MS SQL database table and I want to obtain the new records autonumber immediately afterwards, as follows: MadminRS.CursorLocation = adUseServer MadminRS.CursorType = adOpenKeyset MadminRS.LockType = adLockOptimistic MadminRS.Open "NavBar", objConn, , , adCmdTable MadminRS.AddNew MadminRS("Url") = Request.Form("Website") MadminRS("ParentRecNo") = 0
1
3257
by: Robert | last post by:
I am trying to create a db for service providers by county. I'm relatively new to db programming, but I have done quite a bit of programming ranging from the old basic days up to doing some programming in the HotDocs software. I've kind of accomplished my goal in access, but I'm not quite there yet and figure I've really screwed something up. The db consists of 5 tables I believe. T1 is the main contact info, with an autonumber ID...
2
1423
by: Reddy | last post by:
Hi, I am inserting records into a table having a autonumber column(ID) as primary key. I get the value of ID by using select Max(ID) from table. Is there any way to retieve the value immediately after inserting the record without another query (like in ado). Any tutorials with example would be great Thanks
1
1404
by: kchatel | last post by:
Is it possible in Access to do the following. I filtered my table to have the folowing values ProdID Color ------------------------ 2 Black 2 Red 2 Green Now is it possible to create a view that only displaces 1 unique row
12
2823
by: erick-flores | last post by:
Hello all, Here is what i am trying to do doing: Form A & Form B Form A: compressor_id = 23 Description: test Form B: stage_id = 12 compressor_fk = compressor_id
6
1907
by: PeteCresswell | last post by:
Got a table with "RecordID"=AutoNumber=PrimaryKey. Call it "tblIssuerRating". Ran a query that appends to that table from a work table. Inadvertantly appended from the work table's "RecordID", which was not autonumber and, in fact, contained a zero. The query ran successfully and I would up with a tblIssuerRating
9
2345
by: oriondcv | last post by:
hi, please help me on this issue that im facing... I have created a filtered combo box in ms access. It works but the value it inputs in the database is the primary key value like in my program (lngMyID =1) . What i would like to have is the value that 1 represents username. here is my source code Private Sub cboStore_AfterUpdate() Dim sManagerSource As String Dim sFunctional As String Dim sFunctionMade As Variant
4
3198
by: banderson | last post by:
Hello amazing vba writers, I am trying to make a combo box return a value based on a combo box selection. I have tried a number of the codes posted here and am still having problems. I think it is because I am trying to return a value from a table that only links with the form through an intermediate table. I am hoping to avoid a subform for this one box if at all possible, because of space limitations. Any guidance would be greatly...
4
1965
benchpolo
by: benchpolo | last post by:
I have a dropdown combo "TaxYear" with 2 fields Autonumber, Year. When data are entered in form the value that displays in the dropdown combo are Year, but when i assign the me.year.value to a global variable gYear and call this global value in me.lblYear.caption = gYear i am not getting the Year value, but instead the autonumber value. Please advise. Thanks.
6
8078
by: Wayne | last post by:
I'm using the following SQL statement to find the next highest autonumber value in a table where "CDUGActID is the autonumber field in the "CDUGActuals" table: SELECT CDUGActuals.CDUGActID, CDUGActuals.ActualTonnes, +1 AS NextID FROM CDUGActuals; This works well as long as no autonumber values have been skipped due to deletions or cancelled entries. Is there a simple change I can
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.