[PDF] [PDF] Arrays and Collections - Access All In One

Multi-Dimensional Arrays At this point, VBA is aware that myIntegerArray will be an array Another type of array that VBA implements, is the Variant Array



Previous PDF Next PDF





[PDF] Excel vba array function multidimensional - AWS Simple Storage

Excel vba array function multidimensional You can declare arrays with as many dimensions as you need, even in excess of 20, although you will probably not 



[PDF] Programming Excel/VBA Part II (A Fring)

レ Multidimensional arrays are VBA variables which can hold more than one item related to several index sets (up to 60) · e g a two dimensional array is a 



[PDF] VBA Notes for Professionals

treated differently by the VBA Compiler ReDim Employees(100, 5) 'declaring an 2D array that can store 100 employees with 6 elements of information each, but 



[PDF] Arrays and Collections - Access All In One

Multi-Dimensional Arrays At this point, VBA is aware that myIntegerArray will be an array Another type of array that VBA implements, is the Variant Array



[PDF] Excel - Programmation VBA - LaBRI

2 ARRAYS VBA programming - Hervé Hocquard - University of Bordeaux Multidimensional arrays – Dim MyTable(1 to 10, 1 to 10) As Integer • Assignment



[PDF] Excel vba array length - Squarespace

The following is an introduction to the basic use of arrays for Excel VBA, as well Multi-dimensional arrays are also common in VBA programs, especially when 



[PDF] Arrays in Visual Basic - VbNet

2 sept 2016 · If you are looking for help on arrays in Visual Basic for Applications (VBA), You can create a multidimensional array by using nested array 



[PDF] Working with Array Functions and DLLs in Excel VBA

Let us first look at arrays in Excel and how to use them in VBA parameters for curve fitting or multidimensional integration where the routine is written in C

[PDF] multifamily energy efficiency rebate program

[PDF] multigraph

[PDF] multilayer switch configuration

[PDF] multilevel feedback queue implementation

[PDF] multilevel feedback queue scheduling tutorialspoint

[PDF] multilevel feedback queue scheduling code in java

[PDF] multilevel feedback queue scheduling program in c++

[PDF] multilevel inverter block diagram

[PDF] multilevel inverter ppt

[PDF] multilevel inverter project report

[PDF] multilevel inverter switching pattern

[PDF] multilevel inverter thesis

[PDF] multilevel inverters syllabus

[PDF] multilevel queue scheduling

[PDF] multimedia powerpoint presentation examples

Access VBA Made Easy

Arrays and

Collections

08 www.accessallinone.com

1 | P a g e

This guide was prepared for AccessAllInOne.com by:

Robert Austin

This is one of a series of guides pertaining to the use of Microsoft Access.

© AXLSolutions 2012

All rights reserved. No part of this work may be reproduced in any form, or by any means, without permission in writing.

2 | P a g e

Contents

Arrays and Collections ................................................................................................... 3

Declaring Arrays ......................................................................................................... 3

Referencing Arrays ..................................................................................................... 5

Fixed Length and Dynamic Arrays ............................................................................. 5

ReDim and Preserve ............................................................................................... 5

Variant Arrays ......................................................................................................... 6

Erasing an Array ......................................................................................................... 7

Split Function ............................................................................................................. 7

Join Function .............................................................................................................. 7

Multi-Dimensional Arrays .......................................................................................... 8

Collections ................................................................................................................ 10

Relationship with Objects ........................................................................................ 10

Properties Associated with Objects .......................................................................... 10

Practical Uses of Collections : Form and Report Controls ....................................... 11

Collections: Control. ControlType ........................................................................ 12

Checking if a Form is loaded .................................................................................... 12

Referencing Controls ................................................................................................ 13

Me keyword ........................................................................................................... 13

Full Form Reference ............................................................................................. 13

Sub Form Reference ............................................................................................. 14

Common Errors ........................................................................................................ 14

Not Releasing Memory ............................................................................................. 14

Out of Memory .......................................................................................................... 14

Sloooooow Response Times ..................................................................................... 14

Exception: Out of Bounds ........................................................................................ 14

Questions .................................................................................................................. 15

Answers ..................................................................................................................... 19

3 | P a g e

Arrays and Collections

