2014-07-08 64 views
0

我在窗体中有一个列表框。点击它会导致表单跳转到另一条记录。它应该突出显示一个项目并跳转到正确的记录。相反,它会突出显示并立即清除选择,尽管它仍会跳转到记录中。当我使用标准记录选择按钮时,项目被正确突出显示。列表框项目在点击时没有突出显示

我从.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

a会发现一个SSCCE有用... –

+0

我只是试图解释这个问题。希望这看起来更好 – Celdor

回答

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 

我没有意识到我其实可以设置一个值来使用绑定列的列表框。通过这样做,高亮和聚焦都被设定。我不知道,但我认为,多选,必须设置为0。在我的情况下,线

Me.lbListBox = Me!ID 

不工作:)

我希望这个答案可以帮助别人:)

相关问题