views:

131

answers:

3

How can I show a MS-Access listbox row highlighted? I'm using MS-Access 2007. I want the first row of a multiple-column listbox to be showed highlighted through VBA. I tried Me.LstSample.Selected(0) = True, but it doesn't work.

The code:

Private Sub LstStation_AfterUpdate()
With Me.LstSample
    If IsNull(Me.LstStation) Then
        .RowSource = ""
    Else
        .RowSource = _
            "SELECT * FROM Samples WHERE S='" & Me.LstStation.Value & "'"
    End If
    Call .Requery
    If Not IsNull(Me.LstStation) Then
      Me.LstSample.Selected(0) = True
    End If
End With
End Sub
A: 

Try and change the

Me.LstSample.Selected(0) = True

to

Me.lstSample.SetFocus
Me.lstSample.ListIndex = 0

I'm not sure why the original code isn't working, but I tend to use the ListIndex property instead of the Selected property.

KevenDenen
A: 

I totally stripped the code and resetted the properties of the controls and now it works fine!

I'm figuring out what the differences are and will let you all know.

waanders
I finally found it. Because I set the ColumnHeads propery to True, the first row contains the headings, so to highlight the first (data)row I've to use LstSample.Selected(1)=True instead of LstSample.Selected(0)=True
waanders
A: 

I always set the value of the listbox:

  Me!lstMyListBox = Me!lstMyListBox.ItemData(0)
David-W-Fenton