SydneyGeek
MrExcel MVP
- Joined
- Aug 5, 2003
- Messages
- 12,251
Hi, I'm trying to work out how to push multiple records from an Excel range to an Access database using ADO. Mr Excel's VBA book show how to do it one record at a time, but I will have anything up to 2000 records to push (about 35 fields) and one at a time will take forever...
Here's my starting code, but I am not sure of the syntax for the SQL statement, and how to actually push the data (or even if a bulk update is possible with ADO).
TPath and DataLoader are named ranges in the workbook. DataLoader is a dynamic range with potentially several thousand rows, depending on the # of projects in the file. The target table is tblProjectData.
I know I don't have a statement that achieves the "Push", but I couldn't work out the syntax. Any pointers appreciated...
Here's my starting code, but I am not sure of the syntax for the SQL statement, and how to actually push the data (or even if a bulk update is possible with ADO).
TPath and DataLoader are named ranges in the workbook. DataLoader is a dynamic range with potentially several thousand rows, depending on the # of projects in the file. The target table is tblProjectData.
I know I don't have a statement that achieves the "Push", but I couldn't work out the syntax. Any pointers appreciated...
Code:
Sub PushDataToMDB()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sSQL As String
Dim sPath As String
Dim rSource As Range
Dim MyConn
Set rSource = Range("DataLoader")
sPath = Range("TPath").Value
sSQL = "INSERT INTO tblProjectdata SELECT * FROM " & rSource
MyConn = sPath
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, _
Options:=adCmdText
Application.ScreenUpdating = False
rst.Close
cnn.Close
Application.ScreenUpdating = True
End Sub