Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Location
    Wyoming
    Posts
    48

    Unanswered: Return index of listbox selection

    I'm sure that this is very simple; however, I just can't get it.

    What I need is the index of a listbox selection to be returned to a variable.


    I think this can be done in one or two lines of code but need your help.

    Thanks
    Cheers,
    Ken

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    If we assume that the name of your List Box is List1 then we would have our code as such:

    Code:
      Dim MyIndexNum As Long
      MyIndexNum = Me.List1.ListIndex + 1
      'The variable MyIndexNum will contain
      'the Index number of the item selected
      'in List1.  To Confirm this....
      MsgBox "Item " & MyIndexNum & " was selected."
    Notice the + 1 located after the Me.List1.ListIndex. MS Access always starts the first item index in a List Box (or Combo Box) with zero (0). Display wise...this is confusing therefore we increment the supplied index number by one (1) so that is appears as though, if we select the first item in the list, the number sent back is number 1 (instead of zero). You need to remember this. If you were working with the supplied List Index number progmaticly then you need to always remember that the first item in the list actually contains a List Index number of zero (0). For example, below, the + 1 is removed to show the true Index number supplied:

    Code:
      Dim MyIndexNum As Long
      'The true Index number.
      MyIndexNum = Me.List1.ListIndex
      'The variable MyIndexNum will contain
      'the Index number of the item selected
      'in List1.  To Confirm this....
      MsgBox "Item " & MyIndexNum & " was selected."
    I think you get the picture now....
    Hope this helps

  3. #3
    Join Date
    Nov 2002
    Location
    Wyoming
    Posts
    48
    Hi CL:

    Thank you for a very good explanation of the list box index. Your code was exactly what I needed.

    There are still some issues that I need to iron out, but you have put me well on my way.
    Cheers,
    Ken

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •