简体   繁体   English

IMEX = 0的Excel ADODB查询未从第一行返回数据

[英]Excel ADODB query with IMEX=0 not returning data from the first row

I'm using ADODB in Excel VBA to query/update a named range in another (closed) Excel spreadsheet. 我在Excel VBA中使用ADODB来查询/更新另一个(封闭的)Excel电子表格中的命名范围。 SELECT queries with ADODB against a spreadsheet are simple; 使用ADODB对电子表格进行SELECT查询非常简单; however, figuring out the UPDATE query was a bear. 但是,弄清楚UPDATE查询是一个负担。 After much, much... much research and tweaking, I finally got the UPDATE to work. 经过大量的研究和调整,我终于使UPDATE起作用了。

However, I discovered that the SELECT query does not include the first row of data from the named range, ie, the recordset contains 19 rows while the named range has 20 rows and the first row of the recordset contains data from the second row of the named range. 但是,我发现SELECT查询不包括命名范围中的第一行数据,即记录集包含19行,而命名范围中有20行,而记录集的第一行包含来自第二行的数据。命名范围。 The named range does not have a header row... it's solid data all the way through. 命名范围没有标题行...从头到尾都是固态数据。

I know what you're saying, "oh, you need to set the HDR property to "No". :-) Nope, I tried that. See below. 我知道您在说什么,“哦,您需要将HDR属性设置为“否”。:-)不,我尝试过。请参见下文。

The only additional clue I have is that when IMEX is set to either 1 or 2, then the SELECT query returns all rows. 我仅有的另外一条线索是,当IMEX设置为1或2时,SELECT查询将返回所有行。 When IMEX is set to 0, then the first row is missing. 当IMEX设置为0时,则缺少第一行。 However, when IMEX is set to 1 or 2, the recordset is read-only and thus cannot be updated. 但是,当IMEX设置为1或2时,记录集是只读的,因此无法更新。 As far as I can tell, the only way to make the update work is to set IMEX to 0. 据我所知,使更新生效的唯一方法是将IMEX设置为0。

For cleanliness, the code below is an excerpt from the a larger function. 为了简洁起见,以下代码摘自一个较大的函数。

strSourcePath is the full path to the target workbook strSourceRange is the name of the range to update avNewValues() is a two dimensional array of the new values to write to the strSourcePath是目标工作簿的完整路径strSourceRange是要更新的范围的名称avNewValues()是要写入新值的二维数组。

I'm using 32-bit Office 2016 with 64-bit Windows 10. 我正在使用32位Office 2016和64位Windows 10。

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strconnect As String
Dim strSQL As String
Dim iRow As Integer, iCol As Integer

strconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=" & strSourcePath & ";" & _
            "Extended Properties=""Excel 12.0;HDR=No;IMEX=0"";"

strSQL = "SELECT * FROM " & strSourceRange & ";"

Set conn = CreateObject("adodb.connection")
conn.Open strconnect

Set rs = CreateObject("ADODB.Recordset")
rs.Open strSQL, conn, adOpenStatic, adLockOptimistic

'Update the values in the recordset
rs.MoveFirst
For iRow = 1 To UBound(avNewValues, 1)
    For iCol = 1 To UBound(avNewValues, 2)
        rs.Fields(iCol - 1).Value = avNewValues(iRow, iCol)
    Next
    rs.MoveNext
Next
rs.Update

Please try the following connection string for an XLSX file (note the keyword Xml in the string below): 请为XLSX文件尝试以下连接字符串(注意下面的字符串中的关键字Xml ):

 strconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & strSourcePath & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=0"";"

or this one for an XLSM file (note the keyword Macro in the string below): 或针对XLSM文件的此文件(请注意以下字符串中的关键字Macro ):

 strconnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & strSourcePath & ";" & _
        "Extended Properties=""Excel 12.0 Macro;HDR=No;IMEX=0"";"

Also, try setting IMEX=1 . 另外,尝试设置IMEX = 1 I had a similar issue and this combination of parameters resolved it. 我有一个类似的问题,这种参数组合解决了它。

Here's what I discovered. 这是我发现的。 If you don't specify the IMEX property at all, then it works, ie, all rows get returned and the recordset is read/write. 如果根本不指定IMEX属性,那么它将起作用,即,所有行都将返回并且记录集是可读写的。 This is perplexing because all the docs I can find say that the connection objects assumes a default IMEX value of zero. 这很令人困惑,因为我能找到的所有文档都说连接对象假定IMEX的默认值为零。 If that's true, then explicitly specifying the default value changes the connection object's behavior. 如果是这样,则显式指定默认值将更改连接对象的行为。

Before I discovered this, I had decided that I would just accommodate the bad behavior and add a header row to the data ranges. 在发现这一点之前,我已经决定只解决这种不良行为,并向数据范围添加标题行。 But now I'll just skip the IMEX property altogether and except the default. 但是现在,我将完全跳过IMEX属性,但默认情况除外。 This no doubt introduces some risk with data type mismatches, but the risk is low for the current project. 毫无疑问,这会带来数据类型不匹配的风险,但是对于当前项目而言,风险很小。

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

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