Consulting

Results 1 to 5 of 5

Thread: check if listbox selected

  1. #1
    VBAX Regular
    Joined
    Aug 2013
    Posts
    20
    Location

    check if listbox selected

    hello

    I have 2 listboxes. I need to check if either one, both or neither are selected. have tried a few things but none seem to work. Any ideas please?

    i've tried
    If IsNull(ListBox1) AND  IsNull(ListBox2) then
        MsgBox "Please select an Intrument or Account"
        'ElseIf IsNull(ListBox1) Then
        'MsgBox "account selected"
        'ElseIf IsNull(ListBox2) Then
        'MsgBox "product selected"
        End If

  2. #2
    VBAX Regular HaHoBe's Avatar
    Joined
    Aug 2004
    Location
    Hamburg
    Posts
    89
    Location
    Hi, Nmarkit,

    have you tried using ListBox1.ListIndex > -1 which would correspond to any choice having been made?

    Ciao,
    Holger

  3. #3
    VBAX Regular
    Joined
    Aug 2013
    Posts
    20
    Location
    Hi Holger, thanks for your response. Am afraid it doesn't seem to do the trick. the condition shows up as valid in all scenarios. so whether any list box item is selected or not i get the msg.

    If ListBox1.ListIndex > -1 Then
        MsgBox "Please select an Intrument or Account"
        End If
    Btw both listboxes are multi select. don;t know if that makes a difference
    Last edited by Nmarkit; 09-10-2013 at 05:51 AM.

  4. #4
    VBAX Regular HaHoBe's Avatar
    Joined
    Aug 2004
    Location
    Hamburg
    Posts
    89
    Location
    Hi, Nmarkit,

    what about this code?
    Dim lngItems As Long
    Dim lngSel As Long
    
    For lngItems = 0 To ListBox1.ListCount - 1
      If ListBox1.Selected(lngItems) Then
        lngSel = lngSel + 1
      End If
    Next lngItems
    
    If lngSel = 0 Then
      MsgBox "Please select an Intrument or Account"
    End If
    Ciao,
    Holger

  5. #5
    VBAX Regular
    Joined
    Aug 2013
    Posts
    20
    Location
    That worked perfectly! thanks a lot Holger.

Posting Permissions

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