我在表单中有一个列表框。单击它会导致表单跳转到另一个记录。它应该突出显示一个项目并跳转到正确的记录。相反,它突出显示,然后立即清除选择,虽然它仍然跳到记录。当我使用标准记录选择按钮时,项目被正确突出显示。
我从.ListIndex属性读取所选项目的索引,因为当我测试选择了哪个项目时,Selected()在单选模式下不起作用。但是,.ListIndex是只读属性,我使用.Selected()来突出显示该项。
Option Compare Database
Option Explicit
Private Sub Form_Current()
Call highlightListBox
End Sub
Private Sub lbListBox_Click()
Dim rs As DAO.Recordset
Dim indx As Long
Set rs = Me.RecordsetClone
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
rs.FindFirst "[ID]=" & CStr(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If
End If
rs.Close
Set rs = Nothing
End Sub
Private Sub highlightListBox()
Dim lngIndx As Long
Dim lngI As Long
Dim bNoMatch As Boolean
lngIndx = 0
bNoMatch = True
If Me.NewRecord <> 0 Or IsNull(Me!ID) Then
For lngI = 0 To Me.lbListBox.ListCount - 1
Me.lbListBox.Selected(lngI) = False
Next lngI
Else
Do
lngIndx = lngIndx + 1
If CLng(Me.lbListBox.ItemData(lngIndx - 1)) = Me!ID Then
bNoMatch = False
End If
Loop Until CLng(Me.lbListBox.ItemData(lngIndx - 1)) = Me!ID Or lngIndx = Me.lbListBox.ListCount
End If
If Not bNoMatch Then
Me.lbListBox.Selected(lngIndx - 1) = True
End If
End Sub
答案 0 :(得分:0)
我收到了一个关于稍微不同的问题here的建议,但多亏了Remou我对此进行了整理。
新代码如下:
Option Compare Database
Option Explicit
Private Sub Form_Current()
Me.lbListBox = Me!ID
End Sub
Private Sub lbListBox_Click()
Dim rs As DAO.Recordset
Dim indx As Long
Set rs = Me.RecordsetClone
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
rs.FindFirst "[ID]=" & CStr(Me.lbListBox.ItemData(Me.lbListBox.ListIndex))
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If
End If
Me.lbListBox = Me!ID
rs.Close
Set rs = Nothing
End Sub
我没有意识到我实际上可以使用BoundColumn将值设置为列表框。通过这样做,设置突出显示和聚焦。我不确定,但我认为MultiSelection必须设置为0.在我的例子中,行
Me.lbListBox = Me!ID
完成工作:)
我希望这个答案可以帮助别人:)