RE: Run a Stored Procedure from Excel
- From: Jez <Jez@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 19 Sep 2008 07:38:00 -0700
I tried it using this one line
sQRY = "EXEC jez.sp_WLName_Report 'September', '2008', '17-09-2008', 'PGA'"
I altered the Stored Procedure to pick up this one parameter.
This works great...
My only problem is that I have more than 1 parmater in most cases, thats why
I have changed the Exec Statement to represnt the changes in parmaters.
"Joel" wrote:
The SQL seperator may be a colon instead of a semicolon. From the SQL.
command line try putting two instructions on the same line seperated by a
colon and see what happens. get a single line multiple command working from
your SQL server before trying it in code. another possible solution is to
combine the commands on One Line using an And.
"Jez" wrote:
I have tried all 3 solutions and they still dont connect the results.
I changed all the Char(39) to Char(13) and found that I got an error message
telling me to look at the part of my statement which was like this
+ ',' +
As this didnt seem to be liked.
The other 2 solutions brought back the same error message,
Operation is not allowed when the object is closed. 3704
I am totally baffled, by this...
"Joel" wrote:
I think you are missing the carriage returns. The SQL server expects each
command to start on a new line or seperated by a terminator like a semicolon.
Your combined string don't provide a method for the server to know when one
command ends and the next commands starts.
I don't know SQL very well by try one of three solutions.
chr(13) is a cariage return and chr(39) is a single quote.
semicolons added
sQRY = "DECLARE @wlvals varchar(100); " & _
"SET @wlvals = char(39) + 'T2DEA' + char(39) + ',' +
char(39) + 'T2DEP' + char(39); " & _
"EXEC sp_WLName_Report 'September', '2008', '18-09-2008',
@wlvals"
or
Carriage returns added
sQRY = "DECLARE @wlvals varchar(100)" & chr(13) & _
"SET @wlvals = char(39) + 'T2DEA' + char(39) + ',' +
char(39) + 'T2DEP' + char(39)" & chr(13) & _
"EXEC sp_WLName_Report 'September', '2008', '18-09-2008',
@wlvals"
"Jez" wrote:
The problem area is around this section here
sQRY = "DECLARE @wlvals varchar(100) " & _
"SET @wlvals = char(39) + 'T2DEA' + char(39) + ',' +
char(39) + 'T2DEP' + char(39) " & _
"EXEC sp_WLName_Report 'September', '2008', '18-09-2008',
@wlvals"
When I run this in SQL Server it works perfect.
DECLARE @wlvals varchar(100)
SET @wlvals = char(39) + 'T2DEA' + char(39) + ',' + char(39) + 'T2DEP' +
char(39)
EXEC sp_WLName_Report 'September', '2008', '18-09-2008', @wlvals
Do I need to do something different with my code in Excel to run this
Execute Statement?
"Joel" wrote:
It meansd the OBJECT was closed for some other reason. Either the server
closed the connection because there was some security problem (an illegal
command could of done this) or when you closed rsDW. I would move the close
statement just after the open to check that it works. then I would keep
moving the line down in the code until you get the error message again. This
way you can find out where the problem is.
"Jez" wrote:
I have this code below to Execute a Command in Stored Procedure in SQL
Server. My problem is that I keep having an error message saying
Error: Operation is not allowed when the object is closed. 3704
I have no idea what that means,
Option Explicit
Dim cnnDW As ADODB.Connection
Dim rsDW As ADODB.Recordset
'Dim cnnSP As ADODB.Command
Dim sQRY As String
Dim strDWFilePath As String
Sub GetData()
On Error GoTo Err:
strDWFilePath = "Driver={SQL Native
Client};Server=CISSQL1;Database=CORPINFO;Trusted_Connection=Yes"
Set cnnDW = New ADODB.Connection
Application.ScreenUpdating = False
cnnDW.Open strDWFilePath
Sheet1.Range("E4:F9").ClearContents
Set rsDW = New ADODB.Recordset
sQRY = "DECLARE @wlvals varchar(100) " & _
"SET @wlvals = char(39) + 'T2DEA' + char(39) + ',' +
char(39) + 'T2DEP' + char(39) " & _
"EXEC sp_WLName_Report 'September', '2008', '18-09-2008',
@wlvals"
rsDW.CursorLocation = adUseClient
rsDW.Open sQRY, cnnDW, adOpenDynamic, adLockOptimistic, adCmdText
Application.ScreenUpdating = False
Sheet1.Range("E4").CopyFromRecordset rsDW
rsDW.Close
Set rsDW = Nothing
MsgBox "Import Complete", vbInformation, "SQL Connection"
cnnDW.Close
Set cnnDW = Nothing
Exit Sub
Err:
MsgBox "The following error has occured-" & vbCrLf & vbCrLf & VBA.Error,
vbCritical, "SQL Connection"
MsgBox VBA.Err
End Sub
Where am I going wrong?
- Follow-Ups:
- RE: Run a Stored Procedure from Excel
- From: Joel
- RE: Run a Stored Procedure from Excel
- References:
- Run a Stored Procedure from Excel
- From: Jez
- RE: Run a Stored Procedure from Excel
- From: Joel
- RE: Run a Stored Procedure from Excel
- From: Jez
- RE: Run a Stored Procedure from Excel
- From: Joel
- RE: Run a Stored Procedure from Excel
- From: Jez
- RE: Run a Stored Procedure from Excel
- From: Joel
- Run a Stored Procedure from Excel
- Prev by Date: RE: Input on a user form
- Next by Date: Re: Control scrolling in a TextBox
- Previous by thread: RE: Run a Stored Procedure from Excel
- Next by thread: RE: Run a Stored Procedure from Excel
- Index(es):