Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextBox type not detected by code 2

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,509
US
I have a simple UserForm (Excel VBA) with one textbox, one combo box, and a command button.

Code:
Option Explicit

Private Sub CommandButton1_Click()

Dim c As Control

For Each c In Me.Controls
    Debug.Print c.Name & " is a " & TypeName(c)
    
    If TypeOf c Is TextBox Then[red]
        c.Value = ""[/red]
    End If
    
    If TypeOf c Is ComboBox Then
        c.Value = ""
    End If
Next c

End Sub

When I run this code, ComboBox Type is detected with no problem, but the TextBox Type is not working, the code never gets to the [red]red[/red] portion of my code. Even tho [tt]TypeName(c)[/tt] shows [tt]TextBox[/tt].
What is going on?


---- Andy

There is a great need for a sarcasm font.
 
Excel library, with higher priority, has hidden TextBox control, representing old forms control. Full reference is necessary: [tt]If TypeOf c Is MSForms.TextBox Then[/tt]

combo
 
Thanks, that worked. :)

So, to be safe, would be better to Full reference any control this way:
[tt]
If TypeOf c Is [blue]MSForms.[/blue]TextBox Then

If TypeOf c Is [blue]MSForms.[/blue]ComboBox Then
[/tt]


---- Andy

There is a great need for a sarcasm font.
 
Definitely, OptionButton, CheckBox, ListBox and TextBox are in both libraries. Similar purpose, different class names in excel: Button, DropDown, GroupBox.

combo
 
Interesting - I've never had an issue with the hidden legacy controls taking priority. Guess I've just been lucky. But yes, always best to be as explicit as possible when using TypeOf

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top