I have a form with amongst other things a combobox.
The form is used to enter new and update existing customer records (in an SQL database).
I am populating the combobox from a separate table (list ID and list text) in the database and when, on a new customer record, an entry is selected I am using the rowtag to return the list record ID to store in my database.
When an existing customer record is loaded, how do I make the combobox go to the existing recording list tem?
Thanks, Paul.
PS I don’t store the combobox listindex, just the list ID.
You just need to set ListIndex as you fill the combobox.
If you know the list ID and are populating the rowtag with it while filling the combobox you’re probably doing that in a loop of some sort. While in that loop if that list ID matches the one from the current customer then set the ListIndex to the row you’ve just added.
You might know the current ListIndex by either keeping track of it yourself with a 0 based loop variable, or using ListCount - 1 after the append.
You can also create an extends method to make this universal for combo boxes.
Put this in a module:
Sub SetComboboxIndex(extends cb As ComboBox, assigns s As String)
for i as integer = 0 to cb.ListCount-1
if cb.list(i) = s then
cb.ListIndex = i
exit
end
next
End Sub
Call it like: ComboBox1.SetComboboxIndex = “string value goes here”
I populate the combobox when the window opens then have a listbox of customer records that can be selected.
Each time a customer is selected the record is loaded I want the combobox to change to reflect the stored List ID.
So … how best should I do this (the List ID from the database is not a straight list of numbers i.e. it might be 12, 16, 24 etc.)
Should I re-populate the list box when the record changes? or is there another way?
Cheers,
Paul[/quote]
Sorry, didn’t realise the combobox was already populated and the customer was changing.
So just do as Peter suggests, loop through the items in the combobox and check whether the current item has a row tag value matching the customer’s list ID value, if it matches set the ListIndex to that row’s index (from the loop variable) and exit the loop.
I’ve been using Extends a lot recently on a little project, it’s been really handy for filling in those little gaps of functionality in classes that would be a pain to sub-class.