Disable a cmdButton when no selection in list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use a cmdButton to print reports filtered by a list box with multi select
set to "simple" (http://allenbrowne.com/ser-50.html).
How can I disable the cmdButton when there are no selections. (If there are
no selections it runs the report with the last filter saved with the report)
 
In design view, make sure the cmdButton is disabled (and remember to save the
form with it still disabled - easy to forget after fiddling with the macros!).

Go to properties of the list box and use the builder button next to After
Update event. This event is triggered every time a selection/deselection in
the list made. The kind of code you could use is:

Private Sub ListBox_AfterUpdate()
If IsNull(ListBox) Then
cmdButton.Enabled = False
Else
cmdButton.Enabled = True
End If
End Sub
 
I don't think that will work like you expect it to. I would suggest using
the ListCount property of the List box.

Private Sub ListBox_AfterUpdate()
Me.MyCommandButton.Enabled = Me.MyListBox.ListCount <> 0
End Sub

If No selections have been made, the ListCount will be 0 which will return
False; otherwise, it will return True.
 
Pardon me.

ListCount counts the number of items in the list. It doesn't tell you how
many (if any) have been selected.

ItemsSelected.Count will give you a count of the items that have been
selected with a multi-select listbox.

So, I believe you would have to use

Me.MyCommandButton.Enabled = Me.MyListBox.ItemsSelected.Count <> 0
 
Back
Top