Control "Types"

E

Ed

I want to print out the values of certain aspects of the various controls on
a user form. However, different controls support values that other controls
do not. So if I try to get the ".text" value of a checkbox, I will get an
error. Likewise, if I try to get a ".caption" value of a textbox = error.

Is there a way to determine the 'type' of control that is being 'seen,'
something like:

For Each oControl in MyForm.Controls
atype=oControl.type (this is not a valid value) <<<<<
If atype="Checkbox" then
'do this
elseif atype = "Textbox" then
'do this
etc.
 
H

Helmut Weber

Hi Ed,

maybe "TypeName" helps, in principle:

Private Sub CommandButton1_Click()
Dim oCtrl As Control
For Each oCtrl In Me.Controls
MsgBox TypeName(oCtrl)
Next
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
P

Perry

Various, here's one

for each ctl in me.controls
debug.print typename(ctl)
next

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
B

Beth Melton

Use TypeOf. Such as:

For Each oControl In MyForm.Controls
If TypeOf oControl Is TextBox Then
'do this
ElseIf TypeOf oControl Is CheckBox Then
'do this
End If
Next oControl

Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Co-author of Word 2007 Inside Out:
http://www.microsoft.com/MSPress/books/9801.aspx#AboutTheBook

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
T

Tony Jollans

Be very careful using TypeOf. It can catch you out.

If TypeOf oControl Is CheckBox Then

will fail for a checkbox on a userform because it will actually test against
a Word Form Checkbox Control and not a Userform Checkbox Control. You must
be specific about which type of Checkbox you want ....

If TypeOf oControl Is MSForms.CheckBox Then

should work. The more general TypeName works because it just compares names
as strings, not object types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top