Calling Parameters for a pass through query from a form IN MS Access 2003

Posted on 2011-02-18
Last Modified: 2012-05-11
Hi Experts

I am executing a stored proc as a pass through query in sql but am having difficulty passing the parameters to the sp at run time from an Ms Access form.  I have used various examples that I have found on this and other forums but am just not winning.  The sp is Called upAmbledown and there are Param1 and Param2 that will get pulled from controls on the Access form.  I would like to do an ADO connection on the form and then set up the connection to the query after it has executed using the params as the WHERE and then open a form called frmBusiness and set the source of the form to the connection

Your help will be appreciated.
Question by:caandal
  • 6
  • 3
LVL 84
ID: 34924818
You can't add an ADO connection "to a form". An ADO connection is just an object that you build using VBA. That connection can be used to connect to a database.

You CAN set the Recordset of an Access form to a Recordset, if that's what you mean.

To run your Stored Proc, you can do this:

Dim con as ADODB.Connection
Set con = New ADODB.Connection

con.Open "Your ConnectionString"

con.Execute "EXEC upAmbleDown @Param1=" & Me.YourFirstControl & ", @Param2=" & Me.YourSecondControl

This should fire off your stored procedure, assuming you have the necessary permissions on the server to do this. Note that this assumes the parameters are numeric; if they Text or Date values, do this:

con.Execute "EXEC upAmbleDown @Param1='" & Me.YourFirstControl & "', @Param2='" & Me.YourSecondControl & "'"

Basically, I'ved enclosed the value of the parametrs in single quotes ( ' )

After doing this, you can then open a Recordset based on that connection:

Dim rst AS ADODB.Recordset
Set rst = New ADODB.Recordset

rst.CursorLocation = adUseClient

rst.Open "Your SElect Statement here", con

Now you can open your form, and set the Recordset property:

DoCmd.OpenForm "YourFormName"

Set Forms("YourFormName").Recordset = rst


Author Comment

ID: 34927617
Hi LSM - Long time no chat hope that life is treating u well.  Your code makes absolute sense and is easy to read and understand. - Thanks for that
I am getting an error on rst.Open and suspect that it may have something to do with the connection string statement itself please have a look at the screenshots and see if you can pick anything up.  Screen shot - error  Screen shot error occurring in this line
LVL 84

Accepted Solution

Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 34928796
You need to provide a valid SQL statement. Your "strConnection" string is a connection string that's used to initialize your ADODB.Connection object.

A valid SQL string would be something like:


Does your Stored Proc return a Recordset? If so, then you can "open" it with your recordset:

Dim con as ADODB.Connection
Dim rst As ADODB.Connection

Set con = New ADODB.Connection

con.Open "Your ConnectionString"

Set rst = New ADODB.Connection
rst.Open "EXEC upAmbleDown @Param1=" & Me.YourFirstControl & ", @Param2=" & Me.YourSecondControl, con


Author Comment

ID: 34931386
upUmbledown returns a query with the two parameters.  The statement that you have made above does not make sense to me.  Why are you opening a table for example and then returning another recordset with the stored proc?
The query that is being run is a complicated one.
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.


Author Comment

ID: 34931395
maybe I should just run a view with the 2 parameters instead of a stored procedure?

Author Comment

