Setting selected rows in a list

Always_Learning

Registered User.
Local time
Today, 17:39
Joined
Oct 7, 2013
Messages
71
Hi There,
Can you help me please.
I know how to loop through a list and get the rows that are selected but I need to know how to loop through a list and mark rows as selected.

I have a list with say 4 items in it and if the user changes their mind and wants edit and add more to this list, I will show another list with all the relevant records but I need to set as selected the records that match the 4 in the first list. This allows them to see the ones they had previously selected.

Hope that makes sense.

Thanks for any help.

Best Regards,
 
This says the select property is Read/write so I suspect you just set it to True to select it.
 
As Steve says the property can be set. I build a delimited string with the selected items, then loop the listbox and use the string to set the selected items:

Code:
    For i = 0 To ctl.ListCount - 1
      SearchItem = ctl.ItemData(i) & ";"
      ctl.Selected(i) = (InStr(strSelected, SearchItem) > 0)
    Next i
 
Hi There,
Thanks for your help.
I can't get it working for some reason.
My strItems value is "AY1001; AY1005;AN2001;"
But my code only shows true on finding ";"
Something is off. At the start when I step through the code and look at strItems & SearchItem they have the same kind of values in them but when a true is found SearchItem contains ";"
This is my code:
Set ctlSource = Lst_DBTarget
For intCurrentRow = 1 To ctlSource.ListCount - 1
SearchItem = ctlSource.Column(1, ctlSource.ItemData(intCurrentRow)) & ";"
TempVar = (InStr(strItems, SearchItem) > 0)
If TempVar = True Then
ctlSource.Selected(intCurrentRow) = True
End If
Next intCurrentRow

Set ctlSource = Nothing

Any Ideas?

Best Regards,
 
Set a breakpoint on the line that follows "If TempVar = True Then" and to examine your variables. You can hover the cursor over them or use the Immediate Window and Debug.Print to find out their contents. From the syntax you show and the symptom you describe, it seems to me that your variable "SearchItem" is at that point a null or an empty string, one or the other of those two.
 
surely you shouldn't search for the semi colon in the column value. that's a column separator, not a value.

your list is simply these
AY1001 AY1005 AN2001


I can't tell from inspecting the code, but maybe just

SearchItem = ctlSource.Column(1, ctlSource.ItemData(intCurrentRow))
 
Last edited:
Hi There,
Thanks for the reply.

When I build the SearchItem string with this bit of code the semicolon is added.
SearchItem = ctlSource.Column(1, ctlSource.ItemData(intCurrentRow)) & ";"

I used that piece of code which someone posted in this help topic.

Thanks again for the help, it's very appreciated.

Best Regards,
 
Shouldn't it be...
Code:
SearchItem = ctlSource.Column(1, intCurrentRow)
This...
Code:
ctlSource.ItemData(intCurrentRow)
... gives you the value of the bound column at the given row, and it can't be right to be passing that to the Row parameter of the .Column property.
 
@Dave, as long as the semi-colon is in both sides, it works. I include it so that numeric comparisons don't fail. IOW, if I'm searching for 1, I don't want 10, 31, 112 being considered matches.

@Always, can you attach a sample db that exhibits whatever error you're experiencing?
 
Hi There,

Just to explain what I'm trying to do.

I build my strItems which has a value of "AY1001; AY1005;AN2001;"
I want to loop through my list control and if there is a match on 1 of the values in strItems, set the record as selected in my list control.
so if a match for "AY1001" is found in my list control set it as selected.

Thanks for the help.

Best Regards
 
The code posted does that. I actually needed to do this last night for a client, adapted the same code. It worked perfectly. The guts of it:

Code:
    Set db = CurrentDb()
    Set ctl = Me.lstItems

    strSQL = "SELECT ..."
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

    Do While Not rs.EOF
        strSelected = strSelected & rs!Item & ";"
        rs.MoveNext
    Loop

    For i = 0 To ctl.ListCount - 1
        SearchItem = ctl.ItemData(i) & ";"
        ctl.Selected(i) = (InStr(strSelected, SearchItem) > 0)
    Next i
 

Users who are viewing this thread

Back
Top Bottom