Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


SQL Insert Into return the Autonumber

 
Logged in as: Guest
arrSession:exec spGetSession 2,3,2543
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> ASP >> SQL Insert Into return the Autonumber
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 SQL Insert Into return the Autonumber - 4/1/2005 8:18:36 AM   
  larryw24

 

Posts: 8
Score: 0
Joined: 4/1/2005
From: USA
Status: offline
I add a row to an Access Database with code like the following:

sql = "INSERT INTO EmailChanges (Oldmail, Newmail) VALUES ("
sql = sql & strOldmail & ", " & strNewmail & ")"
my_Conn.Execute sql

The table EmailChanges has a field called "ChangeNumber" which is an AutoNumber field. This number is allocated by the database and placed into the new row.

How can my ASP script add the new row in a way that the script learns what ChangeNumber has been allocated?
 
 
Post #: 1
 
 Re: SQL Insert Into return the Autonumber - 4/1/2005 9:53:10 AM   
  larryw24

 

Posts: 8
Score: 0
Joined: 4/1/2005
From: USA
Status: offline
I think I found the answer. I'm not sure if I have to use Transactions to prevent anyone else from modifying that table between my two statements, but it seems safe. Here's the code without error checking:

my_Conn.BeginTrans
sql = "INSERT INTO EmailChanges (Oldmail, Newmail) VALUES ("
sql = sql & strOldmail & ", " & strNewmail & ")"
my_Conn.Execute sql
Set rs = myConn.Execute("Select @@Identity as x")
my_conn.CommitTrans
Response.Write "<p>New ID value=" & rs.Fields("x") & "</p>"

(in reply to larryw24)
 
 
Post #: 2
 
 Re: SQL Insert Into return the Autonumber - 4/4/2005 7:56:14 PM   
  vietnamblood

 

Posts: 14
Score: 0
Joined: 3/28/2005
From:
Status: offline
I think I found the answer. I'm not sure if I have to use Transactions to prevent anyone else from modifying that table between my two statements, but it seems safe. Here's the code without error checking:

my_Conn.BeginTrans
sql = "INSERT INTO EmailChanges (Oldmail, Newmail) VALUES ('"
sql = sql & strOldmail & "', '" & strNewmail & "')"
my_Conn.Execute sql
Set rs = myConn.Execute("Select @@Identity as x")
my_conn.CommitTrans
Response.Write "<p>New ID value=" & rs.Fields("x") & "</p>"

(in reply to larryw24)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> ASP >> SQL Insert Into return the Autonumber Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts