I asked this question before but I am still stuck and unable to put parameters inside a SQL query.
Public Function testInsert(tableName As String, dType As String, role As String, FiscalYear As String) Dim cmd As ADODB.Command Dim conn As ADODB.connection Dim prm As ADODB.Parameter Dim connectionString As String Dim SQL As String Set conn = New ADODB.connection Set cmd = New ADODB.Command DBPath = "path\to\file.xlsm" connectionString = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath &";HDR=Yes';" SQL = "INSERT INTO [" & tableName & "$] ([Year], [Type], [role]) VALUES (p1, p2, p3)" 'Open connection conn.Open connectionString 'Set command text cmd.CommandText = SQL cmd.CommandType = adCmdText 'Set connection cmd.ActiveConnection = conn 'Set paramters in SQL query Set prm = cmd.CreateParameter("p1", adInteger, adParamInput) cmd.Parameters.Append prm cmd.Parameters("p1").Value = 1 Set prm = cmd.CreateParameter("p2", adInteger, adParamInput) cmd.Parameters.Append prm cmd.Parameters("p2").Value = 2 Set prm = cmd.CreateParameter("p3", adInteger, adParamInput) cmd.Parameters.Append prm cmd.Parameters("p3").Value = 3 cmd.Execute conn.Close End Function I am using VBA inside of excel-2010 and I am running the query against an external excel file on my computer. I get a too few paramaters expecting three error.
Please note this code works if the values are string literals so the issue is with the parameters.