Selecting Multiselect Listbox values from recordset data (1 Viewer)

Philkred

New member
Local time
Today, 09:30
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
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:30
Joined
Feb 19, 2013
Messages
16,553
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
 

Philkred

New member
Local time
Today, 09:30
Joined
Aug 22, 2019
Messages
14
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
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.
 

Philkred

New member
Local time
Today, 09:30
Joined
Aug 22, 2019
Messages
14
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
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?
 

Philkred

New member
Local time
Today, 09:30
Joined
Aug 22, 2019
Messages
14
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
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?
 

Philkred

New member
Local time
Today, 09:30
Joined
Aug 22, 2019
Messages
14
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:30
Joined
Oct 29, 2018
Messages
21,357
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

Top Bottom