OK, so I have a spreadsheet that produces a fairly large amount of records (~ 3500)
I have the following script that inserts them into my db access:
Sub putinDB() Dim Cn As ADODB.Connection, Rs As ADODB.Recordset Dim MyConn, sSQL As String Dim Rw As Long, c As Long Dim MyField, Result Dim x As Integer Dim accName As String, AccNum As String, sector As String, holding As String, holdingvalue As Double, holdingdate As Date theend = lastRow("Holdings", 1) - 1 'Set source MyConn = "S:\Docs\Harry\Engine Client\Engine3.accdb" 'Create query Set r = Sheets("Holdings").Range("a2") x = 0 Do Application.StatusBar = "Inserting record " & x + 1 & " of " & theend accName = r.Offset(x, 0) AccNum = r.Offset(x, 4) sector = r.Offset(x, 2) holding = r.Offset(x, 1) holdingvalue = r.Offset(x, 3) holdingdate = r.Offset(x, 5) sSQL = "INSERT INTO Holdings (AccName, AccNum, Sector, Holding, HoldingValue, HoldingDate)" sSQL = sSQL & " VALUES ('" & Replace(accName, "'", "''") & "', '" & AccNum & "', '" & sector & "', '" & Replace(holding, "'", "''") & "', '" & holdingvalue & "', #" & holdingdate & "#)" Debug.Print (sSQL) 'Create RecordSet Set Cn = New ADODB.Connection With Cn .Provider = "Microsoft.ACE.OLEDB.12.0" .CursorLocation = adUseClient .Open MyConn Set Rs = .Execute(sSQL) End With x = x + 1 Loop While r.Offset(x, 0) <> "" Or x < 15 Application.StatusBar = False End Sub
The problem is that it cycles through each record one by one, rebuilds and executes the query every time, which leads to very slow execution (about 2-3 records per second on my PC).
Is there a way for vba to insert the entire range into the database in one pass without looping? Thanks