Consulting

Results 1 to 11 of 11

Thread: Solved: Check if combo box is populated

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location

    Solved: Check if combo box is populated

    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!

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    There are a couple of approaches.

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

    If ComboBox1.ListIndex = -1
    'More code
    end if
    [/vba]

    David


  3. #3
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location
    Quote Originally Posted by Tinbendr
    There are a couple of approaches.

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

    If ComboBox1.ListIndex = -1
    'More code
    end if
    [/vba]
    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?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    That shows if anything has been selected, if you want to know if it has been populated, test the ListCount property.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Quote Originally Posted by asystole0
    . to check if its populated with anything.
    Oops, my bad. And of course, Xld is correct.

    David


  6. #6
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location
    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


  7. #7
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Hmm.. Don't rightly know.

    This example works on my machine.

    [vba]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
    [/vba]

    David


  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Is this a combobox from a form or from the Control Toolbox toolbar?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Is this a combobox from a form or from the Control Toolbox toolbar?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location
    Quote Originally Posted by xld
    Is this a combobox from a form or from the Control Toolbox toolbar?
    Its a combobox on a form.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Then as Tinbendr says, it should work.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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