简体   繁体   中英

Excel VBA Call to SQL Stored Procedure Returning Bogus Connection Error

I am working on a script to loop through an excel sheet, pass parameters to an SQL Server procedure that takes three parms and generates records based on the parms. When I make the call, it actually does pass the parameters correctly, executes the procedure and writes the records, but then I get a runtime error "1004 The query did not run..."

I've found comments that state to change Background Query = False - check. Set NoCount On - check... still getting the error.

The SQL Server procedure does include a ReturnValue parm (1/0), which I am not passing, because it states "Too Many Parameters" when I try to do that. Is that possibly the problem? Not sure how to receive back a return value.

Here is my VBA Code:

Sub CallProc()
    Dim ws As Worksheet
    Worksheets("AllTags").Activate

    Dim Parm1 As String
    Dim Parm2 As String
    Dim Parm3 As Integer
    Dim RowIndex As Long
    Dim ReturnValue As Integer

    RowIndex = 2

   Do While Sheets("AllTags").Cells(RowIndex, 2).Value <> ""

   If Sheets("AllTags").Cells(RowIndex, 3).Value > 0 Then
   ReturnValue = 0
   Parm1 = Sheets("AllTags").Cells(RowIndex, 1).Value
   Parm2 = Sheets("AllTags").Cells(RowIndex, 2).Value
   Parm3 = Sheets("AllTags").Cells(RowIndex, 3).Value

   With ActiveWorkbook.Connections("TagUpload").OLEDBConnection

   ' SET NOCOUNT ON;  ** IT DOESN'T LIKE THIS 

    .BackgroundQuery = False
    .CommandText = "EXECUTE DCSTransfer.dbo.InsertTags '" & Parm1 & "', '" &  
        Parm2 & "', " & Parm3
    .CommandType = xlCmdSql
    .Connection = Array( _
    "OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security  
     Info=True;Initial Catalog=DCSTransfer;Data Source=bdata03;Use Proc" _
    , _
    "edure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation 
      ID=B-MIS-NWILSON1;Use Encryption for Data=False;Tag with co" _
    , "lumn collation when possible=False;")


    .RefreshOnFileOpen = False
    .SavePassword = False
    .SourceConnectionFile = ""
    .SourceDataFile = ""
    .ServerCredentialsMethod = xlCredentialsMethodIntegrated

     ActiveWorkbook.Connections("TagUpload").Refresh
    .BackgroundQuery = False

     On Error Resume Next  ' **** NEVER GETS THIS FAR ***

     End With

     End If

     RowIndex = RowIndex + 1
     Loop

     End
 End Sub

Here is the script generated when I manually call the procedure in SQL Server and enter the Parms:

USE [DCSTransfer]
 GO

DECLARE @return_value int

 EXEC   @return_value = [dbo].[InsertTags]
        @Badge = N'9046',
        @StartTag = N'80996',
        @Qty = 70

 SELECT 'Return Value' = @return_value

 GO

Any other suggestions?

Figured it out... after two+ entire days spent paining over this!

Changed the CommandText to match the stored procedure:

    .CommandText = "Declare @return_Value int EXEC @return_Value = DCSTransfer.dbo.InsertTags '" & Parm1 & "', '" & Parm2 & "', " & Parm3 & "SELECT 'Return Value' = @return_value"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM