How do I call a SQL Server stored procedure from Access 2010 VBA using DAO to return a dataset?

Posted on 2015-02-11
Last Modified: 2016-05-22
I have an Access 2010 front end with a SQL Server 2012 backend. I have a listbox that retrieves  a list of people. Right now the performance in retrieving the records has been slow. I would like to change the process by using a stored procedure call using DAO from Access VBA. Does anyone have an example of how to do this?
Question by:klyles95
  • 3
LVL 57

Accepted Solution

Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 40603473
This MSKB article shows one way to do it using the OpenRecordset method:

How To Call Stored Procedures Using Data Access Objects

 another method which is a slight variation of that is to use a QueryDef() looking something like this:

          Dim sql As String, sParam As String
          Dim qdf As QueryDef
          Dim i As Integer, result
          Dim Tape As Control, CalcPrice As Double

10        On Error GoTo SoPriceCalc_SQL_Err

20        For i = 0 To 24
30            sParam = sParam & "?,"
40        Next i
50        sParam = sParam & "?"
60        sql = "{? = call qrySoPriceCalc(" & sParam & ")}"
70        Set qdf = GetRDOCConnectCompany().CreateQueryDef("", sql)
80        With qdf
90            .Parameters(0).Direction = dbParamReturnValue
100           .Parameters("@PriceBase").Direction = dbParamOutput
110           .Parameters("@CalcPrice").Direction = dbParamOutput
120           .Parameters("@PromoID").Direction = dbParamOutput
130           .Parameters("@BrkID").Direction = dbParamOutput
140           .Parameters("@BrkAdj").Direction = dbParamOutput
150           .Parameters("@BrkAdjType").Direction = dbParamOutput
160           .Parameters("@PriceType1").Direction = dbParamOutput
170           .Parameters("@PriceType2").Direction = dbParamOutput
180           .Parameters("@PriceType3").Direction = dbParamOutput
190           .Parameters("@PriceTemp1").Direction = dbParamOutput
200           .Parameters("@PriceTemp2").Direction = dbParamOutput
210           .Parameters("@PriceTemp3").Direction = dbParamOutput
220           .Parameters("@CalcAdj1").Direction = dbParamOutput
230           .Parameters("@CalcAdj2").Direction = dbParamOutput
240           .Parameters("@CalcAdj3").Direction = dbParamOutput

250           .Parameters("@ItemID") = PcItemID
260           .Parameters("@LocID") = PcLocID
270           .Parameters("@Uom") = PcUom
280           .Parameters("@SerNum") = PcSerNum
290           .Parameters("@Qty") = PcQty
300           .Parameters("@CustLevel") = PcCustLevel
310           .Parameters("@PriceID") = PcPriceID
320           .Parameters("@Date") = PcDate
330           .Parameters("@gPrecUCost") = gPrecUCost
340           .Parameters("@gPrecUPrice") = gPrecUPrice
350           .Parameters("@DiscountsAllowed") = 1
              '.Parameters("@DiscountsAllowed") = Abs(GetSoGlobal("gSoDiscPricingSerYn"))
360           .Execute

370           Select Case .Parameters(0)
                  Case 0
380                   PcPromoID = Trim$(.Parameters("@PromoID") & "")
390                   PcCalcPrice = .Parameters("@CalcPrice")
400               Case 10
                      'result = GenGetMsg("SoItemRecNotFound", " ", " ")
                      'result = MsgBox(gMsg, gMsgType, gMsgTitle)
410               Case 20
                      'MsgBox ("Item Record Not Found for this Location")
420               Case 30, 40
                      'MsgBox ("UOM Record Not Found for this Item")
430           End Select

Open in new window

 but setting the ReturnsRecord property to True (the example above does not because it's just passing parameters back and forth).

LVL 57
ID: 40603479
Note that it's still a pass through query however and the resulting recordset is read only.

If you want to do any updating, you'd need to construct another SQL statement to do the updates.

LVL 57
ID: 40603505
One other comment; the pass through of:

{? = call qrySoPriceCalc(" & sParam & ")}

can instead be:

exec  <stored procedure>  param1, param2,

As well.

LVL 12

Assisted Solution

by:James Elliott
James Elliott earned 250 total points
ID: 40603578
This is how I do it:

Dim qdf As dao.QueryDef, rst As dao.Recordset

Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = CurrentDb.TableDefs("tblExample").Connect 'A table already linked to the SQL server, to save hard-typing connection details
qdf.SQL = "EXEC dbo.spExample " & pParamExample1
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset(dbOpenSnapshot)

Open in new window

LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40613698
kyles95, you still need help for this question? If not, please accept one or more questions as a solution for your issue.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question