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 Rst.MoveNext Wend Next i Rst.Close Set Rst = Nothing CN.Close Set CN = Nothing End Sub
I have my activeX library 2.5 active from reference.
You have a number of issues with your code:
Once you fix that error you will see a bunch more.
To fix the error you have asked about please try using Named fields in your recordset:
“SELECT TASK_STATUS, TASK_COMPLETED_DATE, RESPONSE FROM Test_Table1 WHERE Test_Table1.ID1=16;”
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.
remove the rst.movenext – you only want to update one record.
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.