UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> TypeOf    

The TypeOf statement can be used to check if an object reference is a specified type of object. For example, we can use TypeOf to see if a Control is a Textbox, or a Form, or a Class Object.

In VBA, the Help for TypeOf can be found under the If/Then help file, and is sparing in it's description.


Basic Syntax

The basic syntax is as follows:

If TypeOf MyObject Is ObjectType Then

Substitute MyObject with your object reference, and ObjectType with the type of object you would like to test for. For example:

If TypeOf Me.ControlName Is Access.TextBox Then Msgbox Me.ControlName & " is a TextBox"

The "TypeOf X Is Y" clause can be used in many cases where a boolean variable can be returned. For Example:

While TypeOf ctl.Parent Is Access.Form = False
MsgBox Iif(TypeOf ctl.Parent Is Access.Form, "Is Form", "Is Not Form")

You can negate the return through one of the following two syntaxes:

If Not TypeOf ObjectReference Is ObjectType
If TypeOf ObjectReference Is ObjectType = False

Derived Objects and TypeOf

TypeOf can be used to compare objects to a derived object. For example, a Textbox control might be checked against an Access.TextBox, an Access.Control and an Object, all of which return True:

Debug.Print Iif(TypeOf Me.txtName Is Access.TextBox, True, False)
Debug.Print Iif(TypeOf Me.txtName Is Access.Control, True, False)
Debug.Print Iif(TypeOf Me.txtName Is Object, True, False)
Debug.Print Iif(TypeOf Me.txtName Is Access.Form, True, False)

This would yield the following results:


Custom (Class) Objects

TypeOf can be used on any object. For example, if we have a class clsMyClass, and we want to see if an object is an instance of MyClass, we can use the following:

If TypeOf MyObject Is clsMyClass Then MsgBox "It's of the MyClass type"

Derived Custom (Class) Objects

Similar phases to "Derived Classes" are "Sub-Classes" and "Class Interface Implementation." Regardless of the phase used, the TypeOf operator can test if an object implements the interface of another class (i.e. is a sub-class of another object class).

Suppose we have a parent class named FormState and two sub-classes that implement FormState's interface: NewDataFormState and ExistingDataFormState. We can perform the following tests:

Dim curentState as FormState
Set currentState = New NewDataFormState
If TypeOf currentState Is FormState Then Debug.Print "It's of the FormState type"
If TypeOf currentState Is NewDataFormState Then Debug.Print "It's of the NewDataFormState type"
If TypeOf currentState Is ExistingDataFormState Then Debug.Print "It's of the ExistingDataFormState type"

This would yield the following results:

It's of the FormState type
It's of the NewDataFormState type

Since the object currentState is not a sub-class of ExistingDataFormState, the TypeOf operator returns false so the statement does not print the message.


TypeOf cannot be used to determine if a variable is a user defined type, nor can it be used to see if a variable is of a standard type (Integer, String, Boolean, etc).

Edit Discussion
Custom Search

Thank you for your support!
This page has been accessed 10,891 times.  This page was last modified 16:52, 20 September 2012 by rabroersma. Contributions by Jack Leach  Disclaimers