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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a row programmatically in a multiselect listbox

Status
Not open for further replies.

toddtharp

Programmer
Nov 23, 2004
8
0
0
US
I have recently changed a list box to Extended multi-select and other bits of code which reference it are now breaking. I have no problem getting at the data values in the list box, but haven't been able to figure out how to SET the value of the list box programmatically.

I have form with two lists and when one is selected (no multi-select), the after update sets the value of the other list (with multi) to a value in one of the columns in the first list box.

Before I switched the list box to multi, simply setting the value of that box would highlight the related row - I then call that list box's after update subroutine and the subform below it filters to the details of the selected item.

Now, since the multiselect list box is forced null by access, I cannot get the list box to highlight the row with the bound value, so when I call the after_update, the code breaks because I cannot get it to hold a specific value.

I realize I can iterate through the collection, but how do I set the value of the list box so the row I want will be highlighted and I can then pass on the filter value to my subform?
 
Okay, update - I can select a row in the list box by setting the listindex property to a number.

Is there a quick way to get at an listindex number if I'm looking for a particular string in one of the columns?


Thanks
 
Okay, further problems - I'm now cycling through the rows in the list box and grabbing the listindex number of the record I want.

However, when I attempt to set the listindex property of the listbox to that number (and thus selecting it), I get the following error: '7777' - You've used the listindex property incorrectly.

The strange thing about this is that I was able to set it in the Immediate window and see the highlighted item change while in runtime, but it breaks in the code.

I've tried to reference it using me.listbox.listindex, FOrms!MainForm.subform.form.listbox.listindex and using a defined access.listbox variable - lst.listindex.

Has anyone else had this problem?
 
Further update:

I've discovered that the list box must be the focus to be able to set the listindex property and have the highlighted row change to that index.



 
Some listbox techniques - typed not tested;-)

[tt]dim lst as listbox
set lst=me!lstNameOfList ' or complete reference
lst.selected(N)=true[/tt]

Should select the rownumber inputtet thru N (note - zero based, unless you've included header).

Select all items in a mulitiselect list:

[tt]dim lst as listbox
dim lngCount as long
set lst=me!lstNameOfList ' or complete reference
for lngCount=0 to lst.listcount-1
lst.selected(lngCount)=true
next lngCount[/tt]

Select one row, based on some value in the first column of the listbox

[tt]dim lst as listbox
dim lngCount as long
dim somevariable as <somedatatype>
somevariable = <your search value>
set lst=me!lstNameOfList ' or complete reference
lst.rowsource=lst.rowsource
for lngCount=0 to lst.listcount-1
if lst.colum(0,lngCount) = somvariable then
lst.selected(lngCount)=true
exit for
end if
next lngCount[/tt]

Note - for numerics, you might want to use the Val function, and for text, perhaps trim:

[tt]if val(lst.colum(0,lngCount)) = somvariable
if trim$(lst.colum(0,lngCount)) = somvariable[/tt]

Included is one of the techniques one can use to deselect all items in a multiselect listbox (reset the rowsource)

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top