473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ASP: Retrieve autonumber field on INSERT

Hi,

Is there an easy way in ASP/VBscript to grab an autonumber (primary
key) field just after an SQL insert? This is probably easy, but I'm
stuck ..

Cheers.

Nov 13 '05 #1
3 7862
I think you would have to do a query to pull up the the record after
it's inserted, because the SQL insert statement is like a for loop in
that you can't insert records one at a time and then examine the
results such as an autonumber field.

This would be somewhat hackish IMHO. You could literally insert
records one at a time, and just before you insert the record, save it's
values locally, and after you insert, then do a query to find that
record(assuming you can find some combination of values that will be
unique) and examine it's autonumber field. I don't know alot about
SQL, but this would probably run much slower than an insert statement
that isnerts all the records as a batch.

Perhaps you should consider generating the primary key yourself? So
that you know what it is. Instead of having an autonumber field, setup
a primary key field that is some concatination of other fields or just
a number you generate based on what numbers already don't exist in that
column. This is similar to using multiple primary keys, but without
dealing with seperate keys. Even with 2 primary keys then normally
complicated queries get super complicated(at least for me).

I'm a noob BTW, so take my suggestions with a grain of salt.

Nov 13 '05 #2
"SQL insert statement is like a for loop in"
This should have read:
"SQL insert statement is not like a for loop in"

And I'm referring to an isenrt statement that inserts multiple records
in one statement. You can't somehow look at the results of each record
insert as the insert statement is running. That's why you would need
to make you're own for loop. But I think making your own for loop is
fvery hackish round about way of solving the problem. I really think
you should somehow generate a fully deterministic primary key if it is
important you know what the primary key is of a record when it is
inserted, rather than using an autonumber field.

Nov 13 '05 #3
Hi!

You probably have a connection variable,
on which you execute the insert. Use
select @@identity on it:

cnn.execute "your insert....."
set rs=cnn.execute("select @@identity",,adcmdtext)
msgbox rs.fields(0).value

I don't know much about script syntax,
but the above should work in VBA, given
rs is declared as adodb.recordset.

ben.werdmul...@sbs.ox.ac.uk wrote:
Hi,

Is there an easy way in ASP/VBscript to grab an autonumber (primary
key) field just after an SQL insert? This is probably easy, but I'm
stuck ..

Cheers.


Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dr. Know | last post by:
I am working on an ASP page that writes to several databases, ranging from MDBs to x-base. One of the tasks involves using an existing highest value from the DB and incrementing it before...
3
by: Ilan Sebba | last post by:
I have a 'supertype' table with only one field: autonumber. Call this table the 'parent' table. There are two subtypes, 'androids' and 'martians'. Martian have only one thing in common: they give...
8
by: bigbinc | last post by:
I am in autonumber hell, it is important for me to get the AutoNumber even in case of previous deleted records, and I cant get the value through a 'test' insert and then delete method. There is no...
1
by: loreille | last post by:
To insert a record in a Ms Access database and be able to retrieve the newly created ID (autonumber) I used the code below (code 1). Now, the problem is that this is not very secure and that, if...
8
by: petebeatty | last post by:
I have created a SQL string the properly inserts a record in the table. However, the insert does not occur at the end of the table. Instead it inserts a record after the last record that I viewed....
11
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any...
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
4
by: Lyn | last post by:
Hi, Is there an "easy" way to write a full record (all fields) using "INSERT INTO..." into a table which has an AutoNumber field? Normally, to write a full new record I would use: INSERT INTO...
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.