We help IT Professionals succeed at work.

ComboBox Value Empty

Golfer219
Golfer219 asked
on
Medium Priority
885 Views
Last Modified: 2012-05-11
I'm trying to write code that hides certain objects on a user form if a combobox's value is empty.  I've tried a series of different options but can't seem to get it.  I was thinking something along the lines of

If UserForm1.ComboBox1.Value = Null Then

or


If UserForm1.ComboBox1.Value = "" Then

or


If UserForm1.ComboBox1.Value = Empty Then


Thanks for the help!
Comment
Watch Question

Commented:
Are you looking to see if the combobox's Text portion is empty (you can try .Text) or are you looking to see if the list is empty (.ListCount =0)?

="" should have worked, but it may not depending on context.

Can you show more code?  Like which event are you checking?
CERTIFIED EXPERT
Top Expert 2008

Commented:
Try:

If Trim(UserForm1.ComboBox1.Value) = vbNullString Or UserForm1.ComboBox1.ListIndex = -1 Then

Kevin

Author

Commented:
Thanks for the help.  I mocked up a quick test file to show you what I'm trying to do.  When you click the control to show the user form, both list boxes and the command button should be hidden since nothing is selected in the combobox.

Thanks!
ComboBox-Test.xls
CERTIFIED EXPERT
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2012
Top Expert 2012

Commented:
Try this in your Userform1 code:
 

And try this in your module:

 
Sub ShowUF1()

Load UserForm1
UserForm1.Show

End Sub

Open in new window


I tested it and its working here.  The load initializes the value to "" and LB/CB's invisible

Then, upon a change they become visible....

Is this where you're headed?

See attached - works for me...

Dave
Private Sub ComboBox1_Change()
    If ComboBox1.Value <> "" Then
        UserForm1.ListBox1.Visible = True
        UserForm1.ListBox2.Visible = True
        UserForm1.CommandButton1.Visible = True
    Else
        UserForm1.ListBox1.Visible = True
        UserForm1.ListBox2.Visible = True
        UserForm.CommandButton1.Visible = True
    End If
End Sub

Private Sub ListBox2_Click()

End Sub

Private Sub UserForm_Initialize()

    ComboBox1.Value = ""
    UserForm1.ListBox1.Visible = False
    UserForm1.ListBox2.Visible = False
    UserForm1.CommandButton1.Visible = True

End Sub

Private Sub UserForm_Terminate()
    Unload UserForm1
End Sub

Open in new window

ComboBox-Test-r1.xls
Most Valuable Expert 2012
Top Expert 2012

Commented:
Whooops - userform code at bottom....

I didn't see Zorvek's last post before i hit send... lol

Dave
Most Valuable Expert 2012
Top Expert 2012

Commented:
Good anyway - get a diversity of response... Different strokes - I like to go through the:

Load (which initiates the Initialize() event)
Show

and Unload (via Terminate) approach...


PS - my post sets you up for making them dissappear if the combobox is cleared on the active userform...

ALSO - my post was a bit of an error comedy - for completeness please review code again and file attached:


Cheers,

Dave
MODULE:
Sub ShowUF1()

Load UserForm1
UserForm1.Show

End Sub

USERFORM:

Private Sub ComboBox1_Change()
    If ComboBox1.Value <> "" Then
        UserForm1.ListBox1.Visible = True
        UserForm1.ListBox2.Visible = True
        UserForm1.CommandButton1.Visible = True
    Else
        UserForm1.ListBox1.Visible = False
        UserForm1.ListBox2.Visible = False
        UserForm1.CommandButton1.Visible = False
    End If
End Sub


Private Sub UserForm_Initialize()

    ComboBox1.Value = ""
    UserForm1.ListBox1.Visible = False
    UserForm1.ListBox2.Visible = False
    UserForm1.CommandButton1.Visible = False

End Sub

Private Sub UserForm_Terminate()
    Unload UserForm1
End Sub

Open in new window

ComboBox-Test-r1.xls

Author

Commented:
Perfect!  Thanks a ton!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.