CopyFromRecordset仅复制和粘贴前一行,即使Excel中存在多条记录

ewm0tg9j  于 2023-01-18  发布在  其他
关注(0)|答案(4)|浏览(338)

我有一个Excel工作表,其中包含类似表格的数据

strSQL = "SELECT S.FIELD_NAME1,S.FIELD_NAME2,S.FIELD_NAME3 from [SourceData$A1:IV6] S"

Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
cn.Open strCon
Set rs = CmdSqlData.Execute()
Worksheets("SourceData").Cells.ClearContent
Worksheets("AnswerData").Cells(2, 1).CopyFromRecordset rs

结果:

仅忽略第一行和其他记录。
我已经试过下面的查询。,

strSQL = "SELECT COUNT(*) from [SourceData$A1:IV6] S"

结果是5
请告诉我为什么其他记录没有复制到记录集中?

kdfy810k

kdfy810k1#

下面是一个成功粘贴记录集的子例程。
请注意,通过intMaxRow和intMaxCol变量,它粘贴到的范围与记录集的大小相同:

Sub sCopyFromRS()
'Send records to the first
'sheet in a new workbook
'
Dim rs As Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim objXL As Excel.Application
Dim objWkb As Workbook
Dim objSht As Worksheet
  Set rs = CurrentDb.OpenRecordset("Customers", _
                    dbOpenSnapshot)
  intMaxCol = rs.Fields.Count
  If rs.RecordCount > 0 Then
    rs.MoveLast:    rs.MoveFirst
    intMaxRow = rs.RecordCount
    Set objXL = New Excel.Application
    With objXL
      .Visible = True
      Set objWkb = .Workbooks.Add
      Set objSht = objWkb.Worksheets(1)
      With objSht
        .Range(.Cells(1, 1), .Cells(intMaxRow, _
            intMaxCol)).CopyFromRecordset rs
      End With
    End With
  End If
End Sub

以该示例为模型,我将对您的代码尝试类似于以下内容的操作:

strSQL = "SELECT S.FIELD_NAME1,S.FIELD_NAME2,S.FIELD_NAME3 from [SourceData$A1:IV6] S"

Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
Dim intMaxCol as Integer
Dim intMaxRow as Integer

cn.Open strCon
Set rs = CmdSqlData.Execute()
intMaxCol = rs.Fields.Count
'- MoveLast/First to get an accurate RecordCount
rs.MoveLast 
rs.MoveFirst

If rs.RecordCount > 0 then
    '-thought you could put the MoveLast/First here but maybe not.
    intMaxRow = rs.RecordCount
    With Worksheets("AnswerData")
        .Range(.Cells(2,1),.Cells(intMaxRow+1,intMaxColumn)).CopyFromRecordset rs
    End With
End If
zujrkrfu

zujrkrfu2#

此答案取决于Excel中可用的ODBC数据库驱动程序。
由于我的corp env,我不得不使用非常旧的Oracle ODBC驱动程序。在我的示例中,Recordset.RecordCount始终为-1。默认情况下,不支持Recordset.MoveLastRecordset.MoveFirst。与原始问题一样,对Excel.Range.CopyFromRecordset(Recordset)的调用也只写入一行。
您可能需要对ADODB.Recordset进行不同的配置。请尝试以下代码:

Dim dbConn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim rng As Excel.Range
Dim rowCount As Long

' TODO: Init your dbConn here.
' TODO: Init your rng here.

Set rs = New ADODB.Recordset
' http://www.w3schools.com/ado/prop_rs_cursortype.asp
rs.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
rs.Open sql, dbConn
rowCount = rng.CopyFromRecordset(rs)

对于我的司机来说,这解决了这个问题。但是,您的里程可能会有所不同。
You can read more about ADO cursor types here.

lmvvr0a8

lmvvr0a83#

我遇到了同样的问题。
仅替换

set rst = conn.execute("SELECT * FROM SOMETABLE;")

call rst.open("SELECT * FROM SOMETABLE;", conn)

那问题就解决了
但是,我不知道为什么。

hc8w905p

hc8w905p4#

工作表(“AnswerData”)。单元格(2,1)。复制自记录集rs,1
复制实际记录
工作表(“AnswerData”)。单元格(2,1)。复制自记录集rs,5
复制下五条记录
工作表(“AnswerData”)。单元格(2,1)。复制自记录集rs,1,3
从实际记录中复制3个字段,因此...

相关问题