Question : How to pass variable to sql server stored procedure from access mdb form

I have an Access mdb front end, which is linked to sql server tables via ODBC.  I'd like to leverage sql server stored procedures to improve the performance, but  I must maintain the front end as an mdb file (not adp).  

Here is a specific scenario:

Form (frmAdoptionList) contains a listbox (lstAdoption) that currently uses a local query as it's record source (qlstAdoption).  This query refers to two fields on the frmAdoptionList form  to filter the listbox. For example:

SELECT dbo_sa_adoption.*
FROM dbo_sa_adoption
WHERE (((dbo_sa_adoption.location)=[Forms]![frmAdoptionList]![cboLocation]) AND ((dbo_sa_adoption.adopted)=[Forms]![frmAdoptionList]![adoptValue]));

I'm also using an AfterUpdate event procedure to requery listbox when user changes either of the two form field values, e.g.:

Private Sub cboLocation_AfterUpdate()
    Me.lstAdoption.Requery
End Sub

I'd like to change row source of the listbox to instead use a store procedure and pass the two form field values so that I'm not pulling the entire record set across.  To prepare for this, I've created a simple stored procedure named "sp_sa_lstAdoption" using this:

CREATE PROCEDURE [dbo].[sp_sa_lstAdoption]
      @location varchar(50),
      @adopted int
AS
SELECT     dbo.sa_adoption.*
FROM       dbo.sa_adoption      
WHERE location = @location and adopted = @adopted

Using this specific example, can someone show me how to set the row source of the listbox to use the stored procedure that uses the two form parameters?  I'm assuming I need to do something on the "OnOpen" event of the form containing the listbox, but I'm not sure exactly how to do it.  I'm not used to setting connection strings in code, so specific guidance on what the resulting code should look like for the OnOpen event would be extremely helpful.  If I can understand how to accomplish this once, I think I'll be able to figure out how to do it in other scenarios.  Any help is much appreciated.

Regards,
Christopher

Answer : How to pass variable to sql server stored procedure from access mdb form

there quite a few approaches to solve this, here's one:

create a passthrough query (it doesnt matter what it is, it's just to hold all connection information so you dont need to enter it every time)

now add a public sub in code:

Public Sub ChangePTquerySQL(strSQL As String)
Dim qdf As DAO.QueryDef

Set qdf = DBEngine(0)(0).QueryDefs("qrySQLPassThrough")
qdf.SQL = strSQL
Set qdf = Nothing
End Sub

now you can call this sub every time with the full sql passthrough syntax, and then set the result to your needs. so in your example you use the after update event of adoptValue and cboLocation in the Forms!frmAdoptionList:

if isNull(cboLocation)=False And IsNull(adoptValue)=False Then
    ChangePTquerySQL "Exec dbo.sp_sa_lstAdoption '" & cboLocation & "'," & adoptValue & ";"

then, if lstAdoption rowsource is set to "qrySQLPassThrough" then
Me.lstAdoption.Requery will do the trick

good luck

Random Solutions  
 
programming4us programming4us