none
Auto select if only one item in listbox

    Question

  • I have form that shows a number of listboxes each with a button to process the selected item. If no item is selected, the button shows a msgbox 'no item selected'. This makes sense when the listbox has multiple items but usually the box has a single item. I'd like the button to autoselect the single item when the button is pressed. I can use listcount property to check for a single item but how do I auto-select it? I tried using listindex but it did not like it. Thanks.
    Thursday, March 31, 2011 4:44 PM

Answers

  • I have form that shows a number of listboxes each with a button to process the selected item. If no item is selected, the button shows a msgbox 'no item selected'. This makes sense when the listbox has multiple items but usually the box has a single item. I'd like the button to autoselect the single item when the button is pressed. I can use listcount property to check for a single item but how do I auto-select it? I tried using listindex but it did not like it. Thanks.

    With Me.lstYourListbox
        If .ListCount = 1 Then
            .Value = .ItemData(Abs(.ColumnHeads))
        End If
    End With


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    Thursday, March 31, 2011 5:50 PM

All replies

  • If you are not showing the column heads (In Property Sheet "Column Heads" is set to No):

    Debug.Print Me.lstMyListbox.Column(0, 0)
    Debug.Print Me.lstMyListbox.Column(1, 0)
    Debug.Print Me.lstMyListbox.Column(2, 0)
    
    

    The first number in the Parentheses is the Listbox Column, the second number is the List box Row Number. This code will show the first three columns in the first row of the List box.  If you just want the first column use the (0, 0).

    If you are using the column heads then the first Row in the Listbox is the Column Head so the code would be:

    Debug.Print Me.lstMyListbox.Column(0, 1)
    Debug.Print Me.lstMyListbox.Column(1, 1)
    Debug.Print Me.lstMyListbox.Column(2, 1)
    
    

    Patrick Wood Gaining Access http://gainingaccess.net
    Thursday, March 31, 2011 5:45 PM
  • I have form that shows a number of listboxes each with a button to process the selected item. If no item is selected, the button shows a msgbox 'no item selected'. This makes sense when the listbox has multiple items but usually the box has a single item. I'd like the button to autoselect the single item when the button is pressed. I can use listcount property to check for a single item but how do I auto-select it? I tried using listindex but it did not like it. Thanks.

    With Me.lstYourListbox
        If .ListCount = 1 Then
            .Value = .ItemData(Abs(.ColumnHeads))
        End If
    End With


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html
    Thursday, March 31, 2011 5:50 PM