Solved

Populate an Array using Get Reocrds

Posted on 2004-10-21
250 Views
Last Modified: 2008-03-17
I'm tryin gto populate an array with data from a recordset, using Get Records. Here is my code:

Delcarations
Type PSQueryDef
    ID As String
    startdate As Date
    stopdate As Date
End Type

Procedure:
Public Function PopulateMBArray()
Dim arrQueries() As PSQueryDef
Dim db As Database
Dim rs0 As Recordset
Dim qdf0 As QueryDef
Dim ID As String, startdate As Date, stopdate As Date
Set db = CurrentDb
Set qdf0 = db.QueryDefs("qryMBArray")
Set rs0 = qdf0.OpenRecordset()
rs0.MoveLast
rs0.MoveFirst
ReDim Preserve arrQueries(rs0.RecordCount - 1, 3)

    If Not rs0.EOF Then
        arrQueries = rs0.GetRows(rs0.RecordCount - 1)
    End If
rs0.Close

The code produces an error "Type Mismatch". There may be other problems too, I'm not familiar with arrays.
Also, can anyone recommend a book that addresses MS Access VBA /programming?
Thanks
RHK
0
Question by:rhk6446
    4 Comments
     
    LVL 11

    Accepted Solution

    by:
    RHK,
    Everything looks good, just a couple modifications...
        Dim arrQueries()
        Dim db As Database
        Dim rs0 As Recordset
        Dim qdf0 As QueryDef
        Dim id As String, startdate As Date, stopdate As Date
       
        Set db = CurrentDb
        Set qdf0 = db.QueryDefs("qryMBArray")
        Set rs0 = qdf0.OpenRecordset()
        rs0.MoveFirst
        ReDim Preserve arrQueries(rs0.RecordCount - 1, 3)
                If Not rs0.EOF Then
                arrQueries = rs0.GetRows(rs0.RecordCount - 1)
            End If
        rs0.Close
    0
     
    LVL 9

    Assisted Solution

    by:Bat17
    Just a minor tweak. It will still need to move last to avoid a record count of 1
    If working above A97 it will need a reference to DAO

     Dim arrQueries()
        Dim db As Database
        Dim rs0 As DAO.Recordset
        Dim qdf0 As QueryDef
        Dim id As String, startdate As Date, stopdate As Date
       
        Set db = CurrentDb
        Set qdf0 = db.QueryDefs("qryMBArray")
        Set rs0 = qdf0.OpenRecordset(, dbOpenDynamic)
        rs0.MoveLast
        rs0.MoveFirst
        ReDim Preserve arrQueries(rs0.RecordCount - 1, 3)
                If Not rs0.EOF Then
                arrQueries = rs0.GetRows(rs0.RecordCount - 1)
            End If
        rs0.Close

    HTH

    Peter
    0
     
    LVL 41

    Expert Comment

    by:shanesuebsahakarn
    Bear in mind that GetRows returns a *two dimensional* array irregardless of the dimensions of the array passed to it. That is, the dimensions of arrQueries does not matter.

    If your data contains:
    ID: 10        Start Date: 01/01/2004      End Date: 10/10/2004
    ID: 11        Start Date: 02/02/2004      End Date: 03/03/2004

    the array returned will have:
    Element:    Value:
    0,0             10
    1,0             01/01/2004
    2,0             10/10/2004
    0,1             11
    1,1             02/02/2004
    2,1             03/03/2004
    0
     
    LVL 10

    Expert Comment

    by:RichardCorrie
    why do you need an array?

    can you not manipulate using the recordset?

    /Richard
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now