问题
Here is my code to retrieve data from access but I always encounter "Run-time error '-2147217900 (80040e14)' Syntax error in FROM clause"
Sub UPDATE_REGION()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim AW As Workbook
Set AW = ActiveWorkbook
Path = AW.Path
cnn_pth = Path & "\Master File.accdb"
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open cnn_pth
End With
Set rst = New ADODB.Recordset
sSQL = "select Package_Nb from [package_db] where [Hubs] is null"
rst.Open Source:=sSQL, ActiveConnection:=cnn, CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, Options:=adCmdTable
end subs
When i stop script to debug, I found Source value in Local Window like this: "select * from select Package_Nb from package_db where Hubs is null"
I don't know why vba auto add select * from
to source like this, anyone know how to fix this?
回答1:
Your options are wrong. This:
Options:=adCmdTable
should be this:
Options:=adCmdText
Since you are not passing a table name but a SQL string. (when you pass a table name it is effectively converted into a SELECT * FROM table_name
statement).
来源:https://stackoverflow.com/questions/28451358/syntax-error-in-from-clause-excel-vba