如何将数据从记录集填充到列表对象?
以下代码无法完全正常工作:
oCN.ConnectionString = "DRIVER={SQL Server};Server=SRVSQL;Database=TEST;"
oCN.Open
Dim sqlString As String
sqlString = "SELECT * FROM MYTABLE"
oRS.Open sqlString, oCN
With Feuil3.ListObjects("TableArticles")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.Delete
End If
' This make a 91 error
Call .DataBodyRange.CopyFromRecordset(oRS)
' This copy data into sheet, not into listobject
Call Feuil3.Range("A2").CopyFromRecordset(oRS)
End With
最佳答案
如果始终总是先删除.DataBodyRange
,则可以使用`.InsertRowRange'。
With Feuil3.ListObjects("TableArticles")
If Not .DataBodyRange Is Nothing Then .DataBodyRange.Delete
.InsertRowRange.CopyFromRecordset(oRS)
End With