Selecting an item from a listbox automatically

T

Tony C

How do I select a row from a listbox so it shows as selected. Microsoft
suggest using:

Forms(formname).Controls(controlname).ListIndex = index

but this will produce an error, as the property is read-only. Various other
combinations dont seem to work such as

myLstbx.value = index (index provided)

Unless we need to requery the listbox after to get the new value to show...

Tony
 
T

Tony C

Dennis, that raises the error:

2455: You entered an expression that has an invalid reference to the
property Selected.

My code is:
ctl.Selected(CInt(ctl.Tag)) = True

ctl resolves to a listbox and cint(ctl.tag) resolves to '1'.
ctl.Value = ctl.ItemData(CInt(ctl.Tag))

however that doesnt automatically fire the Listboxes Chnage event which
contains some code that needs to run to change related values in another
listbox, so I have to manually call the change event.

Any ideas why you code works and mine doesnt ?

Tony
The only way I can get it to work is using :
 
D

Dennis

What do you mean by the Listbox Change event ?
When my form loads, I set my listbox item 2 as being selected.
I have put code in my ListBox After Update event and I can get it to fire
using this code behind the click of a button event. I can' t see why yours
does not work and mine does.

ListBoxName.Selected(1) = True
Call ListBoxName_AfterUpdate
 
D

Dennis

It certainly is. If I type the name of my listbox in code and then press the
dot, selected is available from the list. I think this is where your problem
lies if selected is not available in the list when you type ctl and the dot
 
T

Tony C

Dennis, sorry I have been saying listbox and meant Combobox...(duuh)!!
Selected is not a property of a combobox.

The standard way from all the posts I have looked at seesm to be via the
listindex property as follows:

Mycombo.listindex = index

This doesnt seem to work in my scenario. i am wondering whether is has
something to do with it having a bound rowsource. Sorry for misleading you in
the first plave, your help is appreciated!
Tony
 
T

Tony C

The only code I could get to work is this:

ctl.Value = ctl.ItemData(CInt(ctl.Tag))

using itemdata property works fine, but I then need to call the Change event
manually as this doesnt fire automatically. I'll live with it, but curious to
know why lIstindex raises a 2455 or 7777 error.

tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top