Somehow I’ve made this hard. I’ve got a popup and the list is derived from a database. The rows of the popup are populated when the window opens. A user can create a new record and select a value from the popup. However, if it’s an existing record I want to set the popup to show what the current value is. That removes the original list and just leaves the one item in the popup. So if the user wants to change the popup to another value there’s nothing there to choose from.
The only solution I can think of is to populate the popup rows from the database and then use a for/next loop to find the index of the selected value and then set the popup to that index. Does that sound right?
I did this a few days ago. I populated the list with every row from the database, and at the same time I added the PKID from each record into the rowtag. If the record is existing, you can set its value by either searching the PKID held in the rowtag or search the text values until you find a match, then set the listindex to that of the matching row.
Its lends itself particularly well when working with lookup tables which is where I originally started looking at it. Keeping the PKID in the rowtag meant I didnt have to store it elsewhere, and when I commit a new record, the lookup value is instantly available from pm.RowTag(pm.Listindex).
The method you’re talking about is what we use all the time. In the open event we load the popup menu and set the row tag to either the primary key of the table or load the ActiveRecord object into it.
We have several extends methods for the PopupMenu that allows us to set and get the id from the popup very easily so you don’t have to loop through the list every single time. We have another one that allows us to set the text and row tag in one step too. Don’t underestimate the power of extends.
There’s no secret. Pretty basic stuff. This is for a WebPopupMenu but it’s identical code for a regular Popupmenu.
[code]Sub SetId(extends pm as WebPopupMenu, id as integer)
if id < 1 then
pm.ListIndex = -1
return
end if
for i as Integer = 0 to pm.ListCount - 1
if pm.RowTag(i) isa Data.ActiveRecordBase then
if Data.ActiveRecordBase(pm.RowTag(i)).id = id then
pm.ListIndex = i
return
end if
end if
next
End Sub[/code]
[code]Function GetID(extends pm as WebPopupMenu) As Integer
if pm.ListIndex = -1 then return 0
if pm.RowTag(pm.ListIndex) isa Data.ActiveRecordBase then
dim id as integer = Data.ActiveRecordBase(pm.rowtag(pm.ListIndex)).id
return id
end if
return 0
i guess most good programmer are also lazy programmer… if we do certain things more than twice, we make a method or function instead instead of having the code in multiple place.
Thanks Bob, I have the same. I was more intrigued by “so you don’t have to loop through the list every single time” which made it sound as if you had found a way to not having to loop. But you still loop, you just moved the loop into an extension (like I did), so I misunderstood the sentence.
No worries. It’s really ‘you don’t want to write the code to loop through it every time you need it’. I’ve worked on some projects where the original developer wrote the same looping code a thousand times by copying and pasting code and changing variable names. And then wondered why it was so hard to update his own code when it changed.
Be lazy. If you find yourself copy/pasting/changing variable names you might want to think about subclassing or using extends.
I have 2 extends going on this now too. One to set the listindex value, and another to get it. They both work really well.
#buffoonalert
For reasons known only to the devil himself, I started the loop with ‘for i as integer = 0 to pm.listindex - 1’ (Instead of listcount-1) and had the customary period of head scratching and wondering why it didn’t do a thing.
Here is a suggestion:
After loading into a record set:
if rs.Field(“Sex”).StringValue = “Female” then
SexPopupMenu.ListIndex = 1
elseif rs.Field(“Sex”).StringValue = “Male” then
SexPopupMenu.ListIndex = 0
else
SexPopupMenu.ListIndex = -1
end if
Regards,