PDA

View Full Version : code to have listbox deselect and bring to top



jknuep
08-07-2007, 03:30 PM
I am trying to have a listbox programmatically unselect an item and then scroll all the way to the top.
I tried
Private Sub bringToTop()
Me.lstParts.Selected(1) = True
Me.lstParts.Selected(1) = False
End Sub
But it only works when I have it triggered from a cmd button. It does some funny stuff when I ?call? it as part of an onClick event from another listbox; like being unable to select an item at all in the ?lstParts? listbox. Any ideas as to why this happens or a better way to go about this???

JimmyTheHand
08-08-2007, 10:50 PM
Try this:

Private Sub bringToTop()
Me.lstParts.TopIndex = 0
End Sub

jknuep
08-09-2007, 09:03 AM
Access 2003 doesn't recognize ".topIndex"
I did try

Me!lstParts.SetFocus
Me!lstParts.ListIndex = 0

Which works fine when called from a cmd button, but not when calling it from an onclick event from another listbox. I get this error

“Run-time error ‘7777’
You’ve used the ListIndex property incorrectly.”

JimmyTheHand
08-09-2007, 10:39 PM
Sorry, I tried topIndex in Excel only, and was lazy to check if Access uses it, too. Apparently, it doesn't.

Anyway, I tried it your way and found it working, even when calling it from an onclick event of another listbox. I attached a test .mdb file for you to check. I added another line of code at the end, because you wanted all list items unselected, or so I thought. This codeline translates to
Me!lstParts.ListIndex = -1 in your case.

Does my file work for you?


Jimmy