Results 1 to 5 of 5

Thread: [RESOLVED] Getting the ID from an Inserted record created by connection.execute

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2010
    Posts
    64

    Resolved [RESOLVED] Getting the ID from an Inserted record created by connection.execute

    Greetings

    I am creating a new record in an Auto Increment sql table (by copying an existing record) and I want to retrieve the new id created.
    So far I have got the following, which creates the record nicely, but I dont know how to retrieve the New id

    Private Sub CopyRecord(OriginalTable As String, ByRef ID As Integer)
    myconnection.Execute("SELECT * INTO TempTable FROM " & OriginalTable & " WHERE ID = " & ID)
    myconnection.Execute("ALTER TABLE TempTable DROP COLUMN ID")
    myconnection.Execute("INSERT INTO " & OriginalTable & " OUTPUT INSERTED.ID as ID SELECT * FROM TempTable")
    myconnection.Execute("DROP TABLE TempTable")
    End Sub

    How can I get the subroutine to return the new Id in the subroutines ID parameter
    Thank you for your help

    (Note: myconnection is an ADODB.Connection and has been established earlier by the following
    ConnectionString = "Driver={SQL Server};Server=" & strSQLPath & ";Database=TESTING;Trusted_Connection=yes"
    myconnection.Open(ConnectionString)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting the ID from an Inserted record created by connection.execute

    I would question why you're using ADO in the first place, rather than using ADO.NET. Regardless, you can execute a query to get the value of SCOPE_IDENTITY within the same scope or @@IDENTITY in any scope.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2010
    Posts
    64

    Re: Getting the ID from an Inserted record created by connection.execute

    Thank you for your reply. As I only want the ID in this case, SCOPE_IDENTITY would be fine. I believe that I add that to the end of my SQL Statement, so that would be like the following?
    myconnection.Execute("INSERT INTO " & OriginalTable & " SELECT * FROM TempTable; Select Scope_Identity()")

    But i dont understand is how to return the resultant value in my variable ID ?

    Googling other sites I see some suggestions such as ID = CInt(myCommand.ExecuteScalar()), but I dont have the ExecuteScaler option on myconnection

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting the ID from an Inserted record created by connection.execute

    Quote Originally Posted by Anthonynz View Post
    Googling other sites I see some suggestions such as ID = CInt(myCommand.ExecuteScalar()), but I dont have the ExecuteScaler option on myconnection
    That's because ExecuteScalar is a member of an ADO.NET command object. Again, why aren't you using ADO.NET in the first place? If you're determined to use ADO then you would get the ID from that query the same way you would usually get a value from a query. If you don't know how to do that then you certainly can't use existing knowledge as a reason for using ADO so, yet again, why not use ADO.NET?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2010
    Posts
    64

    Re: Getting the ID from an Inserted record created by connection.execute

    I am modifying an existing program and it uses ADO....best not to change any more than necessary

    After more googling I found the answer.
    For anyone else who has a similar problem, simply save the output into another recordset as follows.

    Private Sub CopyRecord(OriginalTable As String, ByRef ID As Integer)
    myconnection.Execute("SELECT * INTO TempTable FROM " & OriginalTable & " WHERE ID = " & Val(ID))
    myconnection.Execute("ALTER TABLE TempTable DROP COLUMN ID")
    rsTemp = myconnection.Execute("INSERT INTO " & OriginalTable & " OUTPUT INSERTED.* SELECT * FROM TempTable")
    ID = CStr(rsTemp.Fields("ID").Value)
    myconnection.Execute("DROP TABLE TempTable")
    End Sub

    (Where rsTemp has been previously Dimensioned as an ADODB.Recordset)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width