ID: 34931400
Here is the stored proc that is being executed
USE [ComplimedSQL]
/****** Object:  StoredProcedure [dbo].[upAmbledown]    Script Date: 02/18/2011 18:43:03 ******/
ALTER PROCEDURE [dbo].[upAmbledown] 
@MyParam1 NVarchar(50),
@MyParam2 Varchar(50) 
SELECT     dbo.MemberStatus.MemberStatusID, dbo.MemberStatus.MembershipStatus, dbo.Policy.PolicyID, dbo.Policy.PolicyNo, dbo.People.Initial, dbo.People.FirstName, 
                      dbo.People.SURNAME, dbo.People.IDNumber, dbo.People.DOB, dbo.Policy.DateJoined, dbo.Policy.DateResigned, dbo.Policy.CompanyID, dbo.Company.Company, 
                      dbo.Policy.Paypoint, dbo.Brokerage.BrokerageID, dbo.Brokerage.Brokerage, dbo.Broker.Broker, dbo.NewPolicyProductDetails.AmtDue, 
                      dbo.Product.PRDescription AS TARIFF, dbo.NewPolicyProductDetails.TariffAmt, Product_2.PRDescription AS COPAYMENTS, 
                      dbo.NewPolicyProductDetails.CoPaymentsAmt, Product_4.PRDescription AS INHOSPITAL, dbo.NewPolicyProductDetails.InHospitalAmt, 
                      Product_11.PRDescription AS CANCER, dbo.NewPolicyProductDetails.CancerAmt, Product_8.PRDescription AS [HOSPITAL ADMISSION], 
                      dbo.NewPolicyProductDetails.HospAdmissionAmt, Product_9.PRDescription AS [PREMIUM WAIVER], dbo.NewPolicyProductDetails.PremiumWaiverAmt, 
                      Product_1.PRDescription AS COPAYCANCER, dbo.NewPolicyProductDetails.CopayCancerAmt, Product_7.PRDescription AS CASHBACK, 
                      dbo.NewPolicyProductDetails.PerHospBenAmt, Product_12.PRDescription AS [HOSPITAL EXCESS], dbo.NewPolicyProductDetails.HospExcessAmt, 
                      Product_10.PRDescription AS KEYCARE, dbo.NewPolicyProductDetails.KeyCareAmt, Product_3.PRDescription AS FUNERAL, 
                      dbo.NewPolicyProductDetails.FuneralAMT, Product_6.PRDescription AS PACKAGES, dbo.NewPolicyProductDetails.PckgPlansAmt, dbo.Policy.TelW, dbo.Policy.Mobile, 
                      dbo.Policy.[E-mail], dbo.Policy.ReasonResigned, dbo.MedicalAids.MedicalAid, dbo.Policy.MANumber, dbo.Policy.PolYearID, dbo.vPolicyTotals.Premium, 
                      dbo.vPolicyTotals.Risk, dbo.vPolicyTotals.BrokerComm, dbo.vPolicyTotals.BrokerFee, dbo.vPolicyTotals.PolicyFee, dbo.vPolicyTotals.AdminFee, dbo.Policy.Addr1, 
                      dbo.Policy.Addr2, dbo.Title.Title, dbo.Policy.CITY, dbo.Suburb.SuburbID, dbo.Policy.PROVINCE, dbo.Policy.[POSTAL CODE], dbo.Suburb.Suburb, 
                      dbo.Brokerage.[BROKER TEL], dbo.vPolicyTotals.PolicyID AS Expr1,@MyParam2 AS 'Month'
