Solved

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

Posted on 2015-02-11
6
67 Views
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?
0
Comment
Question by:klyles95
  • 3
6 Comments
 
LVL 57

Accepted Solution

by:
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
http://support.microsoft.com/kb/184749

 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).

Jim.
0
 
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.

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

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

can instead be:

exec  <stored procedure>  param1, param2,

As well.

Jim.
0
 
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

0
 
LVL 48

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.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Viewers will learn how the fundamental information of how to create a table.

807 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