Consulting

Results 1 to 3 of 3

Thread: Check no selection in a listbox..

  1. #1

    Check no selection in a listbox..

    Hi,

    I know that this is a simple one but i'm simple, so help me, lol..

    I want to trap error for if nothing has been selected in a listbox (its set to multiselect btw, just in case that matters)..

    I tried this:




     
    If LayoutLIST.ListIndex = -1 Then
    MsgBox "No, its not selected" Exit Sub End If




    ...as a test but it didn't seem to work (saw that code somewhere online - not the msgbox bit)..

    I know that the listbox in VBA is an MSForms listbox not a native VB one, so has different properties and all the info i've found online on how to do this doesn't work, so was wondering if someone on here knew what to do..
    Cheers,

    Paul
    basepointdesignzltd..

    XP Pro SP2
    Pentium Core 2 Q6600 Quad Core
    ASUS P5N-E SLI Motherboard
    4GB DDR2 RAM
    2 x SLI NVIDIA 8500GT SLi 1024MB DDR2 PCI-Express Graphics Cards

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Paul,
    Is this for excel or word?

    Try this instead of -1
    [VBA]If LayoutLIST.ListIndex = 0 Then[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    BTW, if your listbox *is* coming back with a listindex of '0', with nothing selected, then you probably have the listbox set as multiselect style.

    If thats what you are looking for (giving the users the ability to select none, one, or more than one selection) then the listindex is not a reliable method to check - the listindex will return only one number, even if more than one item is selected!

    To check for selections in a multiselect, you have to loop thru the items in the listbox, checking each with the lisbox.selected property.

    If you are only wanting to allow one selection in the listbox, set the liststyle to '0', and set your listindex to a default value at the start of your code, when you create the listbox. Since you cannot *unselect* all items when liststyle=0, you will *always* have a valid listindex of > -1.

Posting Permissions

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