Thanks!! I think this will work, but I'm new to this and am not sure how
to
point it to my form to get the parameter values. Can I use the fieldnames
like this:
Dim qdfPassthrough As DAO.QueryDef
Dim strSQL As String
' Note: Path to text boxes in current database:
Forms![frmCreateRandomsPrintCOCForms]![txtClientNumber]
strSQL = "Exec usp_SelectRandomEmployees @clientNumber='" &
[txtClientNumber] & _
"', @branchNumber='" & [txtBranchNumber] & _
"', @testCount='" & [HowManyForDrugTest] & _
"', @alcoholCount='" & [HowManyForAlcoholTest] & _
"', @alternateCount='" & [HowManyAlternates] & "'"
Set qdfPassthrough = CurrentDb().QueryDefs("MyPassthrough")
qdfPassthrough.SQL = strSQL
qdfPassthrough.Execute
Also, the results are being appended into an existing table by the stored
procedure, so I believe we don't need to return a recordset....does that
sound right?
Thanks again!!!
Susan
Douglas J. Steele said:
I don't believe it's possible with a macro, but it's pretty easy to do
with
VBA.
Assuming your stored procedure is MySP and it takes 2 parameters @parm1
and
@parm2 (with @parm1 being text and @parm2 being numeric), create a
pass-through query that executes the stored procedure:
Exec MySp @parm1='Text string', @parm2=123
Save that pass-through query as, say, MyPassthrough
In VBA, add code like:
Dim qdfPassthrough As DAO.QueryDef
Dim strSQL As String
strSQL = "Exec MySp @parm1='" & Value1 & _
"', @parm2=" & Value2
Set qdfPassthrough = CurrentDb().QueryDefs("MyPassthrough")
qdfPassthrough.SQL = strSQL
qdfPassthrough.Execute
This assumes that qdfPassthrough doesn't return a recordset. If it does:
Dim qdfPassthrough As DAO.QueryDef
Dim rsCurr As DAO.Recordset
Dim strSQL As String
strSQL = "Exec MySp @parm1='" & Value1 & _
"', @parm2=" & Value2
Set qdfPassthrough = CurrentDb().QueryDefs("MyPassthrough")
qdfPassthrough.SQL = strSQL
Set rsCurr = qdfPassthrough.OpenRecordset
Note that it's not really necessary that the pass-through query exist in
advance: you can always set the Connect property of qdfPassthrough if you
like. I just thought that you'd want to test the pass-through first!