How can I show a MS-Access listbox row highlighted? I'm usi开发者_开发知识库ng 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
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.
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.
I always set the value of the listbox:
Me!lstMyListBox = Me!lstMyListBox.ItemData(0)
精彩评论