简体   繁体   English

Function Range.Copyfromrecordset 不显示右列

[英]Function Range.Copyfromrecordset do not shows right columns

I fill a recordset from a simple query with ADO and when manually printing it's all good and clear but when i try to transfer the Recordset in the Sheet's table only 3 columns are filled out of 4.我用 ADO 从一个简单的查询中填充了一个记录集,当手动打印它时一切都很好而且很清楚,但是当我尝试在工作表的表中传输记录集时,只有 3 列被填满了 4 列。

That's the only method i use and always worked.这是我使用并且一直有效的唯一方法。 I changed Range, pointed outside a table, used a single cell and a bigger range than what my query will return.我更改了范围,指向表格外部,使用了单个单元格和比我的查询返回的范围更大的范围。 All I got was 3 columns.我得到的只是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

Use below code before rst.open在 rst.open 之前使用下面的代码

rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic

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

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