User Defined Data Types

VBA lets you create custom, or user-defined, data types (a concept much like Pascal records or C structures). A user-defined data type can ease your work with some types of data. For example, if your application deals with customer information, you might want to create a user-defined data type named

CustomerInfo, as follows:

Type CustomerInfo

Company As String Contact As String RegionCode As Long Sales As Double End Type

Note You define custom data types at the top of your module before any procedures.

After you create a user-defined data type, you use a Dim statement to declare a variable as that type. Usually, you define an array. For example:

Dim Customers(l To l00) As CustomerInfo

Each of the 100 elements in this array consists of four components (as specified by the user-defined data type, CustomerInfo). You can refer to a particular component of the record as follows:

Customers(l).Company = "Acme Tools" Customers(l).Contact = "Tim Robertson" Customers(l).RegionCode = 3 Customers(l).Sales = l50674.98

You can also work with an element in the array as a whole. For example, to copy the information from

Customers(l) to Customers(2) , use this instruction:

Customers(2) = Customers(l)

The preceding example is equivalent to the following instruction block:

Customers(2).Company = Customers(l).Company Customers(2).Contact = Customers(l).Contact Customers(2).RegionCode = Customers(l).RegionCode Customers(2).Sales = Customers(l).Sales

4 PREV

NEXT

0 0

Post a comment