See Also Example
Used in the Declarations section of forms and modules to set the default data type for variables and Function procedures .
DefInt letterrange [,letterrange] . . .
DefLng letterrange [,letterrange] . . .
DefSng letterrange [,letterrange] . . .
DefDbl letterrange [,letterrange] . . .
DefCur letterrange [,letterrange] . . .
DefStr letterrange [,letterrange] . . .
DefVar letterrange [,letterrange] . . .
The argument letterrange has the following form:
The arguments letter1 and letter2 specify the range of variables for which a default data type will be set. Each argument represents the first letter of variable names and can be any uppercase or lowercase letter of the alphabet. For instance, a letter range of A-D sets the default data type for all variables that begin with A, B, C, or D. The case of the letters in letterrange isn't significant.
The reserved word For example, in the following program fragment, you use (type in Deftype) determines which of the following data types will be the default setting of variables in letterrange: Integer(DefInt), Long(DefLng), Single(DefSng), Double(DefDbl), Currency(DefCur), String(DefStr), or Variant (DefVar). Message is a string variable:
Message = "Out of stack space."
A Deftype statement affects only the form or module in which it is used. For example, a DefInt statement in one module affects only the default data type of variables declared in that module; the default data type of variables in forms and other modules is unaffected. If not explicitly declared with a Deftype statement, the default data type for all variables and all Function procedures in forms and modules is Variant.
When a letter range is specified, it usually defines the data type for variables that begin with letters in the first 128 characters of the ANSI character set However, when you specify the letter range A-Z, you set the default to the specified data type for all variables, including any that begin with international characters from the extended part of the ANSI character set (128-255)..
Once the range A-Z has been specified, you cannot further redefine any subranges of variables using Deftype statements. In fact, once a range has been specified, if you include a previously defined letter in another Deftype statement, an error occurs. However, you can explicitly assign the data type of any variable, defined or not, using a Dim statement or a type-declaration character . For example, you can use the following code in the Declarations section of a form or module to define a variable as a Double even though the default data type is Integer:
Dim TaxRate As Double
You can use the following statement in a procedure to explicitly assign the data type of Pi:
Pi! = 3.141593
A type-declaration character always takes precedence over a Deftype statement. Deftype statements don't affect elements of user-defined types.
ANSI Character Set
Deftype Statement Example
The example uses the Deftype statements to set default data types of variables in two ranges. Variables beginning with the letters A-K will be Integer; variables beginning with L-Z will be String. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
NL = Chr(10) ' Define newline.
IntVal = 2.3455
Msg = "Notice that none of the string variables in this demo have "
Msg = Msg & "the $ type-declaration character appended to their "
Msg = Msg & "names." & NL & NL & "The DefStr statement defines "
Msg = Msg & "the default for variables starting with letters 'L' "
Msg = Msg & "through 'Z' to be of the string data type."
Msg = "Notice that the variable 'IntVal' has been defined as an "
Msg = Msg & "integer. Even though it is assigned a real value, it "
Msg = Msg & "behaves as an integer, i.e., IntVal = "
Msg = Msg & IntVal & " instead of 2.3455."
MsgBox Msg ' Display message.