• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 851
  • Last Modified:

ComboBox Value Empty

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!
0
Golfer219
Asked:
Golfer219
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
rspahitzCommented:
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?
0
 
zorvek (Kevin Jones)ConsultantCommented:
Try:

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

Kevin
0
 
Golfer219Author 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
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
zorvek (Kevin Jones)ConsultantCommented:
The code wasn't being executed until the dialog was closed.

Fixed.

Kevin
ComboBox-Test.xls
0
 
zorvek (Kevin Jones)ConsultantCommented:
The problem is that you are showing he form modally which means the form is shown and the code does not continue until the form is closed. I moved the code into the form Activate event handler so it is run when the form is activated.

Kevin
0
 
dlmilleCommented:
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
0
 
dlmilleCommented:
Whooops - userform code at bottom....

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

Dave
0
 
dlmilleCommented:
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
0
 
Golfer219Author Commented:
Perfect!  Thanks a ton!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now