Ato select item from listbox

B

bryan

I am using a userform and populating a listbox from a database.
If I have only one item, how can I auto select that item.

With combo boxes I can use:
If ComboBox1.ListCount = 0 Or ComboBox1.ListCount = 1 Then
Call CommandButton1_Click
End If

Result will be that if there is 1 item it is autoselected.

Can this be done with a list box as well?

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

Set the ListIndex attribute of the Listbox to the item that you want to be
selected.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

bryan

Here is what I did and it works, assume this is what you meant:
If ListBox1.ListCount = 0 Or ListBox1.ListCount = 1 Then
ListBox1.Selected(i) = True
Call CommandButton1_Click
End If

If not, let me know what you meant.

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

Probably should be

If ListBox1.ListCount = 1 then
'Get the first item in the List
ListBox1.List(0, 0)
'Do something with it

End IF
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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