Items in a List Box are deselected when clicking on them.

ZikO

Registered User.
Local time
Today, 11:49
Joined
Dec 15, 2012
Messages
41
Hi

I have added a list box to a form with Single Selection mode on. Its purpose is to improve the interface. It contains items that represent all records. Clicking on the list box causes the form to jump to another record.

The problem is following: if I click on the List Box, it clears the selection and nothing is highlighted but it jumps to correct record. When I use standard record selection buttons, it highlights the correct items.

I read the index of selected item from .ListIndex property because Selected() does not work in a Single Selection mode. However, this is read-only property and I cannot use this to highlight the item back from VBA. But when I use Selected() it is not working. I mean when I click again on the same item it's selected. It's weird. I attached a simple database file with this problem.




The second problem is, when using standard record selection buttons, access iterates through all records and then jump to empty one. That is not like a new record. I don't know which event to use to control this situation. I would like to deselect all items, let user enter the data and re-query the List Box with a new record.


Thanks for any help.
 

Attachments

Last edited:
Please provide DB in *.mdb format.
Hi. It converts down to Access 2003 but without the form; it is always removed. I cannot figure out why. Sorry.

EDIT. I've just tried to create similar form working with a compatible format file (access 2002-2003) but it does not save the form. It's beyond my comprehension :(
 
Last edited:
Ok,

I sorted out the problem.

This is the new code:
Code:
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

This line does the job for me.
Code:
Me.lbListBox = Me!ID

What I did not know before was that I could actually use BoundColumn of a list box and set a value to it. By doing so, access 2010 both highlights and sets focus to corresponding item. In my case, BoundColumn contains values from [tblTable].[ID].

Hope it can help someone else.
 

Users who are viewing this thread

Back
Top Bottom