views:

191

answers:

3

So I have a form that has a listbox that shows like a ledger. My question is how can I make it display the last records (or have the scroll bar default to the bottom instead of the top), instead of the first few as the default.

Now I don't mean reversing the order from bottom to top instead of top to bottom (though that would be a cool thing to learn how to do), just simply having the bottom of the list (in terms of the scroll bar) shown and the default, so that it is always showing the last 10 or so records (based on the size that I made the list box).

So i think this is simple, but then again, I obviously do not know?!?!

Thanks!

A: 

How did you set the listbox items? Are they from a database? If yes, then you need to update the SQL statement with an "order by columnName".

Gerardo Abdo
yes it is a sql statement that just selects the dataset that I want and that is working fine. But i am not reffering to an "order by" of left to right, etc....rather something that makes the listbox object always have the scroll bar start on the bottom rather than on the top
Justin
@Gerardo Abdo: What part of "Now I don't mean reversing the order from bottom to top" was unclear in the original question?
David-W-Fenton
A: 

You could add some code to the form load event so that it will do this:

YourListBox.SetFocus
YourListBox.ListIndex = YourListBox.ListCount - 1
YourListBox.Selected(YourListBox.ListCount - 1) = False

It basically selects the last item in the list box so it will scroll down to it, and then unselects it.

patmortech
so...this is the exact idea I was looking for. however this is resulting in an error: RunTime error 7777 : You have used the ListIndex property incorrectly.
Justin
Thanks! I do appreciate it; this is what I am after...once I can make it work!
Justin
I got that same error before I put the SetFocus line in there.
patmortech
while stepping through i noticed it got thrown to error on the second line of code....
Justin
I was saying that when I take out that first line (ListBox.SetFocus), I get the same error you are mentioning. After putting it in, the error does not come. So make sure that is in there properly.
patmortech
i know. i have it exactly how you have it above (with the exception of re-naming my listbox). so I have the .rowsource method for this listbox right above it in the load event? do you think this might be effecting it?? I would want this to take place after the .rowsource sql statement so I wouldn't think so??I know i must be overlooking something else....just trying to find it
Justin
I don't know why you're getting the error you're getting, but setting Selected to False does not cause the listbox to scroll. @Remou's code is what actually works, and you should accept that as the answer. And the listbox does *not* have to have the focus for his answer to work.
David-W-Fenton
You're right, setting selected to false doesn't scroll the list box. but setting the ListIndex to the last item DOES. I then added the last line so that the item does not remain selected (since he may not want the thing to be selected). And by the way, this does actually work.
patmortech
+2  A: 

In a suitable event, such as the current event:

 Me.ListX.Selected(Me.ListX.ListCount - 1) = True
Remou
perfect!! thanks as always Remou!
Justin