title: Access VBA listbox method .Selected versus .ItemData
body: <p>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 <code>.Selected</code> 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.</p>
  
  <p>It turns out that <code>.Selected</code> 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.</p>
  
  <p>I realized I had to follow up the <code>.Selected</code> method with a <code>.ItemData</code> method to actually have the listbox output value to equal the row that I selected in the <code>.Selected</code> method.</p>
  
  <p>Here is the code I used to copy selections on a Vendor PN Lookup Form to a Main Part Search Form:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>As above, why do I need to use: </p>
  
  <pre><code>Forms("frmPartSearch").lstSearchResults.Selected(i - 1) = True
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>Forms("frmPartSearch").txtHiddenPN.Value 
    = Forms("frmPartSearch").lstSearchResults.ItemData(i - 1)
  </code></pre>
  
  <p>To get <code>txtHiddenPN</code> to receive the correct row value instead of simply setting <code>txtHiddenPN</code>'s control source to <code>=[lstSearchResults]</code> and have it update to the row value after <code>.Selected</code>?</p>
  
  <p>What use is <code>.Selected</code> if it doesn't change the output value of the listbox (other than handily, but futilely highlighting a row)? And similarly, why doesn't <code>.ItemData</code> 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?</p>
  
  <p>I'm new to VBA and coding in general, so maybe this is a common occurrence in the field, but it just seems obtuse.</p>