PDA

View Full Version : Control.ControlType



jediderek
06-22-2017, 12:47 PM
Sorry for the newb questoin, So I cannot understand why I see all over the internet code below:


dim ctl as Control
ctl.ControlType

or something similiar like


dim ctl as Control
For each ctl in Me.Controls
if ctl.ControlType ........
end if
next

How are people getting the ControlType Method in Control? When I try ctl.ControlType <---- this isn't one of its methods listed.
Do you just pretend that it exist and hope it takes it?

Edit:
It works, but I just want to know why it doesn't recognize those methods. Like .ControlType, .Value, .Tag, etc

HiTechCoach
06-23-2017, 12:21 PM
Sorry for the newb questoin,


No need to apologize. Forums, like this one, are here to answer your questions.

You learn by asking questions. So keep on asking ….




So I cannot understand why I see all over the internet code below:


dim ctl as Control
ctl.ControlType

or something similiar like


dim ctl as Control
For each ctl in Me.Controls
if ctl.ControlType ........
end if
next

How are people getting the ControlType Method in Control? When I try ctl.ControlType <---- this isn't one of its methods listed.
Do you just pretend that it exist and hope it takes it?

Edit:
It works, but I just want to know why it doesn't recognize those methods. Like .ControlType, .Value, .Tag, etc

Great question.

What you are referring to is called Intellisense. In the Visual Basic Editor (VBE) when you type something, it looks to see if it can determine what you are doing and offer help.

The issue with using a generic object is that the VBE can’t help.


Dim ctl as Control ‘ creates a place to store many different control types

Unfortunately, for Intellisense a variable of type Control is not specific enough to be allow it to offer any help.
In this cases, it is up to the programmer to know what options are available.

Just because intellisense can’t help you do not mean it is not valid code. The compiler can still recognize it as valid code and it will execute.

Intellisense works great for standard VBA functions and other specific pieces of code. It does have limits when you start writing code beyond that.





' create an variable that is the type of Control
' this can hold any control type
'
Dim ctl As Control

' loop through all the controls on the current form or report (Me)
For each ctl In Me.Controls

' need to test the type of control to see if it is one that has the property/method we can to work with
If ctl.ControlType = ........ Then

End If

Next




In the code above, Intellisense can't determine at design time what the type of control. There can be many types of control on the form or report. There really is no way at design time for the Intellisense to know what type of control’s help to display. This is where the Programmer, a Human, is required to know what is needed.