Error with executing SQL

txchem

New Member
Joined
Jun 16, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
That connection string looks odd to me. I think it should be:

Code:
ExcelTable = "[Excel 12.0;HDR=YES;DATABASE=C:\Users\lueg\Desktop\DB Macro Test.xlsm].[rawdata$]"
 
Upvote 0
That connection string looks odd to me. I think it should be:

Code:
ExcelTable = "[Excel 12.0;HDR=YES;DATABASE=C:\Users\lueg\Desktop\DB Macro Test.xlsm].[rawdata$]"
Sorry I had updated it to run with my Spreadsheet and db. Correct is below

VBA 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\foster.john\Desktop\Production Schedule 2022  DugTest-1.xlsm].[Line14]"
   
    Set dbConnection = CreateObject("ADODB.Connection") 'New ADODB.Connection
    dbConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=\\US5MS0005\_Group Folders\Public\Production Data\Production Database\dbPlantData_be.accdb;"
   
    Set dbCommand = CreateObject("ADODB.Command") 'New ADODB.Command
   
    With dbCommand
        .ActiveConnection = dbConnection
                   
        'Update Volume field in existing records
           
        SQL = "UPDATE tblProcessOrder A" & _
              " INNER JOIN " & ExcelTable & " X" & _
              " ON A.intPO = X.intPO" & _
              " SET A.dtmScheduledDate = X.dtmScheduledDate, SET A.txtSpecInstruct = X.txtSpecInstruct,A.intSAPIDFK = X.intSAPIDFK, A.intQuantityNeeded = X.intQuantityNeeded, A.txtUoM = X.txtUoM, A.intExtruderIDFK = X.intExtruderIDFK "
           
             
        .CommandText = SQL
        .Execute
        

        'Insert new records

        SQL = "INSERT INTO tblProcessOrder" & _
              " SELECT * FROM " & ExcelTable & " X" & _
              " WHERE X.intPO NOT IN (SELECT intPO FROM tblProcessOrder)"
        .CommandText = SQL
        .Execute
       
    End With
   
    dbConnection.Close
    Set dbCommand = Nothing
    Set dbConnection = Nothing
   
End Sub
That connection string looks odd to me. I think it should be:

Code:
ExcelTable = "[Excel 12.0;HDR=YES;DATABASE=C:\Users\lueg\Desktop\DB Macro Test.xlsm].[rawdata$]"
 
Upvote 0
Then it should have a $ sign on the end in your connection string.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top