Hello everyone and thanks in advance for your help. My first post here, it's been a while since I'm working around this issue and need your help.
I have a Store procedure wich has a Bulk Insert command using dynamic SQL that works ok when executed from SQLManagmentStudio. The SP is included bellow (it has one parameter which I want to complete with the info contained in a cell, excel 2010). I'm using a Macro and VBA to execute the SP and send the parameter (see macro bellow, the range in B2 has the path to the .csv file). Unfortunatelly the macro does not work and sends the following error.
"The Query did not run, or the database table could not be opened"
This same workbook has other macros connecting with the same data base but this one doesn't work.. any idea? THANKS!!!!
Store Procedure:
ALTER PROCEDURE [dbo].[sp_Import_CSV]
@PathFileName varchar(100)
AS
DECLARE @SQL varchar (1000)
DELETE FROM Import_Form
SET @SQL = "BULK INSERT Import_Form FROM '"+@PathFileName+"' WITH(FIELDTERMINATOR = ',', ROWTERMINATOR = ',\n')"
EXEC (@SQL)
Macro:
Sub Import()
With ActiveWorkbook.Connections("IMP_csv").OLEDBConnection
.CommandText = "EXECUTE dbo.sp_Import_CSV '" & Range("B2").Value & "' "
End With
ActiveWorkbook.Connections("IMP_csv").Refresh
End Sub
I have a Store procedure wich has a Bulk Insert command using dynamic SQL that works ok when executed from SQLManagmentStudio. The SP is included bellow (it has one parameter which I want to complete with the info contained in a cell, excel 2010). I'm using a Macro and VBA to execute the SP and send the parameter (see macro bellow, the range in B2 has the path to the .csv file). Unfortunatelly the macro does not work and sends the following error.
"The Query did not run, or the database table could not be opened"
This same workbook has other macros connecting with the same data base but this one doesn't work.. any idea? THANKS!!!!
Store Procedure:
ALTER PROCEDURE [dbo].[sp_Import_CSV]
@PathFileName varchar(100)
AS
DECLARE @SQL varchar (1000)
DELETE FROM Import_Form
SET @SQL = "BULK INSERT Import_Form FROM '"+@PathFileName+"' WITH(FIELDTERMINATOR = ',', ROWTERMINATOR = ',\n')"
EXEC (@SQL)
Macro:
Sub Import()
With ActiveWorkbook.Connections("IMP_csv").OLEDBConnection
.CommandText = "EXECUTE dbo.sp_Import_CSV '" & Range("B2").Value & "' "
End With
ActiveWorkbook.Connections("IMP_csv").Refresh
End Sub