FROM子句Excel VBA中的语法错误

时间:2015-02-11 09:59:02

标签: excel vba excel-vba syntax-error runtime-error

以下是我从访问中检索数据的代码,但我总是遇到"运行时错误' -2147217900(80040e14)' FROM子句"

中的语法错误
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

当我停止脚本调试时,我在本地窗口中找到了这样的源值:"select * from select Package_Nb from package_db where Hubs is null"

我不知道为什么vba自动添加select * from这样的来源,有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的选择是错误的。这样:

Options:=adCmdTable

应该是这样的:

Options:=adCmdText

因为您没有传递表名而是传递SQL字符串。 (当您传递表名时,它会被有效地转换为SELECT * FROM table_name语句。)