FROM         dbo.NewRates AS Rates_7 LEFT OUTER JOIN
                      dbo.Product AS Product_7 ON Rates_7.ProductID = Product_7.ProductID RIGHT OUTER JOIN
                      dbo.NewRates AS Rates_6 LEFT OUTER JOIN
                      dbo.Product AS Product_6 ON Rates_6.ProductID = Product_6.ProductID RIGHT OUTER JOIN
                      dbo.Product RIGHT OUTER JOIN
                      dbo.NewRates ON dbo.Product.ProductID = dbo.NewRates.ProductID RIGHT OUTER JOIN
                      dbo.NewRates AS Rates_2 RIGHT OUTER JOIN
                      dbo.NewRates AS Rates_4 RIGHT OUTER JOIN
                      dbo.Product AS Product_1 RIGHT OUTER JOIN
                      dbo.NewRates AS Rates_11 RIGHT OUTER JOIN
                      dbo.Title RIGHT OUTER JOIN
                      dbo.People ON dbo.Title.TitleID = dbo.People.TitleID RIGHT OUTER JOIN
                      dbo.Company RIGHT OUTER JOIN
                      dbo.MemberStatus RIGHT OUTER JOIN
                      dbo.Suburb RIGHT OUTER JOIN
                      dbo.Policy INNER JOIN
                      dbo.vPolicyTotals ON dbo.Policy.PolicyID = dbo.vPolicyTotals.PolicyID RIGHT OUTER JOIN
                      dbo.NewPolicyProductDetails ON dbo.Policy.PolicyID = dbo.NewPolicyProductDetails.PolicyID ON dbo.Suburb.SuburbID = dbo.Policy.SuburbID LEFT OUTER JOIN
                      dbo.Brokerage ON dbo.Policy.BrokerageID = dbo.Brokerage.BrokerageID ON dbo.MemberStatus.MemberStatusID = dbo.Policy.MemberStatusID ON 
                      dbo.Company.CompanyID = dbo.Policy.CompanyID ON dbo.People.PeopleID = dbo.Policy.PrincipalID ON 
                      Rates_11.RateID = dbo.NewPolicyProductDetails.[CancerID-O] LEFT OUTER JOIN
                      dbo.NewRates AS Rates_1 ON dbo.NewPolicyProductDetails.[CopayCancerID-P] = Rates_1.RateID ON Product_1.ProductID = Rates_1.ProductID ON 
                      Rates_4.RateID = dbo.NewPolicyProductDetails.[InHospitalID-N_OR_Q] ON Rates_2.RateID = dbo.NewPolicyProductDetails.[CoPaymentsID-M] LEFT OUTER JOIN
                      dbo.MedicalAids ON dbo.Policy.MedicalAidID = dbo.MedicalAids.MedicalAidID LEFT OUTER JOIN
                      dbo.Broker ON dbo.Policy.BrokerID = dbo.Broker.BrokerID ON dbo.NewRates.RateID = dbo.NewPolicyProductDetails.TariffID LEFT OUTER JOIN
                      dbo.NewRates AS Rates_3 LEFT OUTER JOIN
                      dbo.Product AS Product_3 ON Rates_3.ProductID = Product_3.ProductID ON dbo.NewPolicyProductDetails.FuneralID = Rates_3.RateID ON 
                      Rates_6.RateID = dbo.NewPolicyProductDetails.PckgPlansID ON Rates_7.RateID = dbo.NewPolicyProductDetails.PerHospBenID LEFT OUTER JOIN
                      dbo.NewRates AS Rates_8 LEFT OUTER JOIN
                      dbo.Product AS Product_8 ON Rates_8.ProductID = Product_8.ProductID ON dbo.NewPolicyProductDetails.HospAdmissionID = Rates_8.RateID LEFT OUTER JOIN
                      dbo.NewRates AS Rates_9 ON dbo.NewPolicyProductDetails.PremiumWaiverID = Rates_9.RateID LEFT OUTER JOIN
                      dbo.NewRates AS Rates_10 LEFT OUTER JOIN
                      dbo.Product AS Product_10 ON Rates_10.ProductID = Product_10.ProductID ON dbo.NewPolicyProductDetails.KeyCareID = Rates_10.RateID LEFT OUTER JOIN
                      dbo.NewRates AS Rates_12 LEFT OUTER JOIN
                      dbo.Product AS Product_12 ON Rates_12.ProductID = Product_12.ProductID ON dbo.NewPolicyProductDetails.HospExcessID = Rates_12.RateID LEFT OUTER JOIN
                      dbo.Product AS Product_9 ON Rates_9.ProductID = Product_9.ProductID LEFT OUTER JOIN
                      dbo.Product AS Product_2 ON Rates_2.ProductID = Product_2.ProductID LEFT OUTER JOIN
                      dbo.Product AS Product_4 ON Rates_4.ProductID = Product_4.ProductID LEFT OUTER JOIN
                      dbo.Product AS Product_11 ON Rates_11.ProductID = Product_11.ProductID
WHERE     (Policy.MemberStatusID = 1)and ((policy.CompanyID)= @MyParam1)

Open in new window


Author Closing Comment

ID: 34931573

I've played around and left out the con.Open "Your ConnectionString" and it works perfectly

Thanks a ton
Good Luck with getting to the top Barbequer
We call them Braai's here in South Africa and it is a hugely popular pastime in this country.

Author Comment

ID: 34931580

BTW the link to your website is non-functional at the moment
LVL 84
ID: 34932707
<I've played around and left out the con.Open "Your ConnectionString" and it works perfectly >

Sorry, guess I should have been more explicit, but I thought it was obvious. You'd have to replace "Your Connection STring" with your own connection string. In other words, you would require an open, valid connection to the database before you open your recordset.


Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

895 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

19 Experts available now in Live!

Get 1:1 Help Now