Load Combobox selection.

Hi,

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.

Hi Paul,

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.

Hi Ian,

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

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”

If your listID is in the rowtag of the combo box you can use the same method above modified to check the rowtag value.

[quote=108645:@Paul Stevenson]Hi Ian,

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.

Thanks guys,

This makes sense although I have never used extends methods but them look very useful (another gap in my knowledge plugged).

Yes, using the rowtag (changing one line for anyone viewing this in the future) sorted it for me:

for i as integer = 0 to cb.ListCount-1 if cb.RowTag(i) = s then cb.ListIndex = i exit end next

Thanks,

Paul

Extends are great. Having a handy module with your favorites is a big time saver for new projects.

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.

@Peter Fargo - I just came a cross this thread. Nice solution. Comes in very handy. Thanks for sharing