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

Setting the listbox default

Status
Not open for further replies.

ETID

Programmer
Jul 6, 2001
1,867
US
Hi,

I have a list box on an Excel form that calls it's list from a row source in a worksheet.

Can I set the default item selected in the box based on conditions ? .....

something like:

on form open ...

if condition_1 = true then listboxitem.selected = string_1 else listboxitem.selected = string_2




 
Hi ETID,

You cannot do it directly by the string contents because more than one item in the list could have the same text, but you can do it by number in the list:

Code:
Private Sub UserForm_Initialize()

If Condition_1 = True Then
    ListBox1.Selected(1) = True
Else
    ListBox1.Selected(2) = True
End If

End Sub

Enjoy,
Tony
 
I spent life here until I got the answer
Here it is

Me!List_Result.Selected(0) = True


TIA
 
Thanks tony,


But the list items in this case are static...and I will have a set of 64 possible conditions.

These conditions are defined by the list box itself so they cannot vary...that is to say under one condition, the user will select from the list to populate a record.

then under another condition if the user calls on the record
via the same form...i want the list box to reflect what is in the record.

the code is in place to manage this scenario all except for the syntax to reflect the exsisting record (if any) in the list box ...so the user can either accept the record as is or change it.

 
Let me add
I use List_Result.SetFocus at previous event
and then on List_Result_GOTFocus() event goes all above aa
Private Sub List_Result_GotFocus()
Me!List_Result.Selected(0) = True
End Sub
 
Your using the me ref. in Excel?....
 
No, Access !!!
But you coul figure out and use
List_Result.Selected(0) = True

But after you explained whole story - I give up, sorry

TIA
 
Ok...I cheated a bit, but I found a work around....
Thanks for all the suggestions!

Seeing as the list source (from a worksheet) will never change....I added another column numbered 0 to number of items in the list.


then the code is as follows........


frmReviewActivity.ListBoxCounty.ListIndex = Application.WorksheetFunction.VLookup(Sheets("records_table").Cells(Sheets("row_mirror_ledger").Cells(ActiveCell.Row, ActiveCell.Column), 5), Sheets("county").Range("b2:e68"), 4, False)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top