Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Posts
    42

    Unanswered: Listboxes: Testing if something has been selected?

    A simple question....

    I am attempting to test if something has been selected in a listbox before running a query but the tests I am doing seem to throw up errors.

    Anyone got suggestions?

    Here's my test:

    if (listbox.value = nothing) then
    .....Don't perform query
    else
    .....Perform query
    End if



    I also tried:

    if IsNull(listbox.value) then
    .....Don't perform query
    else
    .....Perform query
    End if


    Eventhough, the value of the listbox is 'nothing' if nothing from the list has been selected, it does not like to be tested against the 'nothing' constant.

    Thanks.

  2. #2
    Join Date
    Feb 2002
    Posts
    403
    Listboxes have a selected property you can use to find the items selected in a list. Like this

    With Me.List0

    For intI = 0 To .ListCount - 1

    If .Selected(intI) Then

  3. #3
    Join Date
    Apr 2003
    Posts
    42

    Thanks

    Thanks for the code.

    Seems like a long way to go just to perform this test.

    I thought there may have been some listbox constant that the listbox would have been set at if nothing was selected.

    I guess not.

    Well, it works now.

    Thanks again.

  4. #4
    Join Date
    May 2003
    Location
    Dallas
    Posts
    817
    Provided Answers: 5
    there is a "Not in list " event that fires if a user adds a value not in the combo box list

  5. #5
    Join Date
    Apr 2003
    Posts
    42

    True but...

    I want to use a listbox rather than a combo box.

  6. #6
    Join Date
    May 2003
    Location
    Dallas
    Posts
    817
    Provided Answers: 5
    then the only way to check is to enumerate through the list to check and compare the values. Sorry

    You could use a one column combobox
    size the box and then use the not in list - it is a hack way but a solution

  7. #7
    Join Date
    Jul 2009
    Posts
    14
    I figured that I would add to this post since I was helped out with a more complete answer:

    For a *non multi select* listbox, you can test for for the existence of a selected record by simply checking the bound columns value (assuming it is not blank in which you would have to look for = "" too...:

    If IsNull(lstItems.Column(0)) Then
    MsgBox ("Select a record for deletion.")
    Else
    ' do whatever..


    For a multi select listbox, which will always return null when tested as above, use the .ItemsSelected.Count:

    If Me.lstItems.ItemsSelected.Count = 0 Then
    MsgBox ("You must Select Record(s) for deletion.")
    Else
    ' do whatever


    Now, to get the values for what was selected, as stated above, you must for each through the listbox's records... but, just to check if anything was selected at all, the above works.

    Enjoy!

    (nod to pbaldy who helped me out with this...)

    regards,
    -madEG

  8. #8
    Join Date
    Apr 2009
    Posts
    10
    Thank you for this!!!!!
    Me.lstItems.ItemsSelected.Count = 0

    I was having a similar issue and this fixed it!!!! Whew!

Posting Permissions

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