Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate an array with recordset data

Tags:

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?

like image 702
Curtis Inderwiesche Avatar asked Mar 08 '10 22:03

Curtis Inderwiesche


People also ask

Is recordset an array?

A recordset is an array, and much more versatile than a VBA array (i.e., reference by column name and not just column index).

What is recordset in Excel?

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).


2 Answers

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

like image 100
Albert D. Kallal Avatar answered Sep 22 '22 18:09

Albert D. Kallal


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.

like image 38
Fionnuala Avatar answered Sep 21 '22 18:09

Fionnuala