We are SQL Server Experts. Call Us TODAY! +17738095456 or email us at sales@itimpact.com
  • The Team
  • Our Services
    • Dashboards Developers
    • Microsoft SQL Server
    • Microsoft SQL Server hosting for Access
    • SQL Server Integration Services (SSIS)
    • SQL Server Reporting Services (SSRS)
    • SQL Server Data Quality Services (DQS)
    • Microsoft Access
    • Microsoft VB.Net
    • Cross Functional / Business Unit Integration
    • Financial Statement Consolidation
    • Website Design
  • Case Studies
    • Automatic Reporting for Security Services Firm
    • Amazon API Integration with Access
    • Custom Quoting and Proposal Solution
    • Data Quality Analysis for Government Program
    • New Claim System Reduces Processing Time
    • Pipe Tracking Software
    • Product Search Analysis
    • Safety Training, Incident and Accident Tracking Tool
    • Union Dues and Membership Management System
  • Blog
  • Contact
  • The Team
  • Our Services
    • Dashboards Developers
    • Microsoft SQL Server
    • Microsoft SQL Server hosting for Access
    • SQL Server Integration Services (SSIS)
    • SQL Server Reporting Services (SSRS)
    • SQL Server Data Quality Services (DQS)
    • Microsoft Access
    • Microsoft VB.Net
    • Cross Functional / Business Unit Integration
    • Financial Statement Consolidation
    • Website Design
  • Case Studies
    • Automatic Reporting for Security Services Firm
    • Amazon API Integration with Access
    • Custom Quoting and Proposal Solution
    • Data Quality Analysis for Government Program
    • New Claim System Reduces Processing Time
    • Pipe Tracking Software
    • Product Search Analysis
    • Safety Training, Incident and Accident Tracking Tool
    • Union Dues and Membership Management System
  • Blog
  • Contact
Toggle panel Social icons
SQL Server

Why you should always use a recordset when executing stored procedures

I’ve talked about using a recordset with stored procedures before, but this time I wanted to emphasize the importance of always using a recordset when executing a stored procedure from Accerss VBA.

Command Object does not cut it

Usually a programmer will use a command object to execute a stored procedure that does not return records. The format is:

ExecuteMyCommand “Exec usp_TestProcedure 1”

Where the procedure name is usp_TestProcedure and the number 1 is an argument. ExecuteMyCommand is from my EasyADODB library. The problem with this approach is the lack of feedback you get back from SQL Server: Did the procedure execute ok? Where all the transactions completed? There is no easy way to return the results back to Access unless you use additional code.

Use a Recordset Instead

When you use a recordset to execute the stored procedure, you can easily return values back from your stored procedure and determine if all is well. First, here is the stored procedure we are going to use for testing:

CREATE PROCEDURE usp_TestProcedure

@TestError Int

AS

IF @TestError = 1
Select 1 AS MyResult
ELSE
Select 0 AS MyResult
END

usp_TestPrcedure will return a recordset with just one field: MyResult. Here is an example that returns 1 from it:

Dim rs As ADODB.Recordset
Dim strSQL As String

strSQL = “Exec usp_TestProcedure 1”
OpenMyRecordset rs, strSQL
With rs
MsgBox “Your procedure returned ” & !MyResult
End With

Start using Recordsets with all of your stored preocedures today!

No Comments on “Why you should always use a recordset when executing stored procedures”

  1. Tony High May 23, 2013 at 12:51 am

    I like to use an output variable and then fill it with ERROR_MESSAGE() so that I can then display the actual error. Otherwise I am troubleshooting in the dark. It adds an extra step to your command but it gives you all the info you need or you can even put in your own message.

    Reply

Click here to cancel reply.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

XHTML: You can use these tags <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts

  • 23 Jan. 2019 7 essential SQL skills that can help you command a higher paycheck

  • 18 Jan. 2018 Quick Guide on Importing Data from SQL Server (or SQL Azure) to Cosmos DB

  • 10 Jan. 2018 Top 7 Questions about Basic Availability Groups

  • 04 Jan. 2018 An introduction to mssql-cli, a command-line client for SQL Server

  • Share and work together on the intranet with SharePoint Team Sites

ITImpact

About IT Impact, Inc.

IT Impact, Inc.

IT Impact, Inc. is a minority-owned software company specializing in custom software solutions. We work with SQL Server, Microsoft Access, web applications, and much more.

Our staff is top of the line and includes two Microsoft MVPs!

Twitter

Tweets by @ITImpactInc

Facebook

© ITImpact. Webdesign by GreatCustomWebsites.com