Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBoxes, ItemsSelected, and AfterUpdate events

Status
Not open for further replies.

eksortso

Programmer
Jun 30, 2003
43
US
If I have an Extended MultiSelect ListBox, how can I update other form fields when the selected values in the listbox are changed? It's not a simple as it looks, because I'm looking for a technique that will work with both a mouse AND the keyboard.

Here's a simple setup to illustrate the problem. Say you have a form with a listbox called [tt]Listbox0[/tt] and a TextBox called [tt]TextBox2[/tt]. The listbox is set to MultiSelect Extended, so that when you press the up- and down-arrow keys, the listbox selection changes. Also, say the listbox uses a Value List row source of "alpha;beta;gamma;delta;epsilon". (It doesn't matter what the row source is, but we'll use this list as an example.)

The code underlying this form is as follows, so that when the listbox's AfterUpdate event fires, the textbox lists the values of the selected items.
Code:
Private Sub List0_AfterUpdate()
    Call ListSelectedItems
End Sub

Private Sub ListSelectedItems()
    Dim v As Variant
    Dim s As String
    For Each v In Me.List0.ItemsSelected
        If s <> "" Then
            s = s & ", "
        End If
        s = s & Me.List0.ItemData(CLng(v))
    Next v
    Me.Text2.Value = s
End Sub

When I click listbox items with the mouse, then the AfterUpdate event fires, and the textbox displays the correct values. I click alpha, the textbox shows "alpha". I drag across beta and gamma, the textbox shows "beta, gamma" as expected.

When I use the keyboard, though, ItemsSelected doesn't get updated until after AfterUpdate fires. Say I'm on alpha. Then I press the down-arrow. Now, the listbox has beta selected (remember that the listbox is using Extended MultiSelect), but the textbox still shows "alpha"! If I press down again, then gamma is selected, but "beta" appears in the textbox!

And when I hold down the Shift key and use the arrows, nothing happens at all. It turns out that the key events fire, but not AfterUpdate.

So, what can you suggest as a way around this problem? I'd like to know because in practice, my users would like to use the keyboard to get their reports in a timely manner. One thought I had was to call [tt]ListSelectedItems[/tt] when the listbox's KeyUp event fires, but that can be impractical when the processing is more involved than just listing the selected items.

 
Hallo,

This is a bit of a frustration with Access events, there never seems to be one which does exactly what you want (like form_current not firing when you delete a record)

You seem to have found a solution when you say
[red]It turns out that the key events fire[/red]
I would call ListSelectedItems on one of those events too.
You could also do something on Mouse events.

If processing is too lengthy, try checking to see if anything has changed at the start of ListSelectedItems.
Could you assume that if the number of selected items hasn't changed then you don't need to update the list.

Static slngItemsSelectedCount as Long
If Me.List0.ItemsSelected.Count=slngItemsSelectedCount then exit sub
slngItemsSelectedCount=Me.List0.ItemsSelected.Count
slngItemsSelectedCount will be 0 on first call, then will remember what it was last time (because it's Static)

Also, I would turn ListSelectedItems into a function which returns no parameter, then you can make the AfterUpdate property:
=ListSelectedItems()
and you don't need the afterupdate event procedure.

- Frink
 
Thanks, Frink, for the reply. I've wished that there was a comprehensive list explaining when events fire on Access forms. The help files are occasionally useful, but whatever information I can find is always piecemeal.

Frink said:
If processing is too lengthy, try checking to see if anything has changed at the start of ListSelectedItems.

I ended up doing somthing like this within the [tt]List0_KeyUp[/tt] event procedure code. AFAIK, I only need to call [tt]ListSelectedItems[/tt] when KeyCode is one of these: vbKeyUp, vbKeyDown, vbKeyPageUp, and vbKeyPageDown.

BTW, because of these sorts of conditions, I rarely enter the names of VBA functions in the on-event properties. It's also easier to maintain an app with full-fledged event procedures, since all the processing logic resides in the form's code.

Frink said:
Could you assume that if the number of selected items hasn't changed then you don't need to update the list.

This wouldn't help if I only had one item selected to begin with, and I press the down-arrow without the Shift key. I could see how this would work if I selected multiple items with the keyboard, though.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top