I have a problem when I try to call a SQL Server Stored Procedure with params from VB (actually VBA if it matters).
The SP look like this:
and the VB code is:
I get an error on the Execute line with the error:
The connection cannot be used to perform this operation. It is either closed or invalid in this context.
This is my first try at passing parameters, so I'm probably doing something very basic wrong. I welcome any help or pointers.
Thanks.
The SP look like this:
Code:
CREATE PROCEDURE sp_MyStoredProc
AS
DECLARE
@StartDat datetime,
@EndDate datetime
SET NOCOUNT ON
SELECT
...
FROM
...
WHERE
(Table.DATE BETWEEN @StartDate and @EndDate)
and the VB code is:
Code:
Dim Conn As ADODB.Connection
Dim ConnStr As String
Dim Cmmd As New ADODB.Command
Dim Pram As New ADODB.Parameter
ConnStr = "Provider=MSDASQL.1"
ConnStr = ConnStr & ";Persist Security Info=False"
ConnStr = ConnStr & ";User ID=sa"
ConnStr = ConnStr & ";pwd=<pwd>"
ConnStr = ConnStr & ";Data Source=<odbc>"
ConnStr = ConnStr & ";Catalog=<db>;"
Set Conn = New ADODB.Connection
Conn.Open ConnStr
Set Cmmd.ActiveConnection = Conn
Cmmd.CommandText = "sp_MyStoredProc"
Cmmd.CommandType = adCmdStoredProc
Set Pram = Cmmd.CreateParameter("@StartDate", adDate, adParamInput, , gd_Start)
Cmmd.Parameters.Append Pram
Set Pram = Cmmd.CreateParameter("@EndDate", adDate, adParamInput, , gd_End)
Cmmd.Parameters.Append Pram
Cmmd.CommandTimeout = 0
Cmmd.Execute
I get an error on the Execute line with the error:
The connection cannot be used to perform this operation. It is either closed or invalid in this context.
This is my first try at passing parameters, so I'm probably doing something very basic wrong. I welcome any help or pointers.
Thanks.