Deselecting and Highlighting listbox items issue (1 Viewer)

johnbirt

Registered User.
Local time
Today, 03:34
Joined
Oct 31, 2009
Messages
20
I have a simple form bound to a table with 4 fields. On the form is an unbound listbox which is used to select a filter string for the table to which the form is bound. There is one problem which I cannot fathom.
I would like the selected item from the listbox to be deselected and the highlight removed after use. I use the AfterUpdate event of the listbox to apply the filter and deselect the item. Unfortunately the item is deselected Ok (Listindex is indeed set to -1) but the item always remains highlighted. I have tried other variations on this with other events but requery is not an option if the record is in save limbo so it has to be AfterUpdate.
This inability to de-highlight is annoying. In this case maybe its just cosmetic but there are other situations where it would be preferable.
Most grateful for any ideas/solutions.
John
Relevant code is:

Private Sub lstHolidayYear_AfterUpdate()
Forms!frmholidays.Filter = FilterString
lstHolidayYear.Selected(lstHolidayYear.ListIndex) = False
' The selected item is still highlighted after this
' but not selected i.e. Listindex is -1 after this
Me.FilterOn = True
Forms!frmholidays.Requery

End Sub
Everything else works exactly as required
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:34
Joined
Jan 20, 2009
Messages
12,856
Just move the focus somewhere else.
 

johnbirt

Registered User.
Local time
Today, 03:34
Joined
Oct 31, 2009
Messages
20
Doesn't work. The item is deselected and the focus is automatically moved to the first field/textbox at completion of AfterUpdate by the requery. I suppose I could use the GotFocus for that textbox and then select and deselect the highlighted item. that would require saving the listindex in a global - might work, but a sledgehammer to crack a peanut. There must be a simpler way but not unfortunately just moving focus. The point here is that an item being selected and it being highlighted seem to be 2 distinct states in this situation (bug?).

You can't select, requery and deselect in the AfterUpdate sub as VBA complains you haven't saved the record - otherwise that would do it. I used that in the form Onload with no problem - the list comes up with no selection or highlight if you select/deselect requery an item.
Possibly this forced highlighting is a feature or by design in 2007 but I would like at least to have the option of de-highlighting.
Ta for suggestion.
 

MarkK

bit cruncher
Local time
Yesterday, 19:34
Joined
Mar 17, 2004
Messages
8,187
Try setting the value of the list to null
Code:
Private Sub lstHolidayYear_AfterUpdate()
  someOtherControl.SetFocus
  lstHolidayYear = null
End Sub
 

johnbirt

Registered User.
Local time
Today, 03:34
Joined
Oct 31, 2009
Messages
20
Doesn't work. Last action of AfterUpdate is to set focus to 1st field and setting list to null following that has no affect.
 

jubb

Registered User.
Local time
Today, 12:34
Joined
Jul 27, 2005
Messages
50
Try this in the afterupdate event (ListName being the name of your list control):

Code:
Private Sub ListName_AfterUpdate()
    'Any other processing you need to do after update here

    Dim ctlSource As Control
    Dim varCurrentRow As Variant
    Set ctlSource = Me.ListName 
    For Each varCurrentRow In ctlSource.ItemsSelected
        ctlSource.Selected(varCurrentRow) = False
    Next varCurrentRow
    Set ctlSource = Nothing
end sub
 

DCrake

Remembered
Local time
Today, 03:34
Joined
Jun 8, 2005
Messages
8,632
Another way is to do it in two steps

First place the rowsource, as an sql string in the Tag property. Then

1. Me.ListBox.Rowsource = Null

2. Me.Listbox.Rowsource = Me.ListBox.Tag

David
 

johnbirt

Registered User.
Local time
Today, 03:34
Joined
Oct 31, 2009
Messages
20
Thanks David and Jubb for your suggestions. Unfortunately neither work. The issue appears to be that although highligted by clicking on the item it doesn't appear to be selected when the listbox AfterUpdate is called.
To test things I created a single field table and a form with a listbox bound to it. The form code had nothing in it other than AfterUpdate and that had a single line
msgbox ...ItemsSelected.Count
which returned 0. On placing Requery before that, it returns 1. For some strange reason in my original code that involves setting the filter string,requery of the form does not "select" the item. It apears that AfterUpdate is called whenever you requery and a consequence of that appears to be that selecting an item via code in the listbox AfterUpdate is a no no. Access complains that a record hasn't been saved.
In fact I checked with the simplified scenario above with virtually no code at all that the listbox OnClick event also returns 0 for ...ItemsSelected.Count so the item though highligted at this point is not marked as selected.
The issue of the highlight is of relatively minor importance compared to my lack of understanding(?) of the events sequence.Maybe AfterUpdate of listbox is not the place to set the filter?
John
 

johnbirt

Registered User.
Local time
Today, 03:34
Joined
Oct 31, 2009
Messages
20
After further investigation it appears that the Multiselect property which was set to "None" for single selection is what is responsible for the orphan highlight. If Multiselect is set to "Extended" or "Simple" then there is no problem - any of the suggestions then work. Don't understand why there is a problem with single selection though.
 

Users who are viewing this thread

Top Bottom