PDA

View Full Version : Solved: Test for Null in Listbox Value



lawsonbooth
12-13-2006, 08:23 AM
How do you test for when a person has not selected any item in a listbox?

I have a listbox on a userform with two command buttons, one for submit and one for cancel. I am trying to check for the situation when a user fails to select a listbox item, but presses the submit instead of the cancel button. I have tested the following ways and all fail:


If userform1.listbox1.value = vbNull then
.Error code?
End if
If userform1.listbox1.value = ?? or _
userform1.listbox1.value = ? ? then
?error code?
End if
If isNull(userform1.listbox1.value) then
?error code?
End if
If isEmpty(userform1.listbox1.value) then
?error code?
End if
If isblank(userform1.listbox1.value) then
?error code?
End if


There must be a trick I do not know in my limited knowledge. Any help would be greatly appreciated.

Lawson:banghead:

Charlize
12-13-2006, 08:32 AM
you could use the on_click event of the listbox. Disable the ok button when form loads and enable it when clicked in listbox.

Charlize

lawsonbooth
12-13-2006, 09:14 AM
Charlize,

Thanks for your quick response. I tried what you suggested and on click can occur even when you just click anywhere in the listbox - you do not have to click on a listed item. I am trying to make sure they have selected an item listed unless they select the cancel button.

The little things like this will drive me crazy until I get an answer. I have been working on this for two days. Sometimes when you get into something like this you need to just stop, go away and come with a fresh approach.

When I tried the following statement again it worked. I do not know what I did outside of I totally shut down Excel, took a long break and came back and it worked.



xSelected = userform1.listbox1.value
if isnull(xSelected) then
'error
End If


Thanks for the help.

Lawson

Bob Phillips
12-13-2006, 09:24 AM
If Userform1.Listbox.ListIndex = -1 Then
'...