Solved

how to move recordset into a vba array (similar to Excel code: varMyArray = myRange)

Posted on 2011-02-17
3
931 Views
Last Modified: 2012-06-22
In excel I often grab a range of cells and put them into a matrix for futher use.

In Access, I use a macro like the attached UDF.  
But, it occured to me that there might be an easier way and a better way to do it.

Does anybody have any comments?

Function RsToArray(rs As DAO.Recordset)
rs.MoveFirst
dim1ubound = rs.RecordCount
dim2ubound = rs.Fields.Count
Dim myArray()
ReDim myArray(0 To dim1ubound, 0 To dim2ubound - 1)
For i1 = 0 To dim1ubound
For i2 = 0 To dim2ubound - 1
myArray(i1, i2) = rs(i2)
Next
rs.MoveNext
Next
RsToArray = myArray
End Function

Open in new window

0
Comment
Question by:rberke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Assisted Solution

by:VTKegan
VTKegan earned 20 total points
ID: 34916462
A recordset is an array essentially.  You access rows and columns using indexes directly on the recordset.  To me it doesn't make sense doing what you are doing.

I would just operate on the recordset instead of creating my own matrix of a recordset.
0
 
LVL 28

Accepted Solution

by:
omgang earned 230 total points
ID: 34916498
I agree with VTKegan and prefer to work with the recordset directly.  But to your question the GetRows method may be worth looking into.  It will populate an array with records from a recordset.  It does the same thing as your function but will do so with a few less lines of code.
http://msdn.microsoft.com/en-us/library/bb221041(v=office.12).aspx

OM Gang
0
 
LVL 5

Author Comment

by:rberke
ID: 34917274
Getrows looks like it will be exactly what I want (other than a superficial difference in that it is the transpose of what I wanted).  

And, I actually agree with comments about keeping the recordset intact 99% of the time.

But maybe 1% of the time using a matrix/array approach is useful.  

By analogy, an Excel Range is also essentially an array, yet about 5% of the time it is convenient for me to stick a range into a vba array.  In fact, I have found that quite a few Excel Gurus like to use the ary = Range approach for certain functions.

Thanks for your help.

rberke

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question