Access World Forums

Access World Forums (https://www.access-programmers.co.uk/forums/index.php)
-   Access FAQs (https://www.access-programmers.co.uk/forums/forumdisplay.php?f=23)
-   -   VBA Create Array (https://www.access-programmers.co.uk/forums/showthread.php?t=194738)

ajetrumpet 06-20-2010 04:57 PM

VBA Create Array
 
Static / Fixed Arrays

These are manually dimensionalized. Dimensions and number of elements are defined manually when arrays are declared. Short example:

Code:

    Dim myarray(6) As Variant
These have to be populated from their declaration specs. For example:

Code:

dim i as long

  for i = 0 to 5
      myarray(i) = i
  next i

Dynamic Arrays

These are unspecified in terms of dimensions and number of elements. Example:

Code:

    Dim myarray() As Variant
These have to be redimensionalized everytime an element or a dimension is added. The ReDim statement is used for this. Using ReDim alone will empty the area everytime it is redimensionalized so you have to populate it afterwards. Use this on arrays with more than one dimension. Example:

Code:

dim i as long
dim j as long

  for i = 0 to 5
      for j = 0 to 10
        ReDim(i, j)
      next j
  next i

Using ReDim Preserve saves the values that are already in the array and also redimensionalizes it. It saves time for one-dimensionals, like:

Code:

dim i as long

  for i = 0 to 5
      ReDim Preserve myarray(i)
      myarray(i) = i
  next i

More than one dimension usually does not work with Preserve because the last dimension must not be manipulated when using the key word.


All times are GMT -8. The time now is 09:54 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World