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

How do I refer to an 'empty' listbox in VBA code?

Status
Not open for further replies.

wakayama

Programmer
Feb 6, 2009
3
GB
Hi - my program runs using a number of listboxes that only allow users to make single selections within each one.

I basically want to show an error message if at least one of these listboxes haven't been selected, but not sure how to define 'empty' when referring to a listbox. I've tried

If sheet1.listbox1.value = Null Then....

and this doesn't seem to work. Can anyone help?

 




Hi,

A suggestion might be to accumulate the selection values in an array, as they are selected.

Disable the control for the next step the process until you have values in all the array elements.

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
What about this ?
If IsNull(sheet1.listbox1.Value) Then....

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
wakayama said:
I basically want to show an error message if at least [red]one of these listboxes haven't been selected[/red], but not sure how to define 'empty' when referring to a listbox.
Are you asking how to determine if the list box does not contain any items or; if the list box does not have any items (in your case, just 1) selected?

Wouldn't ListBox1.ListCount equal zero if there were no items in the list box? Getting the number of items selected isn't as straightforward but could be kept in a private variable and public property.
Code:
Private mListBox1SelectedItemsCount As Integer 'Define at the Form level

Public Property Get ListBox1SelectedItemsCount() As Integer
    ListBox1SelectedItemsCount = mListBox1SelectedItemsCount
End Property

Private Sub ListBox1_Change()
    Dim ix As Integer
    
    mListBox1SelectedItemsCount = 0
    For ix = 0 To ListBox1.ListCount - 1
        If (ListBox1.Selected(ix)) Then mListBox1SelectedItemsCount = mListBox1SelectedItemsCount + 1
    Next
    
End Sub
 
Wakayama,

You say you're using listboxes that only allow single selections?

In a listbox with the MultiSelect property set to fmMultiSelectSingle, I always know that the listbox doesn't have a selection if ListIndex = -1 .

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top