MS-Access / Getting Started

Type Statement

Use a Type statement in a Declarations section to create a user-defined data structure containing one or more variables.

Syntax

[Public | Private] Type typename
    {variablename [({<array dimension>},...)]
	As datatype}
    ...
End Type

where <array dimension> is

[lowerbound To ] upperbound

Notes

A Type statement is most useful for declaring sets of variables that can be passed to procedures, including Windows application programming interface (API) functions, as a single variable. You can also use the Type statement to declare a record structure. After you declare a user-defined data structure, you can use typename in any subsequent Dim, Public, Private, or Static statement to create a variable of that type. You can reference variables in a user-defined data structure variable by entering the variable name, a period, and the name of the variable within the structure. (See the second part of the example that follows.)

Include the Public keyword to declare a user-defined type that is available to all procedures in all modules in your database. Include the Private keyword to declare a user-defined type that is available only within the module in which the declaration is made. You must enter each variablename entry on a new line. You must indicate the end of your user-defined data structure using an End Type statement.

Valid datatype entries are Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, or one of the object types described earlier in this tutorial. You can also declare a userdefined variable structure using the Type statement and then use the user type name as a data type. You should always explicitly declare the data type of your variables. If you do not include the As datatype clause, Visual Basic assigns the Variant data type.

If you do not include an <array dimension> specification but you do include the parentheses, you must include a ReDim statement in each procedure that uses the array to dynamically allocate the array at run time in any variable that you declare as this Type. You can define an array with as many as 60 dimensions. If you do not include a lowerbound value in an <array dimension> specification, the default lower bound is 0. You can reset the default lower bound to 1 by including an Option Base 1 statement in the module Declarations section. The lowerbound and upperbound values must be integers, and upperbound must be greater than or equal to lowerbound. The number of members of an array is limited only by the amount of memory on your computer.

Note that a Type declaration does not reserve any memory. Visual Basic allocates the memory required by the Type statement when you use typename as a data type in a Dim, Public, Private, or Static statement.

Example

To define a user type structure named MyRecord containing a long integer and three string fields, declare a variable named usrContacts using that user type, and then set the first string to "Mike", first enter the following:

Type MyRecord
    lngID As Long
    strLast As String
    strFirst As String
    strMid As String
End Type

Then, within a procedure, enter the following:

Dim usrContacts As MyRecord
usrContacts.strLast = "Mike"
[Previous] [Contents] [Next]