Consulting

Results 1 to 4 of 4

Thread: Solved: Test for Null in Listbox Value

  1. #1
    VBAX Regular
    Joined
    Oct 2006
    Location
    Columbus, GA
    Posts
    42
    Location

    Solved: Test for Null in Listbox Value

    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

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    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

  3. #3
    VBAX Regular
    Joined
    Oct 2006
    Location
    Columbus, GA
    Posts
    42
    Location
    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

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    If Userform1.Listbox.ListIndex = -1 Then
    '...
    [/vba]
    Last edited by Bob Phillips; 12-13-2006 at 11:55 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •