Selecting Multiselect Listbox values from recordset data

Philkred

New member
Local time
Today, 06:16
Joined
Aug 22, 2019
Messages
14
Hi All,

I want to open up a form and on-load have a multi select listbox have its values already selected based on a table/recordsets values.

I think I need to loop through both the recordset and the listbox but im not sure on the correct code.

My failed attempt,
Code:
For Each varItem in rs2.fields(Task_ID)
    For i = 0 to Me.TaskSelection.ListCount -1
        If Me.TaskSelection.ItemData(i) = varItem Then
            Me.TaskSelection.Selected(i) = True
        End if
    Next i
Next varItem

This doesnt find the Task_ID so how do I correctly loop through the a particular recordsets values?

Thanks.

Sent from my SM-G965F using Tapatalk
 
Depends on the other code you have but try

Code:
rs2.movefirst
while not rs2.eof
    For i = 0 to Me.TaskSelection.ListCount -1
        If Me.TaskSelection.ItemData(i) = rs2!Task_ID Then
            Me.TaskSelection.Selected(i) = True
        End if
    Next i
wend
 
Hey, thanks for the reply CJ, however this now seems to lock up the whole database and access crashes :( this makes me think its endlessly looping somehow?

Sent from my SM-G965F using Tapatalk
 
Hey, thanks for the reply CJ, however this now seems to lock up the whole database and access crashes :( this makes me think its endlessly looping somehow?

Sent from my SM-G965F using Tapatalk

Hi. Try adding rs2.MoveNext just above the Wend.
 
Ok thanks so now it doesnt crash but still isnt selecting anything, could it be that I need to use column instead of item data?

Sent from my SM-G965F using Tapatalk
 
Ok thanks so now it doesnt crash but still isnt selecting anything, could it be that I need to use column instead of item data?

Sent from my SM-G965F using Tapatalk

Have you tried stepping through the code to make sure you're comparing the same data?
 
Right ok i think Ive got it to work by doing this,
Code:
 Me.TaskSelection.Column(0, i) + 0 = rs2!Task_ID
I guessed putting + 0 because I tried adding + 1 and it selected the task above the correct selection but what did I do wrong to have to put the + 0 as without it doesn't work?

Sent from my SM-G965F using Tapatalk
 
Right ok i think Ive got it to work by doing this,
Code:
 Me.TaskSelection.Column(0, i) + 0 = rs2!Task_ID
I guessed putting + 0 because I tried adding + 1 and it selected the task above the correct selection but what did I do wrong to have to put the + 0 as without it doesn't work?

Sent from my SM-G965F using Tapatalk

What does your Bound Column property say?
 
Ok I've tested it on both multi selection and single selections and it works, just worry that ive screwed up somewhere as it has to have the + 0. [emoji848]

Sent from my SM-G965F using Tapatalk
 
Ok I've tested it on both multi selection and single selections and it works, just worry that ive screwed up somewhere as it has to have the + 0. [emoji848]

Sent from my SM-G965F using Tapatalk

Hi. Glad to hear you got it sorted out. We might be able to figure out why it works this way and not the other if you could provide a sample file for testing.
 

Users who are viewing this thread

Back
Top Bottom