Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1027427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:14:22+00:00 2026-05-16T12:14:22+00:00

is there a way to get last record inserted without stored procedure in a

  • 0

is there a way to get last record inserted without stored procedure in a multi-user environment?

i am connected to sql server through vba. i insert some records and call:

SELECT SCOPE_IDENTITY()

this is not working. it is returning null every time.

here is the entire code:

Dim sqlstring1 As String
sqlstring1 = "delete from batchinfo where datapath='" & dpath & "' and analystname='" & aname & "' and reportname='" & rname & "' and batchstate='" & bstate & "'"



Dim rowid_batchinfo As String
rs.Filter = "datapath='" + dpath + "' and analystname='" + aname + "' and reportname='" + rname + "' and batchstate='" + bstate + "'"
If Not rs.EOF Then
    rowid_batchinfo = rs.Fields("rowid")
    cn.Execute "delete from batchinfo where rowid=" + rowid_batchinfo
    cn.Execute "delete from calibration where rowid='" + rowid_batchinfo + "'"
    cn.Execute "delete from qvalues where rowid='" + rowid_batchinfo + "'"
End If


With rs
    .AddNew ' create a new record
    ' add values to each field in the record
    .Fields("datapath") = dpath
    .Fields("analysistime") = atime
    .Fields("reporttime") = rtime
    .Fields("lastcalib") = lcalib
    .Fields("analystname") = aname
    .Fields("reportname") = rname
    .Fields("batchstate") = bstate
    .Fields("instrument") = instrument
    .Update ' stores the new record


End With



Set rs = cn.Execute("SELECT SCOPE_IDENTITY()", , adCmdText)

Set rs = rs.NextRecordset

the question is how do i get the last record inserted in a multi-user environment?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:14:23+00:00Added an answer on May 16, 2026 at 12:14 pm

    From the docs:

    Returns the last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

    This means that you should send SCOPE_IDENTITY in the same batch (i. e. SQL call) with your insert query.

    This is not possible with methods like AddNew, so you should use an explicit INSERT statement followed by SELECT SCOPE_IDENTITY.

    Something like this:

    SQL = "INSERT INTO mytable (datapath, analysistype, …) VALUES (@datapath, @analysistime, …) SELECT SCOPE_IDENTITY()"
    Set cmd = Server.CreateOject("ADODB.Command")
    With cmd
          .CommandType = adCmdText
          .CommandText = SQL
          .Parameters.Append .CreateParameter("@datapath", adVarWChar, adParamInput,, dpath)
          …
    End With
    Set rs = cmd.Execute
    Set rs = rs.NextRecordset
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was testing out the SQL Server 2008 change data capture feature on a
I'm using the same SQL pattern over and over, and I know there has
I have a database of last names, first names, addresses, etc. I am trying
I'm writing a Function that pulls Records from a DataBase using LINQ to get
I'm re-evaluating my model and way I am trying to solve a particular problem.
I have looked at various example of the SQL Merge statement.. all of which
I'm having a hard time wrapping my head around this Django query. I could
My code runs fine for sqllite locally, but when I push to heroku I
Hey all, I seem to have these types of methods everywhere. The method below
I am doing a join between two tables and want to select the columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.