.CopyFromRecordset 正在截断粘贴到 Excel 中的数据
.CopyFromRecordset is truncating data pasted into Excel
问题
为什么 .CopyFromRecordset
t运行 从我的记录集输出中匹配字符串?
我以前使用过 .CopyFromRecordset
很多次,但没有遇到过这个问题,但是由于某种原因,这个 VBA 代码导致我的字符串数据被 t运行 处理。我目前使用的代码如下:
当前代码
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
s 被 t运行 归类为 3 个字符,而 company_name
s 被 t运行归类为25 个字符。
编辑: 我已经遗漏了字段 4-7 作为这些(正确)returns 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 查询在我的 SQL 管理程序 (HeidiSQL) 中执行时工作正常 - none 的数据是 t运行cated .更奇怪的是,当我 运行 打开记录集后的这行代码时:
Debug.Print myRecSet.GetString
None的数据是t运行!只有当我使用 .CopyFromRecordset
时,数据才被 t运行 分类。
附加信息
- 我的实际
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
问题
为什么 .CopyFromRecordset
t运行 从我的记录集输出中匹配字符串?
我以前使用过 .CopyFromRecordset
很多次,但没有遇到过这个问题,但是由于某种原因,这个 VBA 代码导致我的字符串数据被 t运行 处理。我目前使用的代码如下:
当前代码
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
s 被 t运行 归类为 3 个字符,而 company_name
s 被 t运行归类为25 个字符。
编辑: 我已经遗漏了字段 4-7 作为这些(正确)returns 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 查询在我的 SQL 管理程序 (HeidiSQL) 中执行时工作正常 - none 的数据是 t运行cated .更奇怪的是,当我 运行 打开记录集后的这行代码时:
Debug.Print myRecSet.GetString
None的数据是t运行!只有当我使用 .CopyFromRecordset
时,数据才被 t运行 分类。
附加信息
- 我的实际
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