Defines a user-defined data type containing one or more elements.
elementname [(subscripts)] As typename
[ elementname [(subscripts)] As typename]
. . .
The Type statement has these parts:
Type Marks the beginning of a user-defined type.
usertype Name of a user-defined data type. It follows standard variable naming conventions.
elementname Name of an element of the user-defined data type. It follows standard variable-naming conventions.
subscripts Dimensions of an array element. You can declare multiple dimensions. The syntax of subscripts is described below.
typename One of these data types: Integer, Long, Single, Double, Currency, String (for variable-length strings), String * length (for fixed-length strings), Variant , or another user-defined type. The argument typename can't be an object type .
End Type Marks the end of a user-defined type.
The argument subscripts has the following syntax:
[lower To ]upper[,[lower To] upper] . . .
The To reserved word provides a way to indicate both the lower and upper bounds of an array variable's subscripts. Array subscripts can be negative. To can be used to specify any range of subscripts between -32,768 and 32,767, inclusive. The maximum number of array dimension is 60.
The Type statement can be used only in the Declarations section of a module Once you have declared a user-defined type using the Type statement, you can declare a variable of that type anywhere in your application. Use Dim, Global, ReDim, or Static to declare a variable of a user-defined type..
Line numbers and line labels aren't allowed in Type...End Type blocks.
User-defined types are often used with data records because data records frequently consist of a number of related elements of different data types.
The following example shows the use of static arrays in a user-defined type:
CityCode (1 To 100) As Integer ' Declare a static array.
County As String * 30
Dim Washington(1 To 100) As StateData
In the preceding example, StateData includes the CityCode static array, and the record Washington has the same structure as StateData.
When you declare a static array within a user-defined type, its dimensions must be declared with numeric constants rather than variables.
Type Statement Example
The example uses Type to create a user-defined type with three elements. A variable is then declared of the user-defined type, and data is assigned to each element. To try this example, paste the user-defined type definition into the Declarations section of a module. Paste the remaining code into the Declarations section of a form. Then press F5 and click the form.
Type TestRecord ' Create user-defined type.
CustNum As Long
CustName As String * 38
CustPurchase As String * 24
Sub Form_Click ()
Dim Answer, FileNum, Index, Msg, NL, TB ' Declare variables.
Static CustEntry As TestRecord ' Create output buffer.
NL = Chr(10): TB = Chr(9) ' Define newline, tab.
FileNum = FreeFile ' Get file number.
Open "TESTFILE" For Random As FileNum
Index = 1 ' Keep track of records.
Do ' Get data to fill all
CustEntry.CustNum = Index ' the elements of the
Msg = "Enter customer name:" ' data records.
CustEntry.CustName = InputBox(Msg)
If CustEntry.CustName = String(38, 32) Then Exit Do
Msg = "Enter purchase:"
CustEntry.CustPurchase = InputBox(Msg)
Msg = "Customer #"
Msg = Msg & CustEntry.CustNum & NL
Msg = Msg & CustEntry.CustName & NL
Msg = Msg & CustEntry.CustPurchase & NL
Msg = Msg & NL & "Is this information correct?"
Answer = MsgBox(Msg, 4) ' Show record, confirm.
If Answer = 6 Then ' If Yes
Put FileNum, Index, CustEntry
Index = Index + 1 ' Increment record.
Close FileNum ' Close file.
Msg = "The records you input have been written to a file. "
Msg = Msg & "Choose OK to remove the test file from your disk."
MsgBox Msg ' Display message.
Kill "TESTFILE" ' Remove test file.