I've been working with SQL for a little while and I am starting to explore stored procedures and how to run them from excel vba. I hope this is the forum for this question. I have this script to run a stored procedure called "sp_SelectTForecast" (SP) that retrieves data from a table called tForecast on my local database called "ITMDB"
I think that I have connected to the correct database, and at first the error said could not find the SP, but now the script runs all the way through but the data does not show up on the excel sheet. what am I doing wrong?
Sub GettForecast()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
I had my ActiveX turned on but what I did not do is create a variable to hold the data which I could then copy the data from that into the excel sheet. Got the answer from a colleague.
Sub GettForecast()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.RecordSet
Set conn = New ADODB.Connection
conn.ConnectionString = "DRIVER=SQL Server;SERVER=localhost\sqlexpress;UID=username;Trusted_Connection=Yes;APP=Microsoft Office 2013;WSID=local database ID;DATABASE=ITMDB"
conn.Open
Set cmd = New ADODB.Command
'cmd.ActiveConnection = conn
'cmd.CommandType = adCmdStoredProc
'cmd.CommandText = "dbo.sp_SelectTForecast"
'save data to variable
Set rs = conn.Execute("dbo.sp_SelectTForecast")
Now the next step is to create a stored procedure that does the same thing as the below query, which deletes rows from a table and inserts the updated data. Right now I just open SQL Management Studio and run this query, I would like to do it all in excel where I collect the data to insert into the table in the database.
//////////////////////////Query//////////////////////////////////////////////
delete
from tForecast
where Vintage='2Q18'
go
INSERT INTO dbo.tForecast
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Text;Database=C:\Users\nreilly\Documents\GlobalDesignTeam\QCCharts', 'select * from ITMDBLoad.csv')
///////////////////////////////////////////////////////////////////////////////
Should I start another post for this or can I continue post?
Thanks everyone and sorry I neglected this one, but it was a temporary remedy and now the IT team has automated the entire data collection and retrieval.