Declaring and Using Arrays

Arrays are indexed elements that have the same data type. Each array element has a unique index number. Arrays can be static or dynamic. Static arrays have a fixed number of elements, whereas dynamic arrays have the option to grow in size. The lowest index of an array is 0 by default.

Here is an example of how to declare and use an array.

Sub DemoFixedArray()

'declare an array with 5 elements Dim arstrPictureFile(4) As String

'populate each array element with a value arstrPictureFile(O) = "Christmas.jpg" arstrPictureFile(l) = "Thanksgiving.jpg" arstrPictureFile(2) = "WinterVacation.jpg" arstrPictureFile(3) = "SummerVacation.jpg" arstrPictureFile(4) = "Anniversary.jpg"

End Sub

The preceding array illustrates how to declare a fixed-size array. In some cases, you may not know exactly how big the array needs to be. In such cases, you use a dynamic array. A dynamic array is declared without an upper bound index, as shown in the following example.

Sub DemoDynamicArray()

'declare an dynamic array Dim arstrPictureFile() As String

'once you have determined how big the array needs to be 'such as other code in between, then specify a size 'for the array

ReDim arstrPictureFile(50) As String

'populate the first 5 array elements with a value arstrPictureFile(O) = "Christmas.jpg"

arstrPictureFile(l) = "Thanksgiving.jpg"

arstrPictureFile(2) = "WinterVacation.jpg"

arstrPictureFile(3) = "SummerVacation.jpg"

arstrPictureFile(4) = "Anniversary.jpg"

End Sub

The ReDim statement is used to define the size of the array when it is known. Any values stored in the array when the ReDim statement is used are lost because the array is reset. The Preserve statement can be used in circumstances where you want to preserve the prior values in the array when using the ReDim statement.

Declaring and Using User-Defined Data Types

User-defined types allow you to create your own data types. User-defined types can contain various pieces of information of the same or varying data types. Here is an example:

Public Type typTripInfo strTripLocation As String dtTripStartDate As Date dtTripEndDate As Date strPhotoPath As String End Type

The preceding code can be placed in the General Declarations section of the module. Now, let's look at a sample procedure that uses the typTripInfo user-defined type.

Sub TestUserDefinedType()

'declare a variable as the user defined type typTripInfo Dim typRecentTrip As typTripInfo

'assign values to the typRecentTrip user defined type typRecentTrip.strTripLocation = "Italy" typRecentTrip.dtTripStartDate = "3-18-04" typRecentTrip.dtTripEndDate = "3-27-04" typRecentTrip.strPhotoPath = "c:\trips\Italy"

End Sub

In the preceding procedure, a local variable is declared as the custom data type typTripInfo. Then, values for each of the variables in the user-defined type are specified.

Declaring and Using Object Variables

Object variables are variables that reference objects, such as databases, recordsets, forms, or controls. Object variables allow you to create references with shorter names than the original object and to pass objects as parameters to procedures.

Here is an example of how to declare and use a text box object variable from a form module.

Sub TestObjectVariable()

'declare an object variable Dim txtPrice As TextBox

'point the object to the txtValue1 text box Set txtPrice = Me.txtValue1

'set the text value of the text box txtPrice.Text = "2000"

End Sub

The preceding procedure declares a new variable as a text box object and then points the new variable to the existing txtValue1 text box. A value of 2 0 0 0 is then assigned to the new variable, which actually ends up physically setting the value in the original txtValue1 text box on the form to which you have pointed the new variable.

0 0

Post a comment