Dirk said:
The code example I gave was for DAO recordsets, which don't have an
UpdateBatch method. If you're using ADO, it will be slightly different.
A variation on you suggestion could be to use the ADO Recordset's
GetString method to write a derived table and use this to do a bulk
insert in one hit e.g.
INSERT INTO Test (col1, col2)
SELECT DT1.col1, DT1.col2
FROM (
SELECT DISTINCT 1 AS col1, 1 AS col2
FROM Test
UNION ALL
SELECT DISTINCT 2, 2
FROM Test
UNION ALL
SELECT DISTINCT 3, 3
FROM Test
) AS DT1;
Here's a demo:
Sub fabr()
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
.Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\DropMe1.mdb"
With .ActiveConnection
' Create auxilary table of digits 0-9
.Execute _
"CREATE TABLE Test (" & _
" col1 INTEGER NOT NULL," & _
" col2 INTEGER NOT NULL);"
.Execute _
"INSERT INTO Test VALUES (99, 99);"
' Fabricate a recordset
Dim rsTemp
Set rsTemp = CreateObject("ADODB.Recordset")
With rsTemp
.Fields.Append "col1", adInteger
.Fields.Append "col2", adInteger
.Open
.AddNew Array("col1", "col2"), Array(1, 1)
.AddNew Array("col1", "col2"), Array(2, 2)
.AddNew Array("col1", "col2"), Array(3, 3)
.MoveFirst
Dim sql As String
sql = _
"INSERT INTO Test (col1, col2)" & _
" SELECT DT1.col1, DT1.col2 FROM (" & _
" SELECT DISTINCT NULL AS col1, " & _
" NULL AS col2 FROM Test WHERE 0=1" & _
" UNION ALL SELECT DISTINCT "
Const DELIMITER As String = _
" FROM Test UNION ALL SELECT DISTINCT "
sql = sql & .GetString(2, , ", ", DELIMITER)
' remove trailing delimiter text
sql = _
Left$(sql, Len(sql) - Len(" UNION ALL SELECT DISTINCT "))
sql = sql & _
") AS DT1;"
End With
MsgBox sql
.Execute sql
Dim rs
Set rs = .Execute( _
"SELECT col1, col2 FROM Test;")
MsgBox rs.GetString
End With
Set .ActiveConnection = Nothing
End With
End Sub
Jamie.
--