Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

highlight value in a list box 5

Status
Not open for further replies.

johnaregan

Programmer
Mar 13, 2001
87
I have a listbox populated with values extracted from a table. At the moment the highlight in the list box goes to the first record available.
I want to be able to get the highlight to go to a record that I specify in code base on another event.
This is because entries are selected from about 20 in the list box and having this feature would mean the user does not have to search thru' all of the list box in order to get to the value.

Thanks in advance
John
 
in order to select the value in a list you have to know what number in the list the value appears. For instance if you wanted the 4th item in the list you would use
Code:
MyList.Selected(3) = True
You will notice that the number following Selected is a 3 even though you wanted the 4th item. This is becuase Access starts counting at 0 (zero). If you want to retrieve the value of the listbox for use in another area or use as a variant you will need to move the focus after you have selected your choice. If you do not it will simply use the last known value (the 1st value in the list in your example.) I hope this helps. I know it gave me headaches for a while. Until I got it figured out.


Good Luck
Scoti ::)
 
Scoty

In reference to your answer how do you move the focus to a listbox row that is selected? Without using Sendkeys "{DOWN 4}" ? Robin Roslansky
Jakarta, Indonesia
 
Robin,
The trick is to use the index number. In my example
Code:
MyList.Select(3) = True
This will highlight the 4th item in the list box. after that it is just a matter of programatically moving the focus to another object and back...for instance say I had a command button on my form. I could (after using the above code)
Code:
DoCmd.GoToControl "MyCmdButton"
DoCmd.GoToControl "MyList"
This will move the focus so fast no one will notice and the value of the list box will become the hilighted selection.

I hope this helps
Scotty ::)
 
I like to do a Sendkeys "{DOWN}" after setting the .selected property. That way it makes the nice black bar highlight, and fires the AfterUpdate event, which just changing the focus never did for me.

Oddly, it doesn't move the selected listitem down by one. For me. The last time I used it.

Ron

PS(I know this is a year old post, but I was searching for "listbox highlight" and found it, so maybe someone else will find it the same way and can use the hint.)

 
Ron, just do it the way that Scotty suggusted, then after you set the focus have

call mylist_AfterUpdate()

I can't test it out right now, but that should work.

HTH!

-Brad
 
Brad -- I tried it before I submitted the post, and it just isn't the same. I found that the setting .select(index) just put the dotted line around the selection, and that the selection was "invisible" to the _AfterUpdate event

Unless I did the Sendkeys

Ron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top