PDA

View Full Version : Listbox problem



lifeson
09-19-2007, 08:12 AM
In the attached workbook I have a problem where on one of the forms if the user clicks the select button and no item in the listbox on the form is selected an error message I have set does not appear as expected.

I have stripped as much of the rubbish out as I can so it may be a bit buggy:bug:

But if you click the button on sheet1 and on the user form

select the following as they appear
New full system
Heating Only
Worcester Greenstar 24Ri

A form will appear showing packs linked that component.
If you use the select button without choosing an item in the list I would have expected the error message to appear from this code:
Private Sub cmdSelect_Click()
Dim iItem As Double
Dim val As String
Dim id As String
If lstAcc.ListIndex = -1 Then
msg = "No item has been selected," & vbNewLine
msg = msg & "Select an item from the list to add"
ans = MsgBox(msg, vbExclamation)
Else
For iItem = 0 To lstAcc.ListCount - 1
If lstAcc.Selected(iItem) = True Then
id = lstAcc.list(iItem) ' identifies pack number
val = lstAcc.list(iItem) ' identifies pack description
If frmQty Is Nothing Then Set frmQty = New FrmAccQty
frmQty.SelectedValue = val
frmQty.Show
lstAcc.Selected(iItem) = False
End If
Next
End If
End Sub

Thanks to XLD and others for getting me this far with this bit of work :bow: :bow:

tpoynton
09-19-2007, 08:21 AM
I am pretty sure that listindex is not -1 if nothing is selected in a multiselect listbox...the way I check is by adding a counter to the for loop that gets selected items, and pop the error message if the counter = 0.

if there is a better way, i'd love to know!

Bob Phillips
09-19-2007, 08:26 AM
It is because it is a multiselect listbox, and as you can select more than one, ListIndex doesn't get set. You can use this code



If lstAcc.ListIndex = -1 Or _
(lstAcc.MultiSelect <> frmMultiSelectSingle And lstAcc.ListIndex = 0) Then


which should work multi or single select.

tpoynton
09-19-2007, 08:43 AM
Thanks for the explanation and the snippet Bob!

Bob Phillips
09-19-2007, 09:30 AM
It is more accurate to say it doesn't get set to -1 (no idea why really). When you select an item, or many items, ListiNdex is the last selected item. This may be useful I guess, but I can't think of a use for knowing the last selected.

Whilst reflecting on this, it occurred to me that my code is flawed, and on testing, it is. If the first item is the last, or only, item selected, you will get the 'No item selected' message.

Therefore, I think you will be safer to go with Tim's method.

lifeson
09-19-2007, 09:51 AM
I am pretty sure that listindex is not -1 if nothing is selected in a multiselect listbox...the way I check is by adding a counter to the for loop that gets selected items, and pop the error message if the counter = 0.

if there is a better way, i'd love to know!

Is this where I would add the counter?
With lstAcc
ReDim aryItems(1 To iLastRow)
For i = 2 To iLastRow
If Cells(i, "A").Value = idx Then
.AddItem Cells(i, "B").Value
.list(.ListCount - 1, 1) = Cells(i, "C")
End If
Next i
End With
And if so, how would I do that? :think:

Thanks in advance..

Bob Phillips
09-19-2007, 10:06 AM
No, it would be in the select button code, something like this



Private Sub cmdSelect_Click()
Dim iItem As Double
Dim val As String
Dim id As String
Dim fSelected As Boolean

For iItem = 0 To lstAcc.ListCount - 1
If lstAcc.Selected(iItem) Then
fSelected = True
Exit For
End If
Next iItem

If lstAcc.ListIndex = -1 Or _
(lstAcc.MultiSelect And Not fSelected) Then
msg = "No item has been selected," & vbNewLine
msg = msg & "Select an item from the list to add"
ans = MsgBox(msg, vbExclamation)
Else
For iItem = 0 To lstAcc.ListCount - 1
If lstAcc.Selected(iItem) = True Then
id = lstAcc.list(iItem) ' identifies pack number
val = lstAcc.list(iItem) ' identifies pack description
If frmQty Is Nothing Then Set frmQty = New FrmAccQty
frmQty.SelectedValue = val
frmQty.Show
lstAcc.Selected(iItem) = False
End If
Next
End If
End Sub

lifeson
09-19-2007, 10:35 AM
As usual, many thanks
I can't believe how helpful everyone here is :bow: :bow: :bow:
I am sure I'll have further questions about this :thumb

Bob Phillips
09-19-2007, 11:00 AM
You should see us if we get to like you <g>