Results 1 to 2 of 2
  1. #1
    Ira is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2011
    Location
    South Florida
    Posts
    29

    Receive value back from SQL Server Stored Procedure

    Calling the store procedure from Access:

    Dim qdef As DAO.QueryDef
    Set qdef = CurrentDb.CreateQueryDef("")
    qdef.Connect = CurrentDb.TableDefs("Clients").Connect
    qdef.SQL = "EXEC dbo.AddClient 'one' , 'two', 'three', 'four'" 'is there a better way to pass the argments? Recordset?
    qdef.ReturnsRecords = False ''avoid 3065 error
    qdef.Execute


    ''''How do I get back the value of last_id_inserted ??


    The stored procedure:

    ALTER PROCEDURE [dbo].[AddClient]
    -- Add the parameters for the stored procedure here


    @BillName varchar(50),
    @BillStreet varchar(50),
    @BillCity varchar(50),
    @BillState varchar(2)
    AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


    -- Insert statements for procedure here
    INSERT INTO Clients
    (BillName, BillStreet, BillCity, BillState)
    VALUES
    (@BillName, @BillStreet, @BillCity, @BillState)

    END
    DECLARE @last_id_inserted int;


    SET @last_id_inserted = SCOPE_IDENTITY();
    RETURN @last_id_inserted;

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I've used an ADO Command object in this type of situation. Your SP does return a value, so an untested idea is to change the ReturnsRecords to true and open the query instead of executing it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Stored Procedure in MS SQL Server from Access
    By jaryszek in forum Access
    Replies: 7
    Last Post: 01-17-2018, 05:58 AM
  2. Pass Text Box Value To SQL Server Stored Procedure
    By Juan4412 in forum Programming
    Replies: 1
    Last Post: 04-11-2017, 06:58 AM
  3. Replies: 2
    Last Post: 06-01-2016, 08:52 AM
  4. Replies: 3
    Last Post: 03-20-2012, 10:00 AM
  5. Replies: 3
    Last Post: 05-23-2010, 05:23 AM

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
  •  
Other Forums: Microsoft Office Forums