![](/img/trans.png)
[英]VBA big SQL-Query - Method 'CopyFromRecordset' of object 'Range' failed
[英]Function Range.Copyfromrecordset do not shows right columns
我用 ADO 從一個簡單的查詢中填充了一個記錄集,當手動打印它時一切都很好而且很清楚,但是當我嘗試在工作表的表中傳輸記錄集時,只有 3 列被填滿了 4 列。
這是我使用並且一直有效的唯一方法。 我更改了范圍,指向表格外部,使用了單個單元格和比我的查詢返回的范圍更大的范圍。 我得到的只是3列。
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
Dim conn As Object
Set conn = openCon 'other function I assure to works
Dim rst As Object
Dim query As String
Set rst = CreateObject("ADODB.RECORDSET")
'I don't think the problem is here but i really don't know what it could be
If ws.Range("tabArts").Rows.Count > 1 Then
ws.Range("tabArts").Rows.Delete
Else
ws.Range("tabArts").ClearContents
End If
query = ""
query = query & "SELECT art.code , "
query = query & " art.descr, "
query = query & " Sum (tb1.qty) 'Qty', "
query = query & " tb1.serials "
'serials is defined as NON NULL, so it's an empty string if not written
query = query & "FROM tb1 "
query = query & " LEFT OUTER JOIN articles art "
query = query & " ON tb1.idart = art.id "
query = query & "GROUP BY code, Descr, serials"
'This query return a full 4 columns table with 15 rows
rst.activeconnection = conn
rst.Open query
ws.Range("tabArts").CopyFromRecordset rst
'tabArts is a table with same query's headers
rst.Close
conn.Close
在 rst.open 之前使用下面的代碼
rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.