How to Use ADO’s GetRows method to quickly load data into an array
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 'Database - How to Use ADO's GetRows method to quickly load data into an array 'GetRows returns a variant array holding the Recordset's values. Simply assign 'a variant variable to this function's result. Then you can examine the values 'in the array instead of needing to loop through the Recordset's results. Dim conn As ADODB.Connection Dim statement As String Dim rs As ADODB.Recordset Dim values As Variant Dim txt As String Dim r As Integer Dim c As Integer ' Open the database connection. '... ' Select the data. statement = "SELECT * FROM Books ORDER BY Title, Year" ' Get the records. Set rs = conn.Execute(statement, , adCmdText) ' Load the values into a variant array. values = rs.GetRows ' Close the recordset and connection. rs.Close conn.Close ' Use the array to build a string ' containing the results. For r = LBound(values, 2) To UBound(values, 2) For c = LBound(values, 1) To UBound(values, 1) txt = txt & values(c, r) & ", " Next c txt = Left$(txt, Len(txt) - 1) & vbCrLf Next r ' Display the results. txtBooks.Text = txt |