Visible and enable textbox based on combobox selection

  • I believe this is a change event but what I have written will not execute

    Code
    Private Sub combobox3_change()
    If Me.ComboBox3.Value = yes Then
    
    
        me.drtn.Visible = True
        me.drtn.Enabled = True
    End If
    End Sub
  • Re: Visible and enable textbox based on combobox selection


    Code
    Private Sub combobox3_change() 
             
            me.drtn.Visible = (comboBox3.Text = "Yes")
            me.drtn.Enabled = (comboBox3.Text = "Yes")
    
    
    End Sub



    There is no intrinsic value of 'Yes' so I guess it must be a string (as contained in the ComboBox)...


    Depending on your settings, that comparison may be case sensitive. Have a read in Help for 'Option Compare text' to set text comparisons to be case insensitive. You might also read about 'Option Explicit' to help avoid issues like you have with this.

  • Re: Visible and enable textbox based on combobox selection


    Thank you for the response. I do not know what "intrinsic value" means. I will read as you have suggested. I am using "option explicit" at the top of my module for this userform. Why does the "combobox3.text="yes" have to be in parentheses?

  • Re: Visible and enable textbox based on combobox selection


    Ah - sorry. Previous answer was using a mobile phone and I didn't see what you were referring to.


    I use parentheses around expressions just to make the order of evaluation obvious. Any expressions inside parentheses will be evaluated first. There is no real need to use them in this case, it's just habit on my part.

  • Re: Visible and enable textbox based on combobox selection


    Regardless of which example I apply, I cannot get the code to execute properly. The textbox "drtn" will not become visible with a yes, but will with a no. Switch it around and it works and still not what I need. If I decide to change the yes to a no, the textbox does not disappear. Interestingly, converting the combobox to a checkbox and it executes correctly. Here's what is really goofy; I apply the code from either example given to different combobox and it executes correctly. I will let you know how I get this running.Streub

  • Re: Visible and enable textbox based on combobox selection


    Basically there's no need to change the property 'enabled' if the control has been hidden.
    So this code suffices:


    Code
    Private Sub combobox3_change() 
        drtn.Visible = lcase(ComboBox3.Value) = "yes" 
    End Sub


    But for showing/hiding another control you'd better use a checkbox or a togglebutton instead of a combobox.

  • Re: Visible and enable textbox based on combobox selection


    if I have another combobox that has several options and I want it to make a textbox visible based upon the selection, do I use "case" or an array? I am unsure with array usage. I prefer the checkbox but space is limited on the userform. . .I think.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!