devxlogo

Why Does The Typename Of A Form Return The Form’s Name, And Not Its Type?

Why Does The Typename Of A Form Return The Form’s Name, And Not Its Type?

You create a routine that uses the Typename() function to find out thetype of an object, and based on that type, performs certain actions. However,when used in a Form object, the value returned by Typename() is not thegeneric object class “Form.” It is, instead, the specific objectclass “Form1.”Try running this sample:

 Private Sub Form_Load()	Dim frm as Form	set frm = Me	Debug.Print TypeName(frm), frm.Name 	'<< prints: Form1 Form1End Sub

You expect TypeName(frm) to resolve to Form instead of Form1 but itdoesn't. That is because the type of the object, in this case, is the formclass itself, Form1. That you assigned it to a Form object does nothingexcept to prevent you from calling any of the Form1 objects methods andproperties directly in code. Similarly, a control never gives a type nameof "Control." Instead, the type name is always something like"CommandButton" or "PictureBox."Interestingly, while the TypeName function returns the most specificname, the If-TypeOf syntax will match either the generic class type orthe specific object class. In other words, both of these If statementswould return True:

 Set frm = MeIf TypeOf frm Is Form1 Then Debug.Print "Form1"If TypeOf frm Is Form Then Debug.Print "Form" '<< both stmts are true
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist