Funk with the ListIndex property

Myriad_Rocker

Questioning Reality
Local time
Today, 08:00
Joined
Mar 26, 2004
Messages
166
Just when you think you have something figured out, BAM. You get slammed with something else you wouldn't expect.

I have a list box that is populated with the days of the week. I have a msg box popping up telling me the value of the item I click on right after I click it. I'm storing that index using the ListIndex property into a long variable called selectedday. Works fine within that sub. If I pick Wednesday, it returns Wednesday in the msg box.

However, I also have a button on the form that I'm trying to get to return the value of the selection when I click on it. I use the ListIndex property again to load the index of the selection into a new long variable. However, the ListIndex is ALWAYS -1. No matter what I select in the list box, the ListIndex always remains -1 in my button click sub.

I have a work around going right now where my selectedday long variable is in the general declarations section. It works fine there. But...why won't it work the other way?
 
Here's some code...

The click event of the List Box:
Code:
Private Sub recapdaysListBox_Click()
    If Me.recapschedulingComboBox.Value = "Weekly" Or Me.recapschedulingComboBox.Value = "Bi-Weekly" Then
        
        Dim selectedday As Long
        
        selectedday = Me.recapdaysListBox.ListIndex
        
        'I'm removing all selections here because Weekly and Bi-Weekly should not allow for multiple selections
        'I'm doing this because there is also a Daily option that does allow for multiple selections and I don't want to use two list boxes
        Me.recapdaysListBox.RowSource = "Monday;Tuesday;Wednesday;Thursday;Friday"
        Me.recapdaysListBox.Requery
        Me.recapdaysListBox.selected(selectedday) = True
        MsgBox Me.recapdaysListBox.ItemData(selectedday)
    End If
End Sub

Here's the code for the button click event:
Code:
Private Sub testcaseButton_Click()
    Dim selectedday As Long
    
    selectedday = Me.recapdaysListBox.ListIndex
    
    MsgBox Me.recapdaysListBox.ItemData(selectedday)
End Sub
 
MR,

I don't quite understand the logic of resetting the RowSource, Requerying
and Reselecting the ListBox ...

But, both MsgBoxes come up properly on my machine using A2003.

Wayne
 
If this is NOT a multiselect list box, then I would not recommend using the Selected() property. The Selected property merely turns off/on the black line, it does nothing to change the Value that is returned by a single-select list box control. The ListIndex property is also a little tricky depending on the type of list box you have. The ListIndex property will follow the Selected() property in the following manner:

- If the list box is multi-select, then the ListIndex returns the index of the last item clicked (Even if no black bars indicate a selection set!!), so if you UN-select an item, then un-selection will set the ListIndex to the last item clicked. If the Selected property of an item is set to True programatically, ListIndex will still return the item index of the last item clicked.

- If the list box is a single select, then the ListIndex will return the index of the item with the black bar selection indicator around it.

- The ListIndex property will return -1 IF the value of a single select list box is Null. ListIndex will return -1 for a multi-select list box if NO items have been clicked in a multi-select list box after setting the list box to Null. So setting the list box control to Null resets the ListIndex value. Please note that multiselect list box's are set to Null upon form open. Also note that setting a multi-select list box to Null does NOT clear all the black selection bars.

Now ... armed with that info, to me it is much simpler to just use the .Value property of single select list box than mess with the Selected() and ListIndex properties. The .Value property is the default property, so you don't even have to type it ...

>> I have a list box that is populated with the days of the week. <<
Assuming the list box is populated with a Value List ....

Monday;Tuesday;Wednesday;Thursday;Friday

Code:
Private Sub lstMyListBox_Click()
    MsgBox Me.lstMyListBox
End Sub
 
Private Sub cmbMyCommandButton_Click()
    MsbBox Me.lstMyListBox
End Sub
To clear a single select list box, just set it to Null ...

Me.lstMyListBox = Null

----

If you want the value returned to be an integer, AND the RowSource is a value list, just add another column. So set the column count to 2 and the row source to, plus set the column width to 0;1 in order to hide the integer you want returned by the list box ...

2;Monday;3;Tuesday;4;Wednesday;5;Thursday;6;Friday

lngMyLongVariable = Me.lstMyListBox

{note: I used the integers I did simply because they correspond to the vba constants of vbMonday, vbTuesday, etc ...}

.....

So ... does that help out?
 

Users who are viewing this thread

Back
Top Bottom