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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Viewers will learn how the fundamental information of how to create a table.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now