I am attempting to move data from a recordset directly into an array. I know this is possible, but specifically I want to do this in VBA as this is being done in MS Access 2003.
Typically I would do something like the following to achieve this:
Dim vaData As Variant
Dim rst As ADODB.Recordset
' Pull data into recordset code here...
' Populate the array with the whole recordset.
vaData = rst.GetRows
What differences exist between VB and VBA which makes this type of operation not work?
What about performance concerns? Is this an "expensive" operations?
A recordset is an array, and much more versatile than a VBA array (i.e., reference by column name and not just column index).
You use Recordset objects to manipulate data in a database at the record level. When you use DAO objects, you manipulate data almost entirely using Recordset objects. All Recordset objects are constructed using records (rows) and fields (columns).
The following code works for me:
Dim rst As ADODB.Recordset
Dim vDat As Variant
Set rst = CurrentProject.Connection.Execute("select * from tblTemp4")
vDat = rst.GetRows
Do a debug-compile, as mentioned this could be ref issue. As noted, some perfer DAO, but keep in mind DAO requires you to do a movelast. ADO does not. These days, ADO or DAO really comes down to your preferance, and performance is rarely an issue. ADO tends to be a bit cleaner of a object model, but whatever your familer with is likey the best choice in most cases
The usual reason that your sample would not work is that the proper library for ADO has not been referenced (Tools->References, Microsoft ActiveX Data Objects x.x Library), otherwise, it should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With