You can't use SQL.REQUEST to achieve this because it's a function.
Here is a way using SQL code:
It demonstrates two methods for inserting records into a CLOSED Excel
workbook.
For this demonstration I have used, as the target workbook,
SqlRequest_COUNT.xls
http://www.bygsoftware.com/examples/zipfiles/SqlRequest_COUNT.zip
This workbook contains a range called LocalTable. It is two (2) coumns wide.
Copy the code, below, into a VBA module.
The SQL code in Method 1 INSERTS a single record into the table.
The SQL code in Method 2 INSERTS multiple rows from a table in the source
workbook.
The source table, called MyTable, must also be two columns wide.
You may have to adjust the paths in the connection string depending on where
you save the source workbook.
This works for hundreds of records.
'' Code begins here - - - - - - - - - - - - - -
Option Explicit
Sub InsertValuesIntoClosedExcelWorkbook()
Dim lStr_Conn As String
Dim lStr_Sql As String
lStr_Conn = ""
lStr_Conn = lStr_Conn & "ODBC;DSN=Excel Files;"
lStr_Conn = lStr_Conn & "DBQ=C:\Demo\SqlRequest_COUNT.xls;"
lStr_Conn = lStr_Conn & "DefaultDir=C:\Demo;"
lStr_Conn = lStr_Conn & "DriverId=790;"
lStr_Conn = lStr_Conn & "MaxBufferSize=2048;"
lStr_Conn = lStr_Conn & "PageTimeout=5;"
'' Method 1
'' lStr_Sql = ""
'' lStr_Sql = lStr_Sql & " INSERT INTO LocalTable"
'' lStr_Sql = lStr_Sql & " VALUES (555,5)"
'' Method 2
lStr_Sql = ""
lStr_Sql = lStr_Sql & " INSERT INTO LocalTable"
lStr_Sql = lStr_Sql & " SELECT *"
lStr_Sql = lStr_Sql & " FROM `" & ThisWorkbook.FullName & "`.MyTable"
With ActiveSheet.QueryTables.Add(Connection:=lStr_Conn,
Destination:=Range("A1"))
.CommandText = lStr_Sql
.Name = "Query from Excel Files"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh
End With
End Sub
'' Code ends here - - - - - - - - - - - - - -