473,320 Members | 1,724 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,320 software developers and data experts.

Recalling a record insert

I want to insert a record into a DB and then immediately recall what
the ID is. I have heard that there is a way to do this with SQL Server
(although I haven't done it yet). Unfortunately this is for Access.

I've done some workarounds for this in the past but want to know if
there are better ways. What I've done in the past is to recall the
file name or something from the FORM collection against the DB
(assuming this file is unique). Using aspupload, if the overwrite
attribute is set for FALSE I can use that as a way to better ensure the
file name is unique as it renames the file if it is not unique. The
problem is I need to to this without aspupload now.

I had thought of recalling the last record ID from the DB on the FORM
side and putting it in a hidden field but this seems risky. Any
suggestions?

Thanks again!

Jul 14 '06 #1
5 1369
There's a section about doing this in Access here:
http://www.aspfaq.com/show.asp?id=2174

Ray at work

"the other john" <ki*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>I want to insert a record into a DB and then immediately recall what
the ID is. I have heard that there is a way to do this with SQL Server
(although I haven't done it yet). Unfortunately this is for Access.

I've done some workarounds for this in the past but want to know if
there are better ways. What I've done in the past is to recall the
file name or something from the FORM collection against the DB
(assuming this file is unique). Using aspupload, if the overwrite
attribute is set for FALSE I can use that as a way to better ensure the
file name is unique as it renames the file if it is not unique. The
problem is I need to to this without aspupload now.

I had thought of recalling the last record ID from the DB on the FORM
side and putting it in a hidden field but this seems risky. Any
suggestions?

Thanks again!

Jul 14 '06 #2
Excellent, thanks!
Ray Costanzo [MVP] wrote:
There's a section about doing this in Access here:
http://www.aspfaq.com/show.asp?id=2174

Ray at work

"the other john" <ki*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
I want to insert a record into a DB and then immediately recall what
the ID is. I have heard that there is a way to do this with SQL Server
(although I haven't done it yet). Unfortunately this is for Access.

I've done some workarounds for this in the past but want to know if
there are better ways. What I've done in the past is to recall the
file name or something from the FORM collection against the DB
(assuming this file is unique). Using aspupload, if the overwrite
attribute is set for FALSE I can use that as a way to better ensure the
file name is unique as it renames the file if it is not unique. The
problem is I need to to this without aspupload now.

I had thought of recalling the last record ID from the DB on the FORM
side and putting it in a hidden field but this seems risky. Any
suggestions?

Thanks again!
Jul 14 '06 #3
That article needs to be rewritten: the example given will raise an error
due to the inability of Jet to perform batched queries. This example:
<%
fakeValue = 5
set conn = CreateObject("ADODB.Connection")
conn.open "<conn string>"
sql = "INSERT someTable(IntColumn) values(" & fakeValue & ")" & _
VBCrLf & " SELECT @@IDENTITY"
set rs = conn.execute(sql)
response.write "New ID was " & rs(0)
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

needs to be rewritten as:

<%
fakeValue = 5
set conn = CreateObject("ADODB.Connection")
conn.open "<conn string>"
sql = "INSERT someTable(IntColumn) values(" & fakeValue & ")"
conn.execute sql,,129
sql = " SELECT @@IDENTITY"
set rs = conn.execute(sql,,1)
response.write "New ID was " & rs(0)
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

the other john wrote:
Excellent, thanks!
Ray Costanzo [MVP] wrote:
>There's a section about doing this in Access here:
http://www.aspfaq.com/show.asp?id=2174

Ray at work

"the other john" <ki*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googleg roups.com...
>>I want to insert a record into a DB and then immediately recall what
the ID is. I have heard that there is a way to do this with SQL
Server (although I haven't done it yet). Unfortunately this is for
Access.

I've done some workarounds for this in the past but want to know if
there are better ways. What I've done in the past is to recall the
file name or something from the FORM collection against the DB
(assuming this file is unique). Using aspupload, if the overwrite
attribute is set for FALSE I can use that as a way to better ensure
the file name is unique as it renames the file if it is not unique.
The problem is I need to to this without aspupload now.

I had thought of recalling the last record ID from the DB on the
FORM side and putting it in a hidden field but this seems risky.
Any suggestions?

Thanks again!
--
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"
Jul 14 '06 #4
Hi Bob,

could you explain these parts? I don't understand the number
references, sorry.
conn.execute sql,,129
set rs = conn.execute(sql,,1)
could you be explicit with these so I know what's happening here?

Thanks again!
Bob Barrows [MVP] wrote:
That article needs to be rewritten: the example given will raise an error
due to the inability of Jet to perform batched queries. This example:
<%
fakeValue = 5
set conn = CreateObject("ADODB.Connection")
conn.open "<conn string>"
sql = "INSERT someTable(IntColumn) values(" & fakeValue & ")" & _
VBCrLf & " SELECT @@IDENTITY"
set rs = conn.execute(sql)
response.write "New ID was " & rs(0)
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

needs to be rewritten as:

<%
fakeValue = 5
set conn = CreateObject("ADODB.Connection")
conn.open "<conn string>"
sql = "INSERT someTable(IntColumn) values(" & fakeValue & ")"
conn.execute sql,,129
sql = " SELECT @@IDENTITY"
set rs = conn.execute(sql,,1)
response.write "New ID was " & rs(0)
rs.close: set rs = nothing
conn.close: set conn = nothing
%>

the other john wrote:
Excellent, thanks!
Ray Costanzo [MVP] wrote:
There's a section about doing this in Access here:
http://www.aspfaq.com/show.asp?id=2174

Ray at work

"the other john" <ki*****@yahoo.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
I want to insert a record into a DB and then immediately recall what
the ID is. I have heard that there is a way to do this with SQL
Server (although I haven't done it yet). Unfortunately this is for
Access.

I've done some workarounds for this in the past but want to know if
there are better ways. What I've done in the past is to recall the
file name or something from the FORM collection against the DB
(assuming this file is unique). Using aspupload, if the overwrite
attribute is set for FALSE I can use that as a way to better ensure
the file name is unique as it renames the file if it is not unique.
The problem is I need to to this without aspupload now.

I had thought of recalling the last record ID from the DB on the
FORM side and putting it in a hidden field but this seems risky.
Any suggestions?

Thanks again!

--
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"
Jul 14 '06 #5
the other john wrote:
Hi Bob,

could you explain these parts? I don't understand the number
references, sorry.
The documentation for all MS technologies, including ADO, can be found here:
http://msdn.microsoft.com/library

The ADO documentation can be found by drilling into the "Win32 and COM
Development" node to "Data Access and Storage" to "Microsoft "ActiveX Data
Objects (ADO)" to "ADO Programmers Reference" to "ADO API Reference".
>
> conn.execute sql,,129
Specifically, in this case, you are looking for the
Execute Method (Connection object) -
http://msdn.microsoft.com/library/en...cnnexecute.asp
CommandTypeEnum
(http://msdn.microsoft.com/library/en...ndtypeenum.asp)
and ExecuteOptionEnum
http://msdn.microsoft.com/library/en...optionenum.asp

129 is the combination of two constants: adCmdText (always tell ADO what the
Command Type is - don't make it guess) and adExecuteNoRecords. The latter
constant tells ADO not to create a recordset because your query is not
returning records. It is always a good idea to tell it this when executing
action queries that return no records.
>
> set rs = conn.execute(sql,,1)
In this case, you should be reading the Open Method (ADO Recordset)
documentation -
http://msdn.microsoft.com/library/en...mthrstopen.asp
The 1 is adCmdText. Again, don't make ADO guess what the Command type is.
--
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"
Jul 14 '06 #6

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

Similar topics

9
by: Mark | last post by:
I have a working PHP/MySQL application used for data entry. The data entry screen includes a "Save" button. The PHP code for this button looks like this: if (isset($_POST)) { if ($_POST ==...
10
by: salamol | last post by:
I has a strange question. My company is using a old system with Win NT 4.0 Server + MS SQL 7.0. The system is busy and handle a lot of SELECTs and INSERTs all the time. Sometimes, some...
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
2
by: Chuck | last post by:
I am trying to retrive one field from the "company info" table that contains several fields but I want the "company name" field. There is only one record in this table. When I entered the code...
4
by: amywolfie | last post by:
I've been trying to do something for about 3 days – I get close, but not 100%. I am trying to: Open frmRevisionHistory from a button on frmFeeInput. If there is NO RELATED RECORD, then I...
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
4
by: CK | last post by:
Good Morning, I have a person table with personID. I have a personRate table with personID, rateID, and effectiveDate. I need to select fields from personRate, but I want the fields from the...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
9
by: Joshua.Buss | last post by:
I am trying to move a record from one linked table to another within access, but I'm a complete beginner to VBA and don't know exactly where to begin. I have an access file that has the two...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.