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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check no selection if a listbox..

Status
Not open for further replies.

basepointdesignz

Programmer
Jul 23, 2002
566
GB
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:

Code:
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)..

Posted same thread in VB6 forum just on case..

Cheers,

Paul
basepointdesignzltd..
XP Pro..
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
 




Hi,

I just posted in the VB forum.
Code:
    Dim i As Integer, bSelected As Boolean
    bSelected = False
    For i = 1 To ListBox1.ListCount
        If ListBox1.Selected(i) Then
            bSelected = True
            Exit For
        End If
    Next
    If Not bSelected Then
        MsgBox "no selection"
    End If

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
Yeah hey Skip, still no joy. Can't find any info on it online at all, all the resources are for native vb listboxes, not vba msforms ones..


Cheers,

Paul
basepointdesignzltd..
XP Pro..
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
 
ListIndex = -1 ought to work with the MSForms ListBox in single selection mode, and Skip's solution should, with a minor correction, work for multiselect.

Dim i As Integer, 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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top