ListBox ListIndex behavior

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • juing
    New Member
    • Apr 2009
    • 10

    ListBox ListIndex behavior

    Hi All,

    I am very new in the VBA coding and I've noticed this (to me strange) behavior:
    Assume this code for "Value Items" type of single select two column ListBox:

    1)
    If List98.ListInde x > -1 Then
    List98.Selected (List98.ListInd ex) = False
    End If
    2)
    List98.RowSourc e = ""
    3)
    Set rst = CurrentDb.OpenR ecordset( "any query" )
    rst.MoveFirst
    4)
    Do Until rst.EOF
    List98.AddItem """" & rst![field1] & """" & ";" & """" & rst![field1] & """"
    rst.MoveNext
    Loop
    5)
    rst.Close

    Let's say ListIndex was 2 before calling this code (third item is selected).
    After point 1) ListIndex = -1 (no selection).

    The "issue" is: When the third item is added in point 4) into the listbox the ListIndex property automatically sets itself back to 2 (the original value). Until the third item is added it remains -1. Adding next items doesn't change the ListIndex anymore so the final value is 2.

    What am I doing wrong?
    Thanks a lot.
    juing
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by juing
    Hi All,

    I am very new in the VBA coding and I've noticed this (to me strange) behavior:
    Assume this code for "Value Items" type of single select two column ListBox:

    1)
    If List98.ListInde x > -1 Then
    List98.Selected (List98.ListInd ex) = False
    End If
    2)
    List98.RowSourc e = ""
    3)
    Set rst = CurrentDb.OpenR ecordset( "any query" )
    rst.MoveFirst
    4)
    Do Until rst.EOF
    List98.AddItem """" & rst![field1] & """" & ";" & """" & rst![field1] & """"
    rst.MoveNext
    Loop
    5)
    rst.Close

    Let's say ListIndex was 2 before calling this code (third item is selected).
    After point 1) ListIndex = -1 (no selection).

    The "issue" is: When the third item is added in point 4) into the listbox the ListIndex property automatically sets itself back to 2 (the original value). Until the third item is added it remains -1. Adding next items doesn't change the ListIndex anymore so the final value is 2.

    What am I doing wrong?
    Thanks a lot.
    juing
    ho this can hapen
    I don't see how this can happen, but as a newly added last code line, try Requerying the List Box, namely:
    Code:
    Me![List98].Requery

    Comment

    • juing
      New Member
      • Apr 2009
      • 10

      #3
      I believe using "Requery()" method on ListBox with RowSourceType = "Value list" doesn't have any effect.

      Anyway, I finally got it working ...
      The problem is that it is not possible to use "Selected() " method on single selection ListBox.
      Although this line
      Code:
      List98.Selected(List98.ListIndex) = False
      changes the ListIndex to -1 but the Value property remains set to the last selected item (it's bound).
      This causes that when I put item with the same value as stored in the "Value" property the ListBox automatically sets its ListIndex to index of that value.

      To avoid this beahavior it is necessary to unselect single selection ListBox using code:
      Code:
       List98.Value = Null
      Thanks.
      juing

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by juing
        I believe using "Requery()" method on ListBox with RowSourceType = "Value list" doesn't have any effect.

        Anyway, I finally got it working ...
        The problem is that it is not possible to use "Selected() " method on single selection ListBox.
        Although this line
        Code:
        List98.Selected(List98.ListIndex) = False
        changes the ListIndex to -1 but the Value property remains set to the last selected item (it's bound).
        This causes that when I put item with the same value as stored in the "Value" property the ListBox automatically sets its ListIndex to index of that value.

        To avoid this beahavior it is necessary to unselect single selection ListBox using code:
        Code:
         List98.Value = Null
        Thanks.
        juing
        Glad you figured it out.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Originally posted by juing
          To avoid this behavior it is necessary to unselect single selection ListBox using code:
          Code:
           List98.Value = Null
          Thanks for posting Juing. That area (with MultiSelects etc) can be quite complicated. The interface is far from intuitive.

          Comment

          Working...