Listbox check loop won't work!

zfind

Registered User.
Local time
Today, 11:15
Joined
Jul 11, 2008
Messages
55
Hey all,

I'm trying to make a simple procedure that will check if any items in a given listbox have been selected and if so, continue, otherwise give an error. Here's what I've got:

Code:
For i = 0 To lstChannel.ListCount - 1
    If lstChannel.Selected(i) Then
    Exit For
    Else
    MsgBox "Channel must be selected"
    Exit Sub
    End If
Next i

With i dimmed as a variant.

The problem is that it won't recognise any of the list rows being selected except the first. If the first is selected, it continues as normal.
 
Actually, here's an excerpt from the Access Help File which should help:

Code:
Sub BoundData()
    Dim frm As Form, ctl As Control
    Dim varItm As Variant

    Set frm = Forms!Contacts
    Set ctl = frm!Names
    For Each varItm In ctl.ItemsSelected
        Debug.Print ctl.ItemData(varItm)
    Next varItm
End Sub
 
Thanks Bob, so within the for loop I could put a condition to Exit For based on ItemsSelected? It's pretty close to what I have, I'll give it a try though.
 
Yeah, some slight modifications should do it for you.
 
If you are just interested in finding out if any items are selected (or not) then you should be able to just refer to

If Me.YourListBox.ItemsSelected.Count = 0

or

If Me.YourListBox.ItemsSelected.Count > 0
 
If you're just checking to make sure something was selected:

If Me.lstChannel.ItemsSelected.Count = 0 Then
...
 
So simple when you know how :) Above .count = 0 was just what I needed, works great.

Thanks!
 

Users who are viewing this thread

Back
Top Bottom