'Access VBA listbox method .Selected versus .ItemData
While working on an application I had trouble getting a selected item in my listbox to actually output the correct row value. I was using the .Selected method to choose which row needed to be selected in order to proceed to the next step, which would pull the bound column's value from the listbox for further processing, without the need of the user to interact with the listbox.
It turns out that .Selected doesn't work the same way as clicking on a listbox row, as its output value remains the last "manually" selected row's bound column value.
I realized I had to follow up the .Selected method with a .ItemData method to actually have the listbox output value to equal the row that I selected in the .Selected method.
Here is the code I used to copy selections on a Vendor PN Lookup Form to a Main Part Search Form:
Private Sub cmdAddTofrmPartSearch_Click()
Dim i as Integer
... [truncated]
Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
... [truncated]
Forms("frmPartSearch").txtHiddenPN.Value
= Forms("frmPartSearch").lstSearchResults.ItemData(i - 1)
...[truncated]
End Sub
As above, why do I need to use:
Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
AND
Forms("frmPartSearch").txtHiddenPN.Value
= Forms("frmPartSearch").lstSearchResults.ItemData(i - 1)
To get txtHiddenPN to receive the correct row value instead of simply setting txtHiddenPN's control source to =[lstSearchResults] and have it update to the row value after .Selected?
What use is .Selected if it doesn't change the output value of the listbox (other than handily, but futilely highlighting a row)? And similarly, why doesn't .ItemData also highlight the row whose bound value is being outputted? Is this bad design, or is there another method that achieves both results simultaneously? Is it best practice to automatically include both methods sequentially? Are there situations where only one method needs to be called?
I'm new to VBA and coding in general, so maybe this is a common occurrence in the field, but it just seems obtuse.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
