Consulting

Results 1 to 6 of 6

Thread: Questions about "TypeOf" command

  1. #1
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location

    Questions about "TypeOf" command

    Ok, this may be obvious, but if it weren't for you good folks here, I never would have found this. In fact, I've been searching through the help in Word 2000 and I *still* can't find reference to the TypeOf command. Is this some "top secret" Microsoft command, or is it in there somewhere and I just can't find it?

    Now for the second - more curious question. In the following code, the textbox condition works, but the checkbox condition does not.
    [VBA]
    Dim ProjCtrl As Control

    For Each ProjCtrl In frmItems.Controls
    If TypeOf ProjCtrl Is TextBox Then
    MsgBox ProjCtrl.Name, vbOKOnly, "Text Control Found"
    ElseIf TypeOf ProjCtrl Is CheckBox Then
    MsgBox ProjCtrl.Name, vbOKCancel, "Check Control Found"
    End If
    Next
    [/VBA]

    However, when I put "MsForms." in front of the control type everthing works:
    [VBA]
    Dim ProjCtrl As Control

    For Each ProjCtrl In frmItems.Controls
    If TypeOf ProjCtrl Is MsForms.TextBox Then
    MsgBox ProjCtrl.Name, vbOKOnly, "Text Control Found"
    ElseIf TypeOf ProjCtrl Is MsForms.CheckBox Then
    MsgBox ProjCtrl.Name, vbOKCancel, "Check Control Found"
    End If
    Next
    [/VBA]

    I understand why it works with the MsForms in front, but could someone explain why without it the textbox works and the checkbox doesn't?

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    TextBox is a member of the Shapes class

    CheckBox is a member of the CheckBox class

    So perhaps it is equivilent to write Shapes with MSForms in front or not. But since the Checkbox has a seperate class it must be referenced with the MSForms.

    All I can say is MicroSoft is nothing if not inconsistent.

  3. #3
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    TypeName is probably a more reliable function to use. It should work regardless of the library used. TypeOf has many limitations when used with Controls, and specifying the library is one of them.

    In your case, Excel cannot distinguish which library to which your Checkbox control belongs. That's why it works once you qualify it with the MSForms library. I'm not sure why the code can distinguish the TextBox control. The only thing I see is that there are fewer libraries with textbox controls than checkbox controls.

    James
    "All that's necessary for evil to triumph is for good men to do nothing."

  4. #4
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Jacob and James have said most of it - TypeOf has its idiosyncracies! The default checkbox type is the one belonging to the word application (the type in a word form - not a userform, I think) whilst, because there isn't a textbox type in the application, the default textbox type is the first one it finds (the msforms one used in the userform). Clear as mud, I guess.

    To answer your first question, you will find a mention of it - only a brief mention, mind - in the Help for the IF statement - I'm a bit surprised you didn't look there first - LOL
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  5. #5
    VBAX Regular village_alchemist's Avatar
    Joined
    Jul 2004
    Location
    Michigan, US
    Posts
    32
    Location
    Thanks guys, guess that pretty much covers it. I tried the TypeName() function and although the help file doesn't specifically state it will work on controls, it works just fine. Even better, unlike TypeOf, it works in case statements!

    LOL Tony, maybe if I thought like Microsoft! But then again, if I did that my code would never work!

  6. #6
    VBAX Tutor jamescol's Avatar
    Joined
    May 2004
    Location
    Charlotte, NC
    Posts
    251
    Location
    Quote Originally Posted by village_alchemist
    LOL Tony, maybe if I thought like Microsoft! But then again, if I did that my code would never work!
    Good rule of thumb: If the help file doesn't provide what you need, navigate to http://support.microsoft.com and search there. 99% of the time I find what I need between it and Help.

    Since TypeName is just a string, you can actually use it anywhere you find it useful.

    Tony - Interestingly, when I ran this code in Off2003, the TextBox object wasn't recognized. The only control TypeOf recognized without qualifying the library was CommandButton! Go figure.

    In my mind, I envision the Office developers with a big cardboard Spinner used to determine these things? Guess CommandButton won this versions spin

    Cheers,
    James
    "All that's necessary for evil to triumph is for good men to do nothing."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •