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

Is there a way to get stored procedures from a SQL Server 2005 Express
Is there some way to get a value from the last inserted row? I
Is there another way to get the last child of a div element other
Is there a way that I can get the last value (based on the
Is there a way to get a list ordered by two fields, say last
Is there a way to get the tests inside of a TestCase to run
Is there any way to get the ID of the element that fires an
Is there a way to get the current xml data when we make our
Is there any way to get Python to use my ActiveTcl installation instead of
Is there a way to get a DrawingContext (or something similar) for a WriteableBitmap

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.