VBA Express
Solved: Check if combo box is populated [Archive] - VBA Express Forum

PDA

View Full Version : Solved: Check if combo box is populated



asystole0
06-12-2010, 05:48 AM
Hi, I feel a bit stupid posting this as it seems really simple.

I have a combobox, I want to check if its populated with anything. I check to see if its empty with null that works



If ComboBox1.Value = Null Then
GoTo CarryOn
ElseIf ComboBox1.Value = ?WHATGOESHERE? Then


Thanks in advance!

Tinbendr
06-12-2010, 06:49 AM
There are a couple of approaches.

If ComboBox1.Value = "" Then
'More code
end if

If ComboBox1.ListIndex = -1
'More code
end if

asystole0
06-12-2010, 07:36 AM
There are a couple of approaches.

If ComboBox1.Value = "" Then
'More code
end if

If ComboBox1.ListIndex = -1
'More code
end if


Thanks, but im checking to see if there is actually an entry in the combobox not if its empty, im pretty sure the following do the same thing and only check to see if its empty?

null
-1
""

Maybe im missing something?

xld
06-12-2010, 07:36 AM
That shows if anything has been selected, if you want to know if it has been populated, test the ListCount property.

Tinbendr
06-12-2010, 07:50 AM
. to check if its populated with anything.Oops, my bad. And of course, Xld is correct.

asystole0
06-13-2010, 03:06 AM
Thanks, Ive attempted to code it as per below example but it doesnt work, any ideas?




If ComboBox1.ListCount = 0 Then
GoTo CarryOn
ElseIf ComboBox1.ListCount = 1 Then

Tinbendr
06-13-2010, 04:21 AM
Hmm.. Don't rightly know.

This example works on my machine.

Private Sub UserForm_Initialize()

If ComboBox1.ListCount = 0 Then
MsgBox "List Empty"
End If

With ComboBox1
.AddItem "One"
.AddItem "Two"
.AddItem "Three"
End With

If ComboBox1.ListCount > 0 Then
MsgBox "Combobox1 has " & ComboBox1.ListCount & " entries"
End If

End Sub

xld
06-13-2010, 05:31 AM
Is this a combobox from a form or from the Control Toolbox toolbar?

xld
06-13-2010, 05:31 AM
Is this a combobox from a form or from the Control Toolbox toolbar?

asystole0
06-14-2010, 12:08 AM
Is this a combobox from a form or from the Control Toolbox toolbar?

Its a combobox on a form.

xld
06-14-2010, 12:40 AM
Then as Tinbendr says, it should work.