Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List box multiselect - how do I deal with it being empty? 1

Status
Not open for further replies.

Welshbird

IS-IT--Management
Jul 14, 2000
7,378
DE
So far, I've tried checking for the listbox.listindex value, but this is zero if eitehr one or no options are selected.

I've tried looping around with some code like this:
Code:
Dim i As Integer
dim bSelected As Boolean    
bSelected = False    
For i = 0 To ListBox1.ListIndex        
If ListBox1.Selected(i) Then            
bSelected = True            
Exit For        
End If    
Next    
If Not bSelected Then        
MsgBox "no selection"    
End If
which I found searching here. But, if there are no selected items it falls out of the sub and what I need it to do is tell the user and then take them back to the form to fill something in.

Help anyone?



Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
I don't meant 'empty' do I - I mean if nothing has been selected.

Just cannot see how to cope with it at the moment.

Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
At the moment I'm doing this:
Code:
Private Sub GeogOK_Click()
Select Case GeogList.ListIndex
Case Is > 0
   ' here is the chunk of the code then.
Case Else
    MsgBox "Please select something"
End Select
   Unload Me
End Sub
Now admittedly this doesn't collapse, but it also closes the form, and I need this to take the user back to the form for the entry.

Am sure I'm missing something really obvious here, but I really am missing it!


Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
The Selected property has base 0, to count list items use ListCount:
Code:
Dim bSelected As Boolean
For i = 0 To Me.ListBox1.ListCount - 1
    bSelected = bSelected Or Me.ListBox1.Selected(i)
Next i
MsgBox bSelected

combo
 
But the listcount is surely counting the items which appear in the list box, rather than how many are selected.

I just want to know when none are selected and ideally to pop up a message and have the form still be there after the message is OK'd so the user can go ahead and choose something.

What I have now does work after a fashion, but it closes the form and the user has to select it again. So its not ideal..

Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 

Try:
Code:
Dim i As Integer
Dim bSelected As Boolean    

bSelected = False    

For i = 0 To ListBox1.[blue]ListCount - 1[/blue]
    If ListBox1.Selected(i) Then            
        bSelected = True            
        Exit For       
    End If    
Next    
If Not bSelected Then        
    MsgBox "no selection"    
End If
You can have [tt]bSelected = False[/tt] at the beginning, but the default value of Boolean is FALSE so you don't really need that line of code. But it is up to you.

Have fun.

---- Andy
 
For a multiselect listbox the Selected(i) property is the only way to check if a given item is selected. So one has to loop through each row and check it. The ListCount returns total number of ListBox rows, so the loop starts at 0 and ends at ListCount-1.

The ListIndex for multiselect listbox returns a row that has focus, it can be either selected or not. So if, say, you select items 1 and 2 and next deselect item 1, yhe ListIndex returns 0 and your first code sample checks only first row (deselected).

combo
 
OK - Si I have that now, but because of the way my spreadsheet works, if nothing is selected it still replots my graph.

So, I really need not just the message box, but for the user form to STILL be visible for the user to maker selections. At present, it gives the message and then does teh rest of teh gubbins anyway!

(or, if I move the second bit of gubbins, it drops out of the form and they have to click again. Which I also think isn't good for users).

Ta so far chaps!

Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
Flip Me!
Code:
Dim i As Integer
Dim bSelected As Boolean
bSelected = False
    For i = 0 To GeogList.ListCount - 1
        If GeogList.Selected(i) Then
    bSelected = True
    Exit For
End If
    Next
If Not bSelected Then
    MsgBox "Please select either RAD or HCE and then the required areas"
[red]    Exit Sub[/red]
End If
As simple as an EXIT SUB then!

Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
bSelected can be set as follows:

bSelected = Not IsNull(GeogList.Value)

or your last bit of code can be replaced in it's entirety with:

If IsNull(GeogList.Value) Then
MsgBox "Please select either RAD or HCE and then the required areas"
Exit Sub
End If
 

Well, that's the problem with computers - they will do what you TELL them to do, and not necessarily what you WANT them to do
:)

Have fun.

---- Andy
 

strongm, from the Help in Excel 2003 VBA:
Value Property
Specifies the state or content of a given control.
Syntax
object.Value [= Variant]
...
Value cannot be used with a multi-select list box.
Wouldn't Welshbird's ListBox qualified as multi-select?

Have fun.

---- Andy
 
Indeed. My mistake. Here are some alternatives for fun ...

Declare bSelected as Public. Then in the ListBox Change event:

Private Sub ListBox1_Change()
bSelected = True
End Sub

leaving us with

If bSelected Then
MsgBox "Please select either RAD or HCE and then the required areas"
Exit Sub
End If

Or do away with bSelected, and use the Tag property:

Private Sub ListBox1_Change()
ListBox1.Tag = True
End Sub


If ListBox1.Tag <> "" Then
MsgBox "Please select either RAD or HCE and then the required areas"
Exit Sub
End If
 
For a multi-select list box it is possible to deselect all items, bSelected will stay True.
You could count items selected instead:

[tt]Dim counter As Integer

Private Sub ListBox1_Change()
counter = counter + IIf(ListBox1.Selected(ListBox1.ListIndex), 1, -1)
End Sub[/tt]

...and use condition:

[tt]If counter=0 Then
MsgBox "Please select either RAD or HCE and then the required areas"
Exit Sub
End If[/tt]

combo
 

If counter is declared at the General Declaration, as soon as any item is seleted in the ListBox (counter = counter + 1, which would be 0 = 0 + 1, well, it is 1 at this point), counter will never have value of 0 again because you keep additing value to it in Change event, either 0 or 1. So if you start with 0, and then add 1, and then add anything after that, counter will never have 0 again :-(

Have fun.

---- Andy
 

Ooops, my mistake.

You did state 'count selected items' and your code will do it. I missed -1 part

Sorry...

Have fun.

---- Andy
 
>you keep additing value to it in Change event

Not at all. combo's code should be subtracting IF we are unselecting an item
 
Well, it seems to work even if I select all and then deselect all - so I'm happy enough!

Thanks for all help guys.

(strongm - it was your code I found originally - so twinkly thing coming your way)


Fee

"The cure for anything is salt water – sweat, tears, or the sea." Isak Dinesen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top