docmd.runsql

G

Guest

Server = SQL Server 2000sp3a
Client = Access 2000 SP3

I have a onClick event on a command button which runs a stored procedure.

docmd.runsql "exec sp_mySproc '" & me.cmbo1 & "', '" & me.cmbo2 & "'"

This results in the error:
2342 A RunSQL action requires an argument consisting of an SQL statement

I've tried typing out the actual thing in VBA, ie not passed from objects
but hard coded, but it still fails. I know the SQL is right because I can
msgbox it and copy the result into QA and run ok from there.

I have no problems running straight docmd.runsql "exec
sp_MySproc_No_Params", just as long as there are no parameter values expected.

Is this normal?

Should I be using this instead?:

Dim adoRST As ADODB.Recordset, adoCON As ADODB.Connection, strSQL As String
Set adoRST = New ADODB.Recordset
Set adoCON = CurrentProject.Connection
strSQL = "exec sp_mySproc '" & me.cmbo1 & "', '" & me.cmbo2 & "'"
adoRST.open strSQL, adoCon


Or is there a better way?
Thanks.

Steve'o
 
G

Graham R Seach

Steve,

You should be using ADO.

If you don't need to return a recordset, then the following will work:
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command

On Error GoTo Proc_Err

Set cn = CurrentProject.Connection
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cn
.CommandText = "sp_mySproc"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters("@Param1") = Me.cmbo1
.Parameters("@Param2") = Me.cmbo2
.Execute

Debug.Print .Parameters("@Return_Value")
End With

Set cmd = Nothing
cn.Close
Set cn = Nothing

'= = = = = = =
If you want to return a recordset, then the following will work:
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset

On Error GoTo Proc_Err

Set cn = CurrentProject.Connection
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = cn
.CommandText = "sp_mySproc"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters("@Param1") = Me.cmbo1
.Parameters("@Param2") = Me.cmbo2
End With

Set rs = cmd.Execute
Debug.Print rs!SomeColumn

rs.Close
Set rs = Nothing
Set cmd = Nothing
cn.Close
Set cn = Nothing

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA: SQL Query in Translation Function 3
SQL -- VBA 3
Error on DoCmd.RunSQL 3
MDE version does not work 1
SQL - VBA once again 10
Code doesn't work on some computers 6
SQL automatically sorting 6
Select sum() query 3

Top