I use VBA and the ADODB to query against a SQL Server server and having trouble with INSERT INTO statement in the following string:
StrQuery = "INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28)," & _
" ('RRA','Care: Quality',29)"
The error message is "Incorrect syntax near ','. I'm sure something is wrong with the above statement but I'm struggling to figure our what might be wrong.
I would like to insert the following values into the table (it does work perfectly in SQL):
INSERT INTO #temp_ImprtSampVol
VALUES
('RBM','Care: Admin',28),
('RBM','Care: Exit',31),
('RBM','Care: Other',22),
('RRA','Care: Quality',29),
('RRA','Care: Finance',37),
My code is as follows:
Sub VBAFromSQL()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=HIDDEN_Outputs;Data Source=HIDDEN;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False"
cnn.Open ConnectionString
cnn.CommandTimeout = 900
StrQuery = "IF OBJECT_ID('tempdb..#temp_MthToDt') IS NOT NULL drop table
#temp_MthToDt" & _
" IF OBJECT_ID('tempdb..#temp_ImprtSampVol') IS NOT NULL drop table
#temp_ImprtSampVol" & _
" IF OBJECT_ID('tempdb..#temp_T1') IS NOT NULL drop table #temp_T2AuditData"
StrQuery = "DECLARE @Last_Day_Loaded VARCHAR(30) = '2018-10-03';" & _
" DECLARE @Start_of_Sample VARCHAR(30) = '2018-10-03';" & _
" DECLARE @End_of_Sample VARCHAR(30) = '2018-10-03';" & _
" DECLARE @Days_Data_so_far INT = 03;" & _
" DECLARE @First_Day_Month VARCHAR(30) = '2018-10-01';" & _
" DECLARE @Working_Days_in_Month INT = 23;" & _
" DECLARE @Days_in_Month INT = 31;" & _
" DECLARE @BBE_Sample_Minimum INT = 10;" & _
" DECLARE @BBE_Sample_adjustment_multiplier decimal(5,2) = 1.0;"
StrQuery = "CREATE TABLE #temp_ImprtSampVol (" & _
" CIS VARCHAR(10) NOT NULL," & _
" RD VARCHAR(40) NOT NULL," & _
" Sample_to_send INT NOT NULL );"
StrQuery = "INSERT INTO #temp_ImprtSampVol Values ('RBM','Care: Admin',28)," & _
" ('RRA','Care: Quality',29)"
rst.Open StrQuery, cnn
Sheets(1).Range("A2").CopyFromRecordset rst
End Sub
Can you please advise how to rectify this? Thanks in advance.