VBA循环并编写ADODB Recordset

时间:2021-10-28 02:27:37

I'm new to working with recordsets, and I'm working on something where I retrieve a recordset from a sql server and then I need to loop through it pasting the values in an excel spreadsheet.

我刚开始使用记录集,我正在研究从sql server检索记录集然后我需要循环遍历excel电子表格中的值。

The order of the fields in the recordset is already correct, so I should be able to just move from cell to cell pasting each value within a range, but I'm not sure exactly how to do it. I know the recordset is correct because I outputed it to a file and everything looked in order.

记录集中字段的顺序已经是正确的,所以我应该能够在一个范围内从一个单元移动到单元格粘贴每个值,但我不确定如何做到这一点。我知道记录集是正确的,因为我把它输出到一个文件,一切都按顺序查看。

This is my code so far:

到目前为止这是我的代码:

Public Sub retrieve()

Dim rsTest As ADODB.Recordset
Set rsTest = New ADODB.Recordset
Set rsTest = DataManager.GetData()

Sheets("Planners").Activate
Dim cel As Range
Dim i As Integer
Dim rsFields As Variant

Do While Not rsTest.EOF

   For Each cel In ActiveSheet.Range("A3:H1000").Cells
     For Each rsFields In rsTest.Fields

     cel = rsTest(rsFields.Name)

     Next
  Next

Loop

End Sub

Thanks in advance for any and all help.

在此先感谢您的帮助。

1 个解决方案

#1


6  

If you want all the recordset fields then you can just do this:

如果您想要所有记录集字段,那么您可以这样做:

Public Sub retrieve()
Dim rsTest As ADODB.Recordset

    Set rsTest = DataManager.GetData()

    If Not rsTest.EOF Then
        Sheets("Planners").Range("A3").CopyFromRecordset rsTest
    End If

End Sub

#1


6  

If you want all the recordset fields then you can just do this:

如果您想要所有记录集字段,那么您可以这样做:

Public Sub retrieve()
Dim rsTest As ADODB.Recordset

    Set rsTest = DataManager.GetData()

    If Not rsTest.EOF Then
        Sheets("Planners").Range("A3").CopyFromRecordset rsTest
    End If

End Sub