How to select top recrod in a listbox by default

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dpatel1682
    New Member
    • Jul 2010
    • 35

    How to select top recrod in a listbox by default

    I need to have top record selected in my listbox when I load my form. Please help

    Thanks
  • munkee
    Contributor
    • Feb 2010
    • 374

    #2
    Me.ListBox1.Sel ected(1) = True

    Where listbox1 is the name of your listbox and (1) denotes the item number in your listbox to select.

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      Actually, since the index is Zero-based, it would be (0) not (1) for the first item, and I believe you need to set focus to the listbox first:
      Code:
      Private Sub Form_Load()
        ListBox1.SetFocus
        Me.ListBox1.Selected(0) = True
      End Sub
      Linq ;0)>

      Comment

      • JohnL

        #4
        Wow -- this simple post ended a long journey of frustration for me today! Awesome! If you have a listbox on a Tabbed Form (not subform) - see the following: I was using the Listbox as a navigation tool to select a records. Each tab's Listbox had a different query associated with it so it had different recordsets. When I would click on the tab it wouldn't change to the new recordset associated with the listbox for that tab/page. You must do the following to force the new recordset to be selected thus displaying the correct records. Note you must use an on change event for the tab control NOT the page. If you use an event associated with the page -- it is referencing the blank part of that page -- not the tab. Note: Tab control value is the page index it starts are zero not 1. So the example below is for my Page Index 1 which is the second page. Use the following for example:

        Private Sub TabCtl0_Change( )
        If TabCtl0.Value = 1 Then
        List1439.SetFoc us
        Me.List1439.Sel ected(0) = True
        End If

        End Sub

        Comment

        Working...