mysql - .CopyFromRecordset 正在截断粘贴到 Excel 中的数据

标签 mysql excel vba adodb

问题

为什么 .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_name 被截断为 3 个字符,company_name 被截断为 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个字符
  • 我的实际评论是使用单撇号 (') 进行注释的,
    不是 /* */

最佳答案

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

在打开记录集之前将 CursorLocation 值设置为 adUseClient,然后重新运行代码会导致我的输出达到应有的效果。

更改我的代码:

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

关于mysql - .CopyFromRecordset 正在截断粘贴到 Excel 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40361995/

相关文章:

php - 如何正确发送小数到mysql数据库?

sql-server - 请求的 OLE DB 提供程序 Microsoft.ACE.OLEDB.16.0 未注册。如果未安装 32 位驱动程序,请以 64 位模式运行该包

Excel公式: Count repetition factor in a list (Vlookup/Indirect help)

vba - Excel MaxIF 函数失败但作为子运行

excel - 分析和更新动态表宏 VBA

php - 仅当名称唯一时才将 csv 上传到 mysql(如果不存在则创建)

php - 无法将时间戳插入 MySQL 数据库

vba - 删除工作表中的所有内容

mysql - 使用 MySQL 的 SQL 语句的正确语法是什么

VBA 字符串范围