Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to determine whether textbox exists on userform

    Now that I have been able to create a userform control (textbox) through VBA, I am finding that once it's created, I will eventually have to remove it as it's only temporary.

    How can I determine if my newly created textbox exists (but maybe hidden behind another control) before I try to remove it?

    Using
    Code:
    Me.MultiPage1.Pages(1).Controls.Remove "tb_cupe_nrf"
    on the userform where this temporary textbox doesn't exist clearly will cause an error. I need to avoid this line if the textbox does not exist.
    I'm thinking something like ...

    If "tb_cupe_nrf" exists then
    remove it
    Otherwise
    keep going

    I've done some Googling, but found nothing that I could adapt.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    JennEx,

    An easier way is to just use VBA's builtin error handling capabilities. Since all you want to do is get rid of the textbox you don't really care if it generates an error since that means it's not there which is what you want so you just tell VBA to ignore the error.
    Code:
    On Error Resume next    '*** If a command causes an error proceed with the following command ***
    Me.MultiPage1.Pages(1).Controls.Remove "tb_cupe_nrf"
    On Error GoTo 0            '*** Return to normal error handling ***
    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,641
    Thanks
    115
    Thanked 652 Times in 594 Posts
    Jennex,

    You could also use some code like this to test if the control exists. If it does, it will delete the control. Note that your line of code only removes the control if the control was created at runtime not at design time

    Code:
    For Each ctrl In Me.Controls
        If ctrl.Name = "tb_cupe_nrf" Then
            Me.MultiPage1.Pages(1).Controls.Remove "tb_cupe_nrf"
            Exit For
        End If
    Next ctrl
    HTH,
    Maud

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    If you are adding/removing the text box then it would actually be a lot simpler to just hide/show it. This avoids a lot of fiddly work when you need to 'add it back'.

    Me.Controls("tb_cupe_nrf").Visible = Not Me.Controls("tb_cupe_nrf").Visible

    You can then use this same Visible property to treat the textbox as "not being there" without looping through all the controls or avoiding error handling
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    New Lounger
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks to all for your replies. It's nice to be able to have a selection of different ways to do the task at hand. If one way proves to be problematic, it's nice knowing there are fallbacks!

Posting Permissions

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