简体   繁体   English

VBA ADO查询中的OPENROWSET

[英]OPENROWSET in VBA ADO query

I am trying to configure an SQL UPDATE query in a VBA program. 我正在尝试在VBA程序中配置SQL UPDATE查询。

Basically, I update a table in the current workbook from a table in a closed workbook. 基本上,我从关闭的工作簿中的表更新当前工作簿中的表。 This works great using the below query only if the source workbook is a .xls file : 仅当源工作簿是.xls文件时,使用以下查询才能很好地工作:

Sub UPDATEQUERY(SourcePath As String, SourceSheet As String, TargetSheet As String, _
Columns As String, Filter As String)

    Dim Cn As ADODB.Connection
    Dim QUERY_SQL As String
    Dim CHAINE_HDR As String
    Dim STRCONNECTION As String

    CHAINE_HDR = "[Excel 8.0;Provider=Microsoft.ACE.OLEDB.12.0;Mode=1;Extended Properties='HDR=YES;'] "

    Set Cn = New ADODB.Connection

    QUERY_SQL = _
    "UPDATE [" & TargetSheet & "$] INNER JOIN (SELECT * FROM [" & SourceSheet & "$] " & _
    "IN '" & SourcePath & "' " & CHAINE_HDR & ") t2 " & _
    "ON [" & TargetSheet & "$].id = t2.id " & _
    "SET [" & TargetSheet & "$].ColA = t2.ColA "

    STRCONNECTION = _
    "Driver={Microsoft Excel Driver (*.xls)};" & _
    "DriverId=790;" & _
    "Dbq=" & ThisWorkbook.FullName & ";" & _
    "DefaultDir=" & ThisWorkbook.FullName & ";ReadOnly=False;"


    Cn.Open STRCONNECTION
    Cn.Execute (QUERY_SQL)

    '--- Fermeture connexion ---
    Cn.Close
    Set Cn = Nothing

End Sub

So as to use .xlsx file as the source file I want to connect to the source using the same provider/driver that I use to connect to the current wb, that is MSDASQL.1 instead of Microsoft.ACE.OLEDB.12.0. 为了将.xlsx文件用作源文件,我想使用与连接到当前wb相同的提供程序/驱动程序连接到源,即MSDASQL.1而不是Microsoft.ACE.OLEDB.12.0。 Indeed If I only set the 'CHAINE_HDR' to Excel 12.0 I get a "ISAM driver not found". 的确,如果我仅将'CHAINE_HDR'设置为Excel 12.0则会得到“找不到ISAM驱动程序”。

To achieve this I'm trying to use OPENROWSET like this : 为了实现这一点,我试图像这样使用OPENROWSET

Sub UPDATEQUERY(SourcePath As String, SourceSheet As String, TargetSheet As String, _
Columns As String, Filter As String)

    Dim Cn As ADODB.Connection
    Dim QUERY_SQL As String
    Dim STRCONNECTION As String
    Dim STRCONNECTION_SOURCE As String


    STRCONNECTION_SOURCE = _
    "'MSDASQL.1'," & _
    "'Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" & SourcePath & ";'," & _
    "'SELECT * FROM [Data$]'"

    Set Cn = New ADODB.Connection

    QUERY_SQL = _
    "UPDATE [" & TargetSheet & "$] INNER JOIN (SELECT * FROM OPENROWSET(" & STRCONNECTION_SOURCE & ")) t2 " & _
    "ON [" & TargetSheet & "$].id = t2.id " & _
    "SET [" & TargetSheet & "$].ColA = t2.ColA "

    STRCONNECTION = _
    "Driver={Microsoft Excel Driver (*.xls)};" & _
    "DriverId=790;" & _
    "Dbq=" & ThisWorkbook.FullName & ";" & _
    "DefaultDir=" & ThisWorkbook.FullName & ";ReadOnly=False;"


    Cn.Open STRCONNECTION
    Cn.Execute (QUERY_SQL)

    '--- Fermeture connexion ---
    Cn.Close
    Set Cn = Nothing

End Sub

However I get an "Synthax error in from clause" . 但是我收到“ from子句中的Synthax错误”

How to properly set up my sql query ? 如何正确设置我的SQL查询?

Thanks 谢谢

First, OPENROWSET is a Microsoft SQL Server method (ie, TSQL) which your link references. 首先, OPENROWSET是您的链接引用的Microsoft SQL Server方法(即TSQL)。 Such queries would only run inside an SQL Server query. 这样的查询只能在SQL Server查询中运行。 It is not a Jet/ACE SQL Engine (which you are currently using) method. 它不是Jet / ACE SQL引擎(您当前正在使用)方法。 So you are getting your databases mixed up. 因此,您正在混淆数据库。 As analogy, Oracle methods would not work in Postgres databases. 以此类推,Oracle方法在Postgres数据库中不起作用。

And yes, the ACE 12.0 provider can connect to both older .xls and current .xlsx files, just as it can with older MS Access (Office sibling to MS Excel) .mdb and current .accdb files. 是的,ACE 12.0提供程序可以连接到较旧的.xls和当前.xlsx文件,就像使用较旧的MS Access(Office到MS Excel的同级文件) .mdb和当前的.accdb文件一样。 Simply change the versions: Excel 8.0; 只需更改版本即可: Excel 8.0; to Excel 12.0 Xml; Excel 12.0 Xml; .

In fact, you do not even need to specify the Provider in the inline SQL commands by using the following format: 实际上,您甚至不需要使用以下格式在嵌入式SQL命令中指定Provider

...INNER JOIN [Excel 12.0 Xml;HDR=Yes;Database=C:\Path\To\Excel\File.xlsx].[SHEETNAME$]

With MS Access (highly advised for database needs, and available on all Windows machines, regardless of MSAccess.exe app install or not), you would not need to specify Excel parameters: 使用MS Access(强烈建议满足数据库需求,并且无论是否安装MSAccess.exe应用程序,无论是否安装MSAccess.exe,它都可在所有Windows计算机上使用),您无需指定Excel参数:

...INNER JOIN [C:\Path\To\Access\File.mdb].[TABLENAME]

...INNER JOIN [C:\Path\To\Access\File.accdb].[TABLENAME]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM