Programmatically select item in List Box - Access 97

R

Ron Hinds

I want the first item in a list box to be the selected item when a form
opens. The idea being that when the form opens if a user wants the first
item (usually they do) then all they have to do is hit Enter and that's the
choice that is made. I've set the default property of a command button on
the form to True, and in the Form's Current event I've set the Selected
property of the first item in the list box to True like so:

lstCutoffTime.Selected(0) = True.

But in the code of the command button, lstCutoffTime.Value always returns
Null, even though the first item in the list box is selected, as evidenced
by highlighting as well as from the Debug window. If I click that same
(already selected) item with the mouse, then the value is what I expect it
to be. What gives here?
 
W

Wayne Morgan

Is the listbox set for multiselect (simple or extended) or for single select
(none)? To set these, go to the Other tab of the listbox's Properties and
set the MultiSelect property. If it is set for multiselect, then you need to
loop through the ItemsSelected collection to get the selected values, the
listbox won't have a single value. If it is a single select listbox, then
you can make a selection by setting the value of the listbox instead of
using Selected() as you are.

Simple Select:
lstCutOffTime = lstCutOffTime.Column(0,0)

Adjust the column index in the statement above to be the Bound Column - 1
(the value is zero based).

Multi Select example placing the selected items into a temporary table:
Dim varItem As Variant
For Each varItem In lstChoirs.ItemsSelected
With rst
.AddNew
![ChoirID] = lstChoirs.ItemData(varItem)
.Update
End With
Next varItem
 
V

Van T. Dinh

If this is a single-select ListBox, set the Value of the ListBox instead.
Something like (IIRC).

Me.ListBox.Value = Me.ListBox.Column(0,0)

(assuming that the bound Column is the first column. The first number is
the column index which is zero-based. The seocnd number is the row number
which is also zero-base.)

If the ListBox is multiple-select, the Value of the ListBox is always Null
regardless of how many rows are selected.
 
R

Ron Hinds

It's single select and that worked! Thanks guys!

Wayne Morgan said:
Is the listbox set for multiselect (simple or extended) or for single select
(none)? To set these, go to the Other tab of the listbox's Properties and
set the MultiSelect property. If it is set for multiselect, then you need to
loop through the ItemsSelected collection to get the selected values, the
listbox won't have a single value. If it is a single select listbox, then
you can make a selection by setting the value of the listbox instead of
using Selected() as you are.

Simple Select:
lstCutOffTime = lstCutOffTime.Column(0,0)

Adjust the column index in the statement above to be the Bound Column - 1
(the value is zero based).

Multi Select example placing the selected items into a temporary table:
Dim varItem As Variant
For Each varItem In lstChoirs.ItemsSelected
With rst
.AddNew
![ChoirID] = lstChoirs.ItemData(varItem)
.Update
End With
Next varItem

--
Wayne Morgan
MS Access MVP


Ron Hinds said:
I want the first item in a list box to be the selected item when a form
opens. The idea being that when the form opens if a user wants the first
item (usually they do) then all they have to do is hit Enter and that's
the
choice that is made. I've set the default property of a command button on
the form to True, and in the Form's Current event I've set the Selected
property of the first item in the list box to True like so:

lstCutoffTime.Selected(0) = True.

But in the code of the command button, lstCutoffTime.Value always returns
Null, even though the first item in the list box is selected, as evidenced
by highlighting as well as from the Debug window. If I click that same
(already selected) item with the mouse, then the value is what I expect it
to be. What gives here?
 
Top