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
75 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 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 50

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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