Home » excel » excel – SYNTAX error in vba upload in access database

excel – SYNTAX error in vba upload in access database

Posted by: admin May 14, 2020 Leave a comment


I am trying to update few fields in an Access Database from an Excel file through vba. The field formats are identical. However, I am facing an error message with following text:

run time error (syntax error in FROM clause)

following is my code:

Dim Wb As Workbook
Dim Ws1, Ws2 As Worksheet
Dim CN As ADODB.Connection
Dim Rst As ADODB.Recordset
Dim i As Long
Dim mySQLst As String
Dim IDd As Long
Dim dbPath
Set Wb = ThisWorkbook
Set Ws1 = Wb.Sheets("Sheet1")

dbPath = Ws1.Range("V1").Value
Set CN = New ADODB.Connection
CN.Open "Provider=microsoft.ace.oledb.12.0;data source=" & dbPath

Set Rst = New ADODB.Recordset

For i = 2 To Ws1.Range("A" & Rows.Count).End(xlUp).Row

    IDd = Ws1.Cells(i, 1).Value
    mySQLst = "SELECT * FROM Test_Table1 WHERE (((Test_Table1.ID1)=16));"

    Rst.Open mySQLst, CN, adOpenDynamic, adLockOptimistic, Options:=adCmdTable

        While Not Rst.EOF
            Rst!TASK_STATUS = Ws1.Cells(i, 13).Value
            Rst!TASK_COMPLETED_DATE = Ws1.Cells(i, 14).Value
            Rst!RESPONSE = Ws1.Cells(i, 15).Value
Next i

Set Rst = Nothing
Set CN = Nothing

End Sub

I have my activeX library 2.5 active from reference.


How to&Answers:

You have a number of issues with your code:
Once you fix that error you will see a bunch more.

  1. To fix the error you have asked about please try using Named fields in your recordset:


  2. If you have a column in your spreadsheet containing ID1 value, you need to replace the “16” in the select statement with that cell each time you loop through.

  3. remove the rst.movenext – you only want to update one record.

  4. change the While Not Rst.EOF to IF Not Rst.EOF (update if there is a record, not loop)

Things will work much better for you after these changes.