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!

Programmatically selecting a given value for the list 1

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
0
0
US
The following line of code dones what I need it to do:
Code:
Me.cboCustomer.SelectedItem().Text = rs.Fields.Item("CustomerID").Value()

but not exactly the way I'd like it to.

This line is in an edit form called from another form. So when I populate the fields in the form for the user to change, the dropdownlists are populated too.

Then what this line does is not what you'd think it does: select the item in the list. It just adds that item from the database to the top of the list.

That's not really so bad, since my major concern is that the user can only choose a value from the list. But it's not very elegant since that value is now twice in the list -- at the top and somewhere in the middle of the list.

Is there a way to select a given item from the list instead of adding it to the top?

dpdoug

 
set the property selected index
Me.cboCustomer.SelectedIndex = ....

You have to loop through the items of the dropdown and compare the values with the value you got from the database;
and then set the above property for the value that matches
 
If items are already added to the dropdown list then the
following code works fine for me . It will select the item in list if it is already added.

Try
dropTitle.Items.FindByValue(rs.Fields.Item("CustomerID").Value).Selected = True

Catch
End Try

Or You can find the item by text using

dropTitle.Items.FindByText(rs.Fields.Item("CustomerID").Value).Selected = True

 
mbhavani's got it.

What your code is doing is overwriting the SelectedItem's text, when you are, of course, wanting to set the SelectedIndex to the item containing the text.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top