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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically Scroll to Bottom of List Box 4

Status
Not open for further replies.

alexbel

Technical User
Jun 27, 2004
77
US
Hi,

I have a list box that displays values from a field in a table. I was wondering how I can make the list box scroll to the very bottom of the list automatically so the user can see the latest values that they have entered?

Thank you!
 
Have you tried this ?
Me!theListBox.ListIndex = Me!theListBox.ListCount - 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV, I just tried this on a sample form, pulling a list from a random table, and got this for an error message when used the code in a button_Click() event:

[BLUE]
Run-time error '7777':

You've used the ListIndex property incorrectly.
[/BLUE]

Any ideas?

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
PHV,

I get the following error message:

"You've used the ListIndex property incorrectly
 
Sorry kjv1611,

Didn't see your message. I am having the same problem!
 
Well, it seemed very interesting, and PHV's idea sounded like it made perfect sense, so I thought I'd try it in case I found a reason to use it at some point in the future.. oh well.. [WINK] - how long dya think it'll take PHV to figure this one out? [SMILE].. if he hasn't already..

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Yeah,

He probably already did. We'll see though. :)
 
Both following lines works for me (acXP, ac2003):
Me!Liste6.ListIndex = Me!Liste6.ListCount - 1
Me!Liste6.Selected(Me!Liste6.ListCount - 1) = True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Don't know why, the help files says the listindex is read/write, but I'm having problems with that too. One workaround for non multiselect listboxes could be trying:

[tt]Me!theListBox = Me!theListBox.column(0, Me!theListBox.ListCount - 1)[/tt]

Roy-Vidar
 
If you use KenReay's solution, make sure you add a field in your table that is the record source which gives the date added, and you can just set the default value in the table equal to Date() like this:

In table design view, make sure the format is set to date, then in the bottom left of the window, look for "Default Value" and enter the value like this:

=Date() - what this will do is to set the date equal to todays date when you update the record today. The good thing is, that it will not change each time you open the table, unlike Excel. If you use =Date() or =Now() in Excel (as a function/equation in a cell), it will change/update everytime the stinkin' workbook is opened. I love that with Access, it just takes the date when the record was added, and does not change when you open the database.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
PHV, where are you putting the code? I tried mine with a button, are you putting it in one of the properties for the control, or in some VBA code?

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Have you tried this VBA code (anywhere in the Form's module, provided the ListBox is populated) ?
Me!Liste6.Selected(Me!Liste6.ListCount - 1) = True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Well, using that variation of the code seems to work perfectly here on my machine..

alexbel?

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Thanks PH - just what I wanted. I would never have thought of your solution
Simon Rouse
 
Hello Gang!

I remember fighting this problem in a design quite some time ago. From my library I have two references quoted. One is for [blue]CommandBars/Combobox[/blue] an the other [blue]Forms/Combobox[/blue]:
Microsoft said:
[blue][purple]ListIndex Property (Microsoft Office Reference)[/purple]
Returns or sets the index number of the selected item in the list portion of the command bar combo box control. If nothing is selected in the list, this property returns zero. [purple]Read/write[/purple] Long.

Note This property fails when applied to controls other than list controls.

Remarks

Setting the ListIndex property causes the specified control to select the given item and execute the appropriate action in the application.[/blue]
Microsoft said:
[blue][purple]ListIndex Property (Access)[/purple]
Setting

The ListIndex property is an integer from 0 to the total number of items in a list box or combo box minus 1. Microsoft Access sets the ListIndex property value when an item is selected in a list box or list box portion of a combo box. The ListIndex property value of the first item in a list is 0, the value of the second item is 1, and so on.

This property is available only by using a macro or Visual Basic. You can read this property only in Form view and Datasheet view. This property is [purple]read-only[/purple] and isn't available in other views.[/blue]
As an alternate method I specify the [purple]Selected Property[/purple]:
Code:
[blue]Me!ListBoxName.Selected(n) = True
[purple]using PHVs reference gives[/purple]:
Me!ListBoxName.Selected(Me!ListBoxName.ListCount-1) = True[/blue]

Calvin.gif
See Ya! . . . . . .
 
Maybe I'm missing something, but don't those two "Microsoft" references conflict with each other -- one says 'read/write' and the other says 'read only' -- or is something missing from my comprehension?

The other thing I noticed was that the first MS reference says that the index returns '0' when nothing is selected -- but the second reference says that the value is in the range of 0..(items - 1), which implies that when the first item in the list is selected, it would also return '0' -- but that also reflects the state of having no items selected. Which is it -- or, again, am I missing something?

Any clarification would be appreciated. Thanks.
 
How are ya dameeti . . . . .

According to MS, [purple]ListIndex Property (Microsoft Office Reference)[/purple] is for [blue]List Controls[/blue] on [purple]CommandBars[/purple] (ToolBars), while [purple]ListIndex Property (Access)[/purple] is for [blue]List Controls[/blue] on [purple]forms[/purple] . . . . .

Calvin.gif
See Ya! . . . . . .
 
Listindex of combos and lists returns -1 when nothing is selected.

The second reference above, is part of the help file on the listindex property of Access combos/lists, the first is something else (applies to the CommandBarComboBox Object), so they don't conflict, they are separete, different object types.

Here's the full help text (Access combos/lists listindex):

"ListIndex Property
See Also Applies To Example Specifics
You can use the ListIndex property to determine which item is selected in a list box or combo box. Read/write Long.

expression.ListIndex

expression Required. An expression that returns one of the objects in the Applies To list.

Remarks
The ListIndex property is an integer from 0 to the total number of items in a list box or combo box minus 1. Microsoft Access sets the ListIndex property value when an item is selected in a list box or list box portion of a combo box. The ListIndex property value of the first item in a list is 0, the value of the second item is 1, and so on.

This property is available only by using a macro or Visual Basic. You can read this property only in Form view and Datasheet view. This property is read-only and isn't available in other views.

The ListIndex property value is also available by setting the BoundColumn property to 0 for a combo box or list box. If the BoundColumn property is set to 0, the underlying table field to which the combo box or list box is bound will contain the same value as the ListIndex property setting.

List boxes also have a MultiSelect property that allows the user to select multiple items from the control. When multiple selections are made in a list box, you can determine which items are selected by using the Selected property of the control. The Selected property is an array of values from 0 to the ListCount property value minus 1. For each item in the list box the Selected property will be True if the item is selected and False if it is not selected.

The ItemsSelected collection also provides a way to access data in the selected rows of a list box or combo box.

Example
To return the value of the ListIndex property, you can use the following:

Dim l As Long
l = Forms(formname).Controls(controlname).ListIndex
To set the ListIndex property value, you can use the following:

Forms(formname).Controls(controlname).ListIndex = index

Where formname and controlname are the names of the form and list box or combo box control, respectively, expressed as String values, and index is the index value of the item."

Find any inconsistency (hint I've bolded some)?

It seems sometimes one can set the listindex (though I've failed to do so), perhaps when the listbox has focus? or on some versions? dependent on the BoundColumn settings? Microsoft states it is Read/Write, Read Only and assignes a value... PHV's latest (repeated by TheAceMan1) is probably among the best ways of selecting an item.

Now - it's very easy to try, you know, just fire up a form with a listbox and a combo, don't select anything and run a routine with only:

[tt]msgbox "List " & me!lst.listindex & " Combo " & me!cbo.listindex[/tt]

then you'd found out... In addition some little experimentationg with the F1 key on the keyboard, and there wouldn't be any need to paste it here...

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top