我想从两个不同的工作簿中加入两个工作表。下面是我的代码,我遇到错误'不支持JOIN表达式'当我执行sql。
Sub RunSELECT()
Dim cn As Object, rs As Object, output As String, sql As String
Dim pre_reviewed_file As String, cond As String
Dim fso As Object, outfile As String, file_obj As Object
Set fso = CreateObject("Scripting.FileSystemObject")
outfile = "C:\Users\ZhouK\Desktop\offline comments\offline-comments\result.txt"
Set file_obj = fso.CreateTextFile(outfile, True)
pre_reviewed_file = "56022473AML2002 OFFLINE listings 20161010 - reviewed.xls"
'MsgBox ActiveWorkbook.FullName
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
cond = "1=1"
With ActiveWorkbook.Sheets("LIS01").Rows(3)
For i = 1 To 24
cond = cond & " and a.[" & .Cells(i).Value & "]=b.[" & .Cells(i).Value & "]"
Next
End With
sheet_name = "LIS01"
'---Run the SQL SELECT Query---
sql = "SELECT b.[COMMENTS] FROM [" & sheet_name & "$A3:AZ3000] a left join [Excel 12.0;HDR=Yes;Database=" & ActiveWorkbook.Path & _
"\" & pre_reviewed_file & ";].[" & sheet_name & "$A3:AZ3000] b on " & cond
file_obj.Write (sql)
file_obj.Close
Set rs = cn.Execute(sql)
ActiveWorkbook.Sheets("me").Range("A1").CopyFromRecordset rs
'Do
'output = output & rs(0) & ";" & rs(1) & ";" & rs(2) & vbNewLine
'Debug.Print rs(0); ";" & rs(1) & ";" & rs(2)
'rs.Movenext
'Loop Until rs.EOF
'MsgBox output
'---Clean up---
rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing
End Sub
真正的sql是
select b.[COMMENTS]
from [LIS01$A3:AZ3000] a
left join [Excel 12.0;HDR=Yes;Database=C:\Users\ZhouK\Desktop\offline comments\offline-comments\56022473AML2002 OFFLINE listings 20161010 - reviewed.xls;].[LIS01$A3:AZ3000] b
on 1=1 and a.[Study Site Identifier]=b.[Study Site Identifier]
and a.[Unique Subject Identifier]=b.[Unique Subject Identifier]
and a.[CM Record Number]=b.[CM Record Number]
and a.[Medication]=b.[Medication]
and a.[Indication]=b.[Indication]
and a.[AE-Date-Term1]=b.[AE-Date-Term1] and a.[AE-Date-Term2]=b.[AE-Date-Term2] and a.[AE-Date-Term3]=b.[AE-Date-Term3]
and a.[AE-Date-Term4]=b.[AE-Date-Term4] and a.[AE-Date-Term5]=b.[AE-Date-Term5]
and a.[MH-Date-Term1]=b.[MH-Date-Term1] and a.[MH-Date-Term2]=b.[MH-Date-Term2] and a.[MH-Date-Term3]=b.[MH-Date-Term3]
and a.[MH-Date-Term4]=b.[MH-Date-Term4] and a.[MH-Date-Term5]=b.[MH-Date-Term5]
and a.[Prohylaxis or Other]=b.[Prohylaxis or Other]
and a.[Dose]=b.[Dose] and a.[Dose Unit]=b.[Dose Unit]
and a.[Dose Form]=b.[Dose Form] and a.[Frequency]=b.[Frequency]
and a.[Route]=b.[Route] and a.[Ongoing]=b.[Ongoing]
and a.[CM Start Date]=b.[CM Start Date] and a.[CM End Date]=b.[CM End Date]
我真的不知道哪个部分是错的。你能看一下我的代码吗?
答案 0 :(得分:0)
问题在于您的SQL JOIN
,因为Jet / ACE SQL方言的on 1=1
子句中不允许使用表达式ON
。因为您需要此项来启动循环的字符串,所以请考虑在循环后删除它:
con = Replace(con, "on 1=1 and ", "on ")
或者,通过将ON
表达式移动到WHERE
子句并用逗号分隔FROM
表来重写等效查询。这被称为隐式连接,而不是使用JOIN
的显式连接,两者都是相同的评估和性能:
select b.[COMMENTS]
from [LIS01$A3:AZ3000] a,
[Excel 12.0;HDR=Yes;Database=C:\Users\ZhouK\Desktop\offline comments\offline-comments\56022473AML2002 OFFLINE listings 20161010 - reviewed.xls;].[LIS01$A3:AZ3000] b
where 1=1 and a.[Study Site Identifier]=b.[Study Site Identifier]
and a.[Unique Subject Identifier]=b.[Unique Subject Identifier]
and a.[CM Record Number]=b.[CM Record Number]
and a.[Medication]=b.[Medication]
and a.[Indication]=b.[Indication]
and a.[AE-Date-Term1]=b.[AE-Date-Term1] and a.[AE-Date-Term2]=b.[AE-Date-Term2] and a.[AE-Date-Term3]=b.[AE-Date-Term3]
and a.[AE-Date-Term4]=b.[AE-Date-Term4] and a.[AE-Date-Term5]=b.[AE-Date-Term5]
and a.[MH-Date-Term1]=b.[MH-Date-Term1] and a.[MH-Date-Term2]=b.[MH-Date-Term2] and a.[MH-Date-Term3]=b.[MH-Date-Term3]
and a.[MH-Date-Term4]=b.[MH-Date-Term4] and a.[MH-Date-Term5]=b.[MH-Date-Term5]
and a.[Prohylaxis or Other]=b.[Prohylaxis or Other]
and a.[Dose]=b.[Dose] and a.[Dose Unit]=b.[Dose Unit]
and a.[Dose Form]=b.[Dose Form] and a.[Frequency]=b.[Frequency]
and a.[Route]=b.[Route] and a.[Ongoing]=b.[Ongoing]
and a.[CM Start Date]=b.[CM Start Date] and a.[CM End Date]=b.[CM End Date]