【问题标题】:.CopyFromRecordset is truncating data pasted into Excel.CopyFromRecordset 正在截断粘贴到 Excel 中的数据
【发布时间】:2020-11-25 02:01:55
【问题描述】:

问题

为什么.CopyFromRecordset 会从我的记录集输出中截断字符串?


我之前多次使用.CopyFromRecordset 并且没有遇到过这个问题,但是由于某种原因,这个 VBA 代码导致我的字符串数据被截断。我目前使用的代码如下:

当前代码

Sub GetTable()

Dim myConnObj As ADODB.Connection
Dim myRecSet As ADODB.Recordset
Dim SQLStr As String
Dim eRow As Long

/*Open connection to database*/
Set myConnObj = CreateObject("ADODB.Connection")
myConnObj.Open _
    "Driver={MySQL ODBC 5.3 ANSI Driver}; " & _
    "Server=SERVERNAME; " & _
    "Database=DATABASENAME; " & _
    "Uid=ID; " & _
    "Pwd=PASSWORD; " & _
    "Option=3"

/* Set SQL string */
SQLStr = "SELECT t.field1, t.field2, t.field3, t.field4, t.field5, t.field6, NULL as field7 "
SQLStr = SQLStr & "FROM table AS t WHERE ISNULL(t.field4) AND NOT ISNULL(t.field5) GROUP BY t.field3;"

/* Open recordset */
Set myRecSet = CreateObject("ADODB.Recordset")
myRecSet.Open SQLStr, myConnObj, adOpenStatic

/* Set end row */
eRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

/* Clear current range */
ThisWorkbook.Sheets("Sheet1").Range("A2:G" & eRow).ClearContents

/* Copy data into worksheet */
ThisWorkbook.Sheets("Sheet1").Range("A2").CopyFromRecordset myRecSet

/* Close off objects */
Set myRecSet = Nothing
myConnObj.Close
Set myConnObj = Nothing

End Sub

电流输出

这段代码的输出如下所示:

provider_name     id              company_name
ABC               AA1234          Example Limited
ABC               AB1231          Another Example Limited
ABC               AC1235          Another Company Example L
DEF               AA1238          E.g. Limited
GF&               AB1261          Final Example Company Lim

然而,无论出于何种原因,每个单元格都被填充,provider_names 被截断为 3 个字符,company_names 被截断为 25 个字符。

编辑:我省略了字段 4-7,因为这些(正确)的所有数据都返回 NULL 值。

期望的输出

我的输出应该是这样的:

provider_name     id              company_name
ABC               AA1234          Example Limited
ABCDEF            AB1231          Another Example Limited
ABC               AC1235          Another Company Example Ltd
DEFGHI            AA1238          E.g. Limited
JK&L              AB1261          Final Example Company Limited

我的尝试

在我的 SQL 管理程序 (HeidiSQL) 中执行 SQL 查询时工作正常 - 没有任何数据被截断。更奇怪的是,当我打开记录集后运行这行代码时:

Debug.Print myRecSet.GetString

没有数据被截断!只有当我使用.CopyFromRecordset 时,数据才会被截断。

其他信息

  • 我的实际SQLStr 是 313 个字符长,因此拆分 字符串。
  • 我的实际查询只产生 86 行和 7 列。
  • company_name 最长为 56 个字符
  • 我的实际 cmets 使用单数撇号 (') 进行注释,
    不是/* */

【问题讨论】:

  • company_name 字段可以多长?
  • 最长的 company_name 是我应该得到的输出中的 56 个字符,但是对于未来的查询,这可能会更长。
  • 这两个输出似乎与嵌入式 SQL 语句不相符。您向我们展示了正确的代码吗?
  • p2p.wrox.com/classic-asp-databases/… 这个选项 3 与处理列宽有关
  • 感谢您的回复。 @destination-data 抱歉,我遗漏了第 4-7 列有点不清楚,因为它们只返回 NULL 值(已编辑问题)。

标签: mysql excel vba adodb


【解决方案1】:

从将myRecSet 添加到我在VBA 上的监视列表中,我注意到CursorLocation 值设置为adUseServer。我记得看到这通常设置为adUseClient

在打开记录集然后重新运行代码之前将 CursorLocation 值设置为 adUseClient 会导致我的输出正常。

更改我的代码:

/* Open recordset */
Set myRecSet = CreateObject("ADODB.Recordset")
myRecSet.CursorLocation = adUseClient  /* <--Added Code */
myRecSet.Open SQLStr, myConnObj, adOpenStatic

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多