Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511

    Unanswered: How do get Selected List Item to Appear at Top

    In Access 2000, I have a list box of several thousand numbered items with numbers ranging from 100 to 9999. I want to use a combo box to quickly move to an area of the list, so I have assigned values in my combo box: 1000;2000;3000;4000; etc. In the AfterUpdate event of my combo box I have a For loop that checks each number in the 1st column of the list box until the number is >= to the number selected in the combo box. The search works quickly, and the item I have found and selected appears toward the bottom of the listbox.

    This is my question: How can I get selected list item to appear at the top of the list box?

    I have tried selecting the 20th item beyond by changing its SELECTED property to TRUE then FALSE, and then selecting the found item; this causes the combo box to take full control of my form so that nothing works but the combo box.

    Here's the code including the two commented lines that cause the combo box to take full control:

    j = myList.ListCount
    TargetVal = cboTargets.Text

    For i = 1 To j
    If myList.ItemData(i) >= TargetVal Then
    'myList.Selected(i + 20) = True
    'myList.Selected(i + 20) = False
    myList.Selected(i) = True
    Exit For
    End If
    Next i

    Thank you for advice on this working but imperfect solution.

  2. #2
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511

    Talking

    OK...I'll answer this one myself. Using the UPDATE event of a combo box that has a range of numbers assigned: 1000,2000, etc., and not limited to the combo box list, I can use the value in the combo box, either typed in or picked from a drop down list, to search a numeric field in one of the columns of a list box and cause the item greater or equal to the combo box value to appear at the top of the list box. Selecting a list item down the list, then selecting the target item brings it to the top of the list.

    My goal was to bring the found target to the top of the list box. Because my combo box is not limited to the list, a target number can be entered in the box to locate a list box target or position the list at the next highest number if the exact number was not in the list box. With 2,500 items in the list, searching is instaneous.

    The "Me!myList = Null" statement below causes the found target to be unselected. The 1st line of code below exits the procedure for the update caused by clearing the combo box for next use (4th statement below).

    The revised code:

    If cboTargets.Text = Null Or cboTargets.Text = "" Then Exit Sub
    j = myList.ListCount
    TargetVal = cboTargets.Text
    Me!cboTargets = Null

    For i = 1 To j
    If Me!myList.ItemData(i) >= TargetVal Then
    Me!myList.Selected(i + 20) = True
    Me!myList.Selected(i + 20) = False
    Me!myList.Selected(i) = True
    Me!myList = Null
    Exit For
    End If
    Next i

  3. #3
    Join Date
    Nov 2002
    Posts
    272
    You'll get an error if item (i+20) doesn't exist.
    You could select/deselect the last item in the list instead.

  4. #4
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511
    Originally posted by ivon
    You'll get an error if item (i+20) doesn't exist.
    You could select/deselect the last item in the list instead.
    Thanks. Instead of i+20, I'll use j-1.

    Jerry

Posting Permissions

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