我已经在这里待了2个小时,不知道问题是什么。虽然我已经使用SQL工作了很多,但在使用ADO ACE连接查询Excel工作表时,我在使用Access查询相对于SQL查询的特性时遇到了困难。我最基本的目标是使用子查询进行连接。我已经大大简化了查询以显示问题,所以请原谅这样一个事实,即运行这样的查询真的没有意义。
我一直得到的错误是Syntax Error in From Clause
。该工作表只有2列,Account Number
和Cost
Sub stackoverflow()
Dim acctcon As New ADODB.Connection
Dim acctrec As New ADODB.Recordset
acctcon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Documents\Book1.xlsx;" & _
"Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=YES" & Chr(34) & ";"
acctcon.Open
querystr = "Select [Accounts$].[Account Number], [mm].[Cost] " & _
"FROM [Accounts$] " & _
"JOIN (Select [Account Number], [Cost] " & _
"FROM [Accounts$]) As mm " & _
"ON [Accounts$].[Account Number] = [mm].[Account Number]"
acctrec.Open querystr, acctcon
'Syntax Error on From Clause
End Sub
答案 0 :(得分:0)
尝试重置As mm
querystr = "Select [Accounts$].[Account Number], [mm].[Cost] " & _
"FROM [Accounts$] " & _
"INNER JOIN (Select [Account Number], [Cost] " & _
"FROM [Accounts$] As mm) " & _
"ON [Accounts$].[Account Number] = [mm].[Account Number]"
但是你必须更换内连接,因为没有像@krish KM所说的那样。我想你把它作为占位符。