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
76 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 58

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 58
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 58
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 51

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 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