select in listbox

K

Ken Snell MVP

My suggestion works on a listbox regardless of the setting of the Multi
Select property.
 
S

Stuart McCall

Ken Snell MVP said:
My suggestion works on a listbox regardless of the setting of the Multi
Select property.
<snip>

So it does. Well I'll be hornswoggled!

I learnt something today. Thanks, and apologies for stepping on your toes.
 
K

Ken Snell MVP

Point taken, Dale. Thanks.
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


Dale_Fye via AccessMonster.com said:
Yes,Ken, it will select the item, but if you do the following on a Multi-
Select = None listbox

Me.NameOfListBox.Selected(0) = True
msgbox me.NameOfListBox.Value

it will generate an error #94, Invalid use of Null
My suggestion works on a listbox regardless of the setting of the Multi
Select property.
Me.NameOfListBox.Selected(0) = True
[quoted text clipped - 6 lines]
Me.NameOfListBox.Value = Me.NameOfListBox.ItemData(0)

--
HTH

Dale Fye

Message posted via AccessMonster.com
 
M

Mac

Can you explain why this happens? I've been trying to select the first item
in a listbox (multiselect = none) when users cancel a new listbox entry
routine (using Me.Undo). Although the first item appears to be selected,
other fields which depend on the listbox aren't being updated when I use the
Selected(x) approach:

The statement:
Me.lstProblem.Selected(0) = True
MsgBox Me.lstProblem
generates a null, whereas the statement:
Me.lstProblem.Value = Me.lstProblem.ItemData(0)
MsgBox Me.lstProblem
displays the first entry on my listbox. I'm happy the latter statement
works, but am curious what I'm missing with the former. Thanks....




Ken Snell MVP said:
Point taken, Dale. Thanks.
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


Dale_Fye via AccessMonster.com said:
Yes,Ken, it will select the item, but if you do the following on a Multi-
Select = None listbox

Me.NameOfListBox.Selected(0) = True
msgbox me.NameOfListBox.Value

it will generate an error #94, Invalid use of Null
My suggestion works on a listbox regardless of the setting of the Multi
Select property.

Me.NameOfListBox.Selected(0) = True

[quoted text clipped - 6 lines]

Me.NameOfListBox.Value = Me.NameOfListBox.ItemData(0)

--
HTH

Dale Fye

Message posted via AccessMonster.com
 
S

Stuart McCall

Mac said:
Can you explain why this happens? I've been trying to select the first
item
in a listbox (multiselect = none) when users cancel a new listbox entry
routine (using Me.Undo). Although the first item appears to be selected,
other fields which depend on the listbox aren't being updated when I use
the
Selected(x) approach:

The statement:
Me.lstProblem.Selected(0) = True
MsgBox Me.lstProblem
generates a null, whereas the statement:
Me.lstProblem.Value = Me.lstProblem.ItemData(0)
MsgBox Me.lstProblem
displays the first entry on my listbox. I'm happy the latter statement
works, but am curious what I'm missing with the former. Thanks....




Ken Snell MVP said:
Point taken, Dale. Thanks.
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


Dale_Fye via AccessMonster.com said:
Yes,Ken, it will select the item, but if you do the following on a
Multi-
Select = None listbox

Me.NameOfListBox.Selected(0) = True
msgbox me.NameOfListBox.Value

it will generate an error #94, Invalid use of Null

Ken Snell MVP wrote:
My suggestion works on a listbox regardless of the setting of the Multi
Select property.

Me.NameOfListBox.Selected(0) = True

[quoted text clipped - 6 lines]

Me.NameOfListBox.Value = Me.NameOfListBox.ItemData(0)

--
HTH

Dale Fye

Message posted via AccessMonster.com

The Selected collection is available only for multi-select listboxes (or at
least that's how MS intended it). The ItemData(0) method is the way to go
with single-select boxes.

So you're not missing anything.

Apart from one little niggle (there's always something). If you set the
listbox's Column Heads property to Yes, item 0 contains the headers, so in
order to select the first data item you need to use an index of 1.
 
K

Ken Snell [MVP]

Stuart McCall said:
The Selected collection is available only for multi-select listboxes (or
at least that's how MS intended it). The ItemData(0) method is the way to
go with single-select boxes.

So you're not missing anything.

Apart from one little niggle (there's always something). If you set the
listbox's Column Heads property to Yes, item 0 contains the headers, so in
order to select the first data item you need to use an index of 1.

This can be done automatically using this code step:

Me.NameOfListBox.Value =
Me.NameOfListBox.ItemData(Abs(Me.NameOfListBox.ColumnHeads))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top