Computing is all about sets of similar looking data; appointments, files, pictures, addresses, UDP packets, tracks, database records, patient records, library records, lots of records. These different data structures inside our programs, computers, hard-drives and memory will be stored as repeating rows making up arrays and collections. somewhat extended variant. This will lay the foundation for understanding Collections and appreciating the differences between the two structures and be able to choose which best suits your particular task. Traditionally, an Array has always been a block of memory put aside to hold values of a particular type. Its size is set at the time it is initiated and any element within it may be accessed randomly or sequentially. The best way to envisage an Array is like a table of data that is held in memory. A Collection is an object that holds references to other objects of a similar type. It is somewhat similar to an array, in that it holds a list of things, but a collection is normally dynamic in size and, over all, easier to use than an Array. Objects in a collection can also be randomly or sequentially accessed.

Declaring Arrays

You can think of an array as a row of boxes with a number on each, 0 to n. When we first declare an array we must at least state its type and may also state its size (we can set the size later if we wish). Firstly, we will create an array that will hold Integer types (whole numbers). 1

Dim myIntegerArray() as Integer

myIntegerArray : Array of Integers

Figure 8.1

The opening and closing parenthesis after the variable name are the indicator that myIntegerArray is an array. At this point, VBA is aware that myIntegerArray will be an array In this example we will set the size of the array when we declare it. We will make a 10 integer array. Each Integer takes up 4 bytes. 1

Dim myIntegerArray(10) as Integer

myIntegerArray: Array of Integers (0..9)

0 1 2 3 4 5 6 7 8 9

0 0 0 0 0 0 0 0 0 0

Figure 8.2

4 | P a g e

indicating the total items it can hold and use a redim statement to set the size later on in the code. 1 2

Dim myStringArray() as String

ReDim myStringArray(10)

myStringArray: Array of Strings (0..9)

0 1 2 3 4 5 6 7 8 9

Figure 8.3

9%$ LQLPLMOLVHV 6PULQJV PR ³´ MQ HPSP\ 6PULQJB (MŃO ŃOMUMŃPHU RI M VPULQJ PMNHV XS MP OHMVP 2

bytes. 1 2

Dim myFloatArray() as Float

ReDim myFloatArray(10)

myFloatArray: Array of Floats (0..9)

0 1 2 3 4 5 6 7 8 9

0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

Figure 8.4

VBA initialises Floats to 0.0. A float takes up 8 bytes. 1 2

Dim myDateArray() as Date

ReDim myDateArray (10)

myDateArray: Array of Dates (0..9)

0 1 2 3 4 5 6 7 8 9

00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00 00:00:00

Figure 8.5

VBA initialises Dates to 00:00:00. A date takes up 8 bytes. 1 2

Dim myBooleanArray() as Boolean

ReDim myBooleanArray (10)

myBooleanArray: Array of Boolean (0..9)

0 1 2 3 4 5 6 7 8 9

False False False False False False False False False False

Figure 8.6

VBA initialises Boolean values to False. A Boolean takes up 1 byte.

5 | P a g e

Referencing Arrays

Continuing with our row of boxes analogy, an array is referenced by its name and the box we wish to work with. For example, to get the value of box 0 we use myIntegerArray(0); to reference box 9 we use myIntegerArray(9). 3 4 5 6 7 myIntegerArray(0) = 10 myIntegerArray(1) = 36 myIntegerArray(2) = 77 myIntegerArray(4) = 87 myIntegerArray(5) = -10 myIntegerArray: Array of Integers (0..9)

0 1 2 3 4 5 6 7 8 9

10 36 77 87 -10 0 0 0 0 0

Figure 8.7

Here are some useful Strings, 10 Top-Level Domains: 3 4 5 6 7 P\6PULQJ$UUM\ 0 ³8.´ P\6PULQJ$UUM\ D ³0(´ P\6PULQJ$UUM\ 1 ³58´ P\6PULQJ$UUM\ 6 ³F20´ P\6PULQJ$UUM\ 2 ³+5´ P\6PULQJ$UUM\ 7 ³H1)2´ P\6PULQJ$UUM\ 3 ³G(´ P\6PULQJ$UUM\ 8 ³1(7´ P\6PULQJ$UUM\ 4 ³)5´ P\6PULQJ$UUM\ E ³(8´ myStringArray: Array of Strings (0..9)

0 1 2 3 4 5 6 7 8 9

UK RU HR DE FR ME COM INFO NET EU

Figure 8.8

Fixed Length and Dynamic Arrays

One of the headaches with arrays is that they are static blocks of memory and are not designed to change in size. If we want to add another 5 domain names to myStringArray we have to re-declare the array. Oh, and by the way, doing so usually gives you back a new clean array!

ReDim and Preserve

VBA offers the ReDim function which performs much of the leg-work involved in changing array as you change its size. 1 2 3 4 Dim myIntegerArray() as Integer µ GHILQH MUUM\ YMULMNOH ReDim myIntegerArray(10) µ VHP MUUM\ VL]H MQG PHPRU\ MOORŃMPLRQ myIntegerArray(0) = 22 µ VHP 0 PR 22 ReDim Preserve myIntegerArray(20)µ H[PHQG MUUM\ SUHVHUYLQJ 0 22

Figure 8.9

The standard ReDim function would destroy the old array and make a new one; with the Preserve keyword included, VBA creates the new array of the new size and copies over the previous arrays values, making them available to us.

6 | P a g e

A fixed-length array is what the above arrays are called ± they cannot be changed. A dynamic array is more flexible allowing the array to grow and shrink in size over time without having to recreate the array data and structure.

Variant Arrays

Another type of array that VBA implements, is the Variant Array. Variant arrays handle all primitive types and each element of the array can be loaded with any data type. This ŃRQPUMVPV RLPO ³VPMQGMUG´ MUUM\V ROLŃO ŃMQ OROG RQO\ RQH SULPLPLYH GMPM P\SHB Variant arrays handle just like regular arrays, requiring us to ReDim to change the number of variables it can store. 1 2 3 4 5 6 7 8 µ $ YMULMQP MUUM\ ŃMQ OROG MQ\ SULPLPLYH GMPM P\SH NXP LP LV

µ MŃPXMOO\ VPRUHG MV MQ RNÓHŃP

Dim myVariableArray As Variant

myVariableArray = Array(10)

P\9MULMNOH$UUM\0 ³)LUVP HOHPHQP´

myVariableArray(1) = 2 myVariableArray(2) = new Date(#12-09-1989#)

Figure 8.11

7 | P a g e

Erasing an Array

Erasing an array is so important that VBA ± a language that usually makes things easy for programmers ± provides a dedicated function to release memory held by an array. If you scope, but you are advised to explicitly erase array structures when finished with them. Once 1 2 3 4 5 6 7 8

Dim myVariableArray() As Variant

ReDim myVariableArray(10)

myVariableArray(0) = 1 myVariableArray(1) = 2 myVariableArray(2) = 3 Erase myVariableArray µ P\9MULMNOH$UUM\ OMV QR PRUH GMPM MQG PXVP NH

Figure 8.12

Split Function

The split function splits a string into an array of strings based on some delimiter. The following example demonstrates splitting a string based on spaces. 1 2 3 4 5 6 7 8

Sub SplitFunction()

Dim i As Integer

Dim myArray() As String

myArray = Split("here;we;go;again!", ";")

For i = LBound(myArray) To UBound(myArray)

Debug.Print myArray(i)

Next i

End Sub

Figure 8.13

Join Function

Join does the exact opposite of split; it requires an array and a delimiter and returns a single string. 1 2 3 4 5

Sub JoinFunction()

Dim myArray() As Variant

myArray() = Array("here", "we", "go", "again", "!")

Debug.Print Join(myArray(), " ")

End Sub

Figure 8.14

8 | P a g e

Multi-Dimensional Arrays

All the arrays shown above are one-dimensional arrays. It is also possible to create an array with more than one dimension. For example, you may have an array of week numbers with days to hold an Integer number. 1 2

Dim myIntegerArray() as Integer

ReDim myIntegerArray(52,7)

Figure 8.15

In the above code, we tell Access to create an array of 364 elements (7×52). In the code below, we create a 3×5 array, fill certain elements with values and print it to the immediate window. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15quotesdbs_dbs17.pdfusesText_23