ACC2000: How to Use Visual Basic for Applications to Fill an Array

ID: Q210442


The information in this article applies to:
  • Microsoft Access 2000

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


SUMMARY

This article shows you two Visual Basic for Applications methods to fill an array. The first method fills an array when you know the number of elements in the array. The second method fills an array when you do not know the number of elements in the array.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you need to reference the Microsoft DAO 3.6 Object Library.


MORE INFORMATION

This example demonstrates how to fill a one-dimensional array when you know the number of elements.

  1. Create a module and type the following line in the Declarations section if it is not already there:


  2. 
    Option Explicit 
  3. Type the following procedure:


  4.  
    Function FillOneDimArray()
       Dim intCounter As Long
       Dim dbSample As DAO.Database
       Dim rstSample As DAO.Recordset
       Dim lngRecordCount As Long
       On Error GoTo ErrorHandler
       Set dbSample = CurrentDb()
       Set rstSample = dbSample.OpenRecordset("Employees")
    
       ' Get number of records.
       With rstSample
          .MoveLast
          lngRecordCount = .RecordCount
          ' Create the (zero-based) array.
          ' Address elements starting from row 0 rather than 1.
          ReDim AnArray(lngRecordCount - 1)
          ' Fill the array.
          .MoveFirst
          For intCounter = 0 To lngRecordCount - 1
             AnArray(intCounter) = ![LastName]
             .MoveNext
          Next intCounter
          ' View the array contents.
          For intCounter = 0 To lngRecordCount - 1
             Debug.Print AnArray(intCounter)
          Next intCounter
          .Close
       End With
       dbSample.Close
       Exit Function
    
    ErrorHandler:
       MsgBox Error
       Exit Function
    End Function
     
  5. To test this function, type the following line in the Immediate window, and then press ENTER:
    
    ? FillOneDimArray() 
    Note that you receive the following results;


  6. 
       Davolio
       Fuller
       Leverling
       Peacock
       Buchanan
       Suyama
       King
       Callahan
       Dodsworth 
The next example demonstrates how to fill an array when you do not know the number of elements. It uses the Preserve argument of the ReDim statement to adjust the size of the array without destroying the array's contents. Realize that using Preserve can cause your code to run more slowly; therefore, if possible, it is better to create an array with a known number of elements.
  1. Create a module and type the following line in the Declarations section if it is not already there:


  2. 
    Option Explicit 
  3. Type the following procedure:


  4.  
    Function FillIndefArray()
       Dim dbSample As DAO.Database
       Dim rstSample As DAO.Recordset
       Dim intArrayCount As Integer
       Dim aryTestArray() As Variant
       Dim intCounter As Long
       
       Set dbSample = CurrentDb()
       Set rstSample = dbSample.OpenRecordset("Employees")
       intArrayCount = 0
       ReDim Preserve aryTestArray(0)
       
       ' Fill the array.
       With rstSample
          .MoveFirst
          Do Until rstSample.EOF
             ' Fill the array row with the last name.
             aryTestArray(intArrayCount) = ![LastName]
             ' Increase the number of elements in the array
             ' by one to accommodate the next record.
             ReDim Preserve aryTestArray(UBound(aryTestArray) + 1)
             intArrayCount = intArrayCount + 1
             .MoveNext
          Loop
       ' Remove the remaining empty array row.
       ReDim Preserve aryTestArray(UBound(aryTestArray) - 1)
       .Close
       End With
       dbSample.Close
       
       ' View the array contents.
       For intCounter = 0 To intArrayCount - 1
           Debug.Print aryTestArray(intCounter)
       Next intCounter
    End Function 
  5. To test this function, type the following line in the Immediate window, and then press ENTER:
    
    ? FillIndefArray() 
    Note that you receive the following results:


  6. 
       Davolio
       Fuller
       Leverling
       Peacock
       Buchanan
       Suyama
       King
       Callahan
       Dodsworth 

Additional query words:

Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.