0

I'm trying to get records from a database and fill them into excel. This is how I get the data and fill it (note: the query is simplified, I can't just ORDER BY DESC):

Sub FillPersons(ByRef connection As ADODB.connection)
    Dim recordSet As ADODB.recordSet
    Set recordSet = New ADODB.recordSet
    Dim sql As String

    sql = "SELECT TOP 2 Id FROM Persons"

    recordSet.activeconnection = connection 
    recordSet.Open sql

    Dim a As Variant

    If Not recordSet.EOF Then
        a = recordSet.GetRows
        a.Reverse (a)
        Sheet1.Cells(10, 2).Resize(UBound(a, 1) + 1, UBound(a, 2) + 1).Value = a
    End If

    'Sheet1.Range("B10").CopyFromRecordset recordSet

    recordSet.Close
    connection .Close
    Set connection = Nothing
End Sub

Unfortunately, the result looks like this:

B10B11
2    1    

instead of

B10B11
1    2    

I've tried to reverse a but unfortunately I can't get it to work.

Is there a trick to get the correct result?

Thanks in advance

1
  • There's not enough data to ascertain a definite pattern, but I'm guessing it's in the right order, but in reverse?
    – Jonno
    Commented Feb 24, 2016 at 12:40

1 Answer 1

0

Why do you want to sort / reverse a instead of recordSet?

Use
recordset.Sort = recordSet.Fields(1).Name & " ASC"

More details: https://msdn.microsoft.com/en-us/library/ms675783(v=vs.85).aspx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .