Selecting listbox item in code - Value property??

M

marshallartsoz

Hi, using Access2007, and want to select an item in a listbox in my VBA code.
I'm doing it like this:

Listbox.Selected(n) = True

This works, but only up to a point. The item certainly gets selected
visually in the listbox on the form, but believe it or not, the 'Value'
property of the listbox DOES NOT GET SET! I couldn't believe this, but I
have been through it several times in the debugger, and this is definitely
what is happening.

If the item is selected by clicking on it, the Value property is set as
expected. But setting the Select(n) property to True in code leaves the
Value property of the listbox unchanged - it remains at whatever it was
before. I have a subform which is linked to the main form using the value of
this listbox, so selecting the item programmatically means my subform does
not update correctly. My listbox RowSource is a query, if that makes any
difference.

Is there a way around this? I thought of setting the listbox Value property
myself, but it is readonly, so I cannot set it in code. Am I missing
something? This looks awfully like an Access/VBA bug to me.
 
A

Allen Browne

Value does not apply to a multi-selected list box.

Iterate through the ItemsSelected collection to see which items are
selected.
 
M

marshallartsoz

OK, I have solved this myself after doing a bit more searching. Yes I do
have to set the Value property myself, but it has to be done indirectly, by
just setting the listbox to the selected value. So it's a 2-step process,
like this:

Listbox.Selected(n) = True
Listbox = Listbox.ItemData(n)

And this now works fine. I am still totally bemused that I have to do this.
If I select an item, surely that's what the listbox's Value should be (as
long as it's single-select of course)? I can't think of a reason why it
shouldn't just happen, but obviously someone could!
 
Joined
Mar 22, 2012
Messages
15
Reaction score
0
OK, I have solved this myself after doing a bit more searching. Yes I do
have to set the Value property myself, but it has to be done indirectly, by
just setting the listbox to the selected value. So it's a 2-step process,
like this:

Listbox.Selected(n) = True
Listbox = Listbox.ItemData(n)

And this now works fine. I am still totally bemused that I have to do this.
If I select an item, surely that's what the listbox's Value should be (as
long as it's single-select of course)? I can't think of a reason why it
shouldn't just happen, but obviously someone could!


"marshallartsoz" wrote:

> Hi, using Access2007, and want to select an item in a listbox in my VBA code.
> I'm doing it like this:
>
> Listbox.Selected(n) = True
>
> This works, but only up to a point. The item certainly gets selected
> visually in the listbox on the form, but believe it or not, the 'Value'
> property of the listbox DOES NOT GET SET! I couldn't believe this, but I
> have been through it several times in the debugger, and this is definitely
> what is happening.
>
> If the item is selected by clicking on it, the Value property is set as
> expected. But setting the Select(n) property to True in code leaves the
> Value property of the listbox unchanged - it remains at whatever it was
> before. I have a subform which is linked to the main form using the value of
> this listbox, so selecting the item programmatically means my subform does
> not update correctly. My listbox RowSource is a query, if that makes any
> difference.
>
> Is there a way around this? I thought of setting the listbox Value property
> myself, but it is readonly, so I cannot set it in code. Am I missing
> something? This looks awfully like an Access/VBA bug to me.

+1. 4 years on: that qualifies this as a classic, I think.
 
Joined
Sep 5, 2007
Messages
2
Reaction score
1
OK, I have solved this myself after doing a bit more searching. Yes I do
have to set the Value property myself, but it has to be done indirectly, by
just setting the listbox to the selected value. So it's a 2-step process,
like this:

Listbox.Selected(n) = True
Listbox = Listbox.ItemData(n)

And this now works fine. I am still totally bemused that I have to do this.
If I select an item, surely that's what the listbox's Value should be (as
long as it's single-select of course)? I can't think of a reason why it
shouldn't just happen, but obviously someone could!
Hello. It is now December 2020. Your elegant solution has solved a problem which has been bugging me for months. Thank you a million times and a millikon Qudos points to you.
 

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