I found this code from an earlier post however I keep getting an error on the ".Execute" statement, the error is a runtime error with Syntax error in Update Statement and when I hit debug it goes to the .Execute piece of code. IS there a reference or something else I need to turn on?
VBA Code:
Code:
Public Sub Update_and_Insert_Access_Table()
Dim dbConnection As Object 'ADODB.Connection
Dim dbCommand As Object 'ADODB.Command
Dim ExcelTable As String
Dim SQL As String
ExcelTable = "[Excel 12.0 Xml;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=C:\Users\lueg\Desktop\DB Macro Test.xlsm].[rawdata$]"
Set dbConnection = CreateObject("ADODB.Connection") 'New ADODB.Connection
dbConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=\\an\HRS SD Performance Data\Capacity DB.accdb;"
Set dbCommand = CreateObject("ADODB.Command") 'New ADODB.Command
With dbCommand
.ActiveConnection = dbConnection
'Update Volume field in existing records
SQL = "UPDATE AssignedVol_tbl A" & _
" INNER JOIN " & ExcelTable & " X" & _
" ON A.ID_Unique = X.ID_Unique" & _
" SET A.Volume = X.Volume"
.CommandText = SQL
.Execute
'Insert new records
SQL = "INSERT INTO AssignedVol_tbl" & _
" SELECT * FROM " & ExcelTable & " X" & _
" WHERE X.ID_Unique NOT IN (SELECT ID_Unique FROM AssignedVol_tbl)"
.CommandText = SQL
.Execute
End With
dbConnection.Close
Set dbCommand = Nothing
Set dbConnection = Nothing
End Sub