BY CATEGORY▼
- VIDEOS HOME PAGE
- .NET (14)
- Business Intelligence (40)
- Integration Services (19)
- Macros and Programming (82)
- Microsoft Excel (70)
- Microsoft Office (92)
- Miscellaneous (1)
- Power BI (35)
- Power Platform (35)
- Python (31)
- Report Builder (107)
- Reporting Services (113)
- SQL (42)
- Visual Basic for Applications (215)
- Visual C# (14)
VBA CATEGORIES▼
- Excel VBA - Basics (24)
- VBA User Forms (22)
- Excel VBA - pivot tables (9)
- Excel VBA - charts (6)
- VBA - advanced (14)
- VBA - working with files (12)
- VBA - linking applications (12)
- VBA - working with Outlook (14)
- Built-in VBA functions (9)
- VBA - working with data (57)
- VBA - scraping websites (25)
- VBA - working with shapes (5)
- VBA - classes and structures (6)
VBA - WORKING WITH DATA VIDEOS▼
- Excel VBA Part 30 - Querying a Database with ADO
- Excel VBA Part 31 - Modifying Data with ADO Recordsets
- Excel VBA Part 32 - Executing SQL Commands with ADO
- Excel VBA Part 56.1 - Connect to SQL Server using ADO
- Excel VBA Part 56.2 - Get Data from SQL Server with ADO Recordsets
- Excel VBA Part 56.3 - Read and Execute SQL Query Files
- Excel VBA Part 56.4 - Return Multiple Result Sets from SQL Queries
- Excel VBA Part 56.5 - Basic ADO Commands with SQL Server
- Excel VBA Part 56.6 - ADO Commands and Parameters
- Excel VBA Part 56.7 - ADO Commands and Stored Procedures
- Excel VBA Part 58.1 - SQL for Excel Files - Basic Select Statements
- Excel VBA Part 58.2 - SQL for Excel Files - Sorting Rows in a Query
- Excel VBA Part 58.3 - SQL for Excel Files - Selecting the Top N Rows
- Excel VBA Part 58.4 - SQL for Excel Files - Selecting All or Distinct Rows
- Excel VBA Part 58.5 - SQL for Excel Files - Basic Criteria in Queries
- Excel VBA Part 58.6 - SQL for Excel Files - Text Criteria and the Like Operator
- Excel VBA Part 58.7 - SQL for Excel Files - Basic Calculated Columns
- Excel VBA Part 58.8 - SQL for Excel Files - Conditional Functions
- Excel VBA Part 58.9 - SQL for Excel Files - Nulls in Expressions
- Excel VBA Part 58.10 - SQL for Excel Files - Date Expressions
- Excel VBA Part 58.11 - SQL for Excel Files - Text Expressions
- Excel VBA Part 58.12 - SQL for Excel Files - Aggregation Functions
- Excel VBA Part 58.13 - SQL for Excel Files - Grouping Rows
- Excel VBA Part 58.14 - SQL for Excel Files - Criteria in the Having Clause
- Excel VBA Part 58.15 - SQL for Excel Files - Pivoting Data (Crosstab Queries)
- Excel VBA Part 58.16 - SQL for Excel Files - Basic Union Queries
- Excel VBA Part 58.17 - SQL for Excel Files - Union Queries with Total Rows
- Excel VBA Part 58.18 - SQL for Excel Files - Union Queries from Multiple Files
- Excel VBA Part 58.19 - SQL for Excel Files - Inner Joins
- Excel VBA Part 58.20 - SQL for Excel Files - Outer Joins
- Excel VBA Part 58.21 - SQL for Excel Files - Constructing Full Outer Joins
- Excel VBA Part 58.22 - SQL for Excel Files - Join Worksheets from Multiple Files
- Excel VBA Part 58.23 - SQL for Excel Files - Merge Worksheets Side by Side
- Excel VBA Part 58.24 - SQL for Excel Files - Consolidate Worksheets using Derived Tables
- Excel VBA Part 58.25 - SQL for Excel Files - Basic Subqueries
- Excel VBA Part 58.26 - SQL for Excel Files - Nested Subqueries
- Excel VBA Part 58.27 - SQL for Excel Files - Correlated Subqueries
- Excel VBA Part 58.28 - SQL for Excel Files - Inserting Data into an Existing Sheet
- Excel VBA Part 58.29 - SQL for Excel Files - Selecting Data into a New Sheet or Workbook
- Excel VBA Part 58.30 - SQL for Excel Files - Updating Existing Data
- Excel VBA Part 58.31 - SQL for Excel Files - Split a Table into Separate Sheets
- How do I pass Japanese text to a stored procedure parameter in VBA?
- How do I get data from a closed Excel file using VBA?
- How do I loop through worksheets in a closed workbook with VBA?
- How do I get data from multiple closed Excel files using VBA?
- How do I get data from multiple workbooks with one query in VBA?
- How do I loop through worksheets in multiple closed workbooks in VBA?
- How do I populate a listbox using an ADO recordset in VBA?
- How do I get the column names from an ADO recordset?
- Excel VBA - How do I get data from a CSV file using ActiveX Data Objects
- How do I add a filename to the results of an ADODB recordset?
- How do I populate an array with an ADODB recordset?
- How do I count the rows in an ADODB recordset?
- How do I find the data type of a column in an ADODB recordset?
- How do I refer to a field name with a dot in an ADO SQL query?
- How do I import tab delimited files with ActiveX Data Objects?
- How do I sort tables on multiple sheets in Excel VBA?
VBA - working with data videos | Excel VBA Part 56.7 - ADO Commands and Stored Procedures
Posted by Andrew Gould on 05 July 2019
This video shows you how to call and pass values to stored procedures from a SQL Server database using ADO commands. You'll learn how to reference parameters by name, how to pass values to parameters and how to retrieve a result set using a recordset object. You'll also see how to extract information from a stored procedure using output parameters.
See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.
This video has the following accompanying files:
File name | Type | Description |
---|---|---|
01 Create Movies Database 2016.sql | SQL query | |
02 Create Movie Procedure Single Parameter.sql | SQL query | |
03 Create Movie Procedure Multi Parameters.sql | SQL query | |
04 Create Movie Procedure Output Parameters.sql | SQL query | |
Stored Procedures and Parameters with ADO.xlsm | Excel workbook with macros |
Click to download a zipped copy of the above files.
There are no exercises for this video.
Making a video bigger
You can increase the size of your video to make it fill the screen like this:

Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown at its bottom right-hand corner.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
Improving the quality of a video
To improve the quality of a video, first click on the Settings icon:

Make sure you're playing your video so that the icons shown appear, then click on this gear icon at the bottom right-hand corner.
Choose to change the video quality:

Click on Quality as shown to bring up the submenu.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):

Don't choose the HD option unless you have a fast enough connection speed to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
Andrew,
I have followed your video and everything works fine.
However, I would like to find out the number of records in the recordset because if there are more than 1m records, they can't be returned to the worksheet and so I would like to do some filtering to the results.
I added these few lines to your existing code:
Dim j
j = rs.GetRows(rs.RecordCount)
Dim myarray() As Variant
myarray = j
So altogether, the entire code is as follows:
Sub GetDataFromStoredProcedure()
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
cn.ConnectionString = _
"Provider=SQLNCLI11;Server=MYLAPTOPNAME\SQLEXPRESS;Database=Movies;Trusted_Connection=yes"
cn.Open
Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "spFilmList"
Set rs = cmd.Execute
Dim j
j = rs.GetRows(rs.RecordCount)
Dim myarray() As Variant
myarray = j
WriteToSheets rs
rs.Close
cn.Close
End Sub
Private Sub WriteToSheets(ResultSet As ADODB.Recordset)
Dim ws As Worksheet
Dim i As Integer
Set ws = ThisWorkbook.Worksheets.Add
ws.Select
For i = 0 To ResultSet.Fields.Count - 1
ws.Cells(1, i + 1).Value = ResultSet.Fields(i).Name
Next i
ws.Range("A2").CopyFromRecordset ResultSet
End Sub
Why is it that adding those few lines makes the code return no results onto the worksheet (though the field names are retirned)?
Thanks
Hi Duggie,
Here's the documentation for the GetRecords method you're using https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/getrows-method-ado?view=sql-server-ver15
The important part is:
After you call GetRows, the next unread record becomes the current record, or the EOF property is set to True if there are no more records.
Rather than doing that, you can set your recordset to use a client side cursor and then just just the RecordCount property to determine how many rows you have retrieved:
Sub GetDataFromStoredProcedure()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
cn.ConnectionString = _
"Provider=SQLNCLI11;Server=.\SQL2017;Database=Movies;Trusted_Connection=yes"
cn.Open
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Source = "spFilmList"
rs.CursorLocation = adUseClient
rs.Open
Debug.Print rs.RecordCount
If rs.RecordCount > 1000000 Then
'code to apply recordset filters
Else
WriteToSheets rs
End If
rs.Close
cn.Close
End Sub
Hope that helps!
Andrew,
Thanks for your suggestion.
Apologies for the delay in replying, been side tracked with job-hunting, successfully started but then cut owing to a certain worldwide incident!