Selecting listbox item in code - Value property??

  • Thread starter Thread starter marshallartsoz
  • Start date Start date
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.
 
Value does not apply to a multi-selected list box.

Iterate through the ItemsSelected collection to see which items are
selected.
 
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!
 
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.
 
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.
 
Back
Top