All
I have sub below which exports an ADODB-recordset to Excel. Before the export, the recordset is sorted. In the loop I print out the supplier name in the Immediate window where the values are indeed sorted upon supplier name.
However, in Excel the records are in the original order instead of "supplier name". How can I fix this? I found a solution for DAO-recordsets where the original recordset (rs) is put into another DAO-recordset (set rs2=rs.openrecordset) but in ADO this "openrecordset" does not exist.
I have sub below which exports an ADODB-recordset to Excel. Before the export, the recordset is sorted. In the loop I print out the supplier name in the Immediate window where the values are indeed sorted upon supplier name.
However, in Excel the records are in the original order instead of "supplier name". How can I fix this? I found a solution for DAO-recordsets where the original recordset (rs) is put into another DAO-recordset (set rs2=rs.openrecordset) but in ADO this "openrecordset" does not exist.
Code:
Private Sub ExportExcel(rs As ADODB.Recordset)
Dim app As Object
Dim wkb As Object
Set app = CreateObject("Excel.application")
Set wkb = app.workbooks.Add
app.Visible = True
rs.MoveFirst
rs.Sort = "[supplier name]"
While Not rs.EOF
Debug.Print rs.Fields("supplier name")
rs.MoveNext
Wend
app.range("A2").copyfromrecordset rs
End Sub