Scrolling to show selected item in list box

grahamj

Board Regular
Joined
Feb 18, 2003
Messages
101
I have an unbound multiselect list box.
I redisplay records from a table, and have a little routine to highlight the items previously selected in the list box.
However, there are twenty items in my list and I only have room to show four at a time. So the first selected row is often not visible.
What VBA command do I use to scroll the list box to show the first selected row, please?
 
Ahh - I only tested it using a button click not an 'on current' event. Even if I call the button click event as part of the forms 'on current' event my code does exactly what you described. Adding repaint commands doesn't fix it either. I will have to look into this some more....

Andrew
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi

Well I've looked into this and I'm stumped (I don't often admit defeat).

I can get the code to work ok (where it scrolls the list to the first selected record) by either clicking a button or by using an 'on enter' event for another field. No matter what I try I cannot get the focus on to the listbox control during the 'on current' event. I believe this is a limitation in Access (I'm happy to be corrected if I am wrong) because all of the documentation I have read on the 'on current' event indicates no controls/objects can get the focus until after the 'on current' event has finished running. So I don't believe any workarounds, like the sendkeys solution you proposed, will work either. I tried setting the focus to a hidden control using the 'on current' event (and using an event on that control) but I could not get that to work either.....

How about a 'next record' button instead of using the navigation buttons?

Andrew
 
Upvote 0
Bewdy! Thanks, Andrew.

The Next Record button does it all.
I just copied your code into the next and previous buttons and it does exactly what I want.

I used the On Current event because I also jump to the form from a list-and-select form.
Interestingly, when I do that with a filter to a specific record and with your code only in the On Current event, it also works perfectly.

Thanks very much.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top