setting PopupMenu value from database field value

I have a Breeders table that you could think of as being like a contacts table or address book and a Breed table for all the different breeds (there will only be 10-12 in this application).

When the window opens to maintain the Breeders table, a method runs to load the list of breeds in a popup menu with all the Breed Names. The user then selects the appropriate breed for the selected Breeder and the text value of Breed Name is written to the database record. Now when the user comes back later to update the Breeders and I load the TextFields on the window with the database values. How do I set the popupmenu.index to the entry that matches the breed in the database record?

Is there a better way to do this but I like the popup menu for it?

thanks
bill

Found the answer. no longer a question

I have the same problem - can you add your solution so I may learn?

Philip, the answer is actually a control made by Paul Lefever called Selectable Popup Menu,

see Examples/Desktop/Custom Controls/SelectablePopupMenu

We do this using extends, so no need to use a subclass. When we load the PopupMenu we pass the text and tag in at the same time:

[code]Public Sub AddRowAndTag(extends PM as popupmenu, Text as string, Tag as Variant)
pm.AddRow Text
pm.RowTag(pm.ListCount-1) = tag

End Sub
[/code]

me.addRowAndTag “First Value”, 1
me.addRowAndTag “Second Value”, 2
me.addRowAndTag “Third Value”, 3

When we want to set the popup menu to the correct ID (because your really want to work with ID’s the than text in the database) we call this:

Public Sub SetFromRowTag(extends pm as PopupMenu, tag as Variant) for i as integer = 0 to pm.ListCount-1 if pm.RowTag(i) = tag then pm.ListIndex = i return end next End Sub

Usage: PopupMenu1.SetFromRowTag(2) //Will set to second value.

If you must set by text we do this:

Public Sub SetText(extends pm as PopupMenu, text as String) for i as integer = 0 to pm.ListCount -1 if pm.list(i) = text then pm.ListIndex = i exit end next End Sub

Usage: Popupmenu1.SetText(“First Value”)

I find the extends to be simpler to use because you don’t need the subclass and AutoComplete still works with it.

1 Like