web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Multiple ListBox selec...
Power Apps
Answered

Multiple ListBox selections into Collection

(0) ShareShare
ReportReport
Posted on by 8

I am trying to get a ListBox working whereby a user can select multiple options and then click on a button and it adds these selections into a collection.

 

I can get it to a point whereby if a user is selecting for example, two things from the ListBox, and click the button, it will then two records into the Collection. However, it always adds these two records with the first selected item in the listbox. I.e. if 1 and 2 are selected, it creates two records both with 1 rather than one with 1 and the other with 2.

 

Is there a way of getting it to work as required?

 

I've tried various different ways, but cannot get it to work.

 

Many thanks in advance.

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @jworth_tcs 

    In most cases you don't need a collection as the Listbox control already has a table of the selected items.

    But if you want to duplicate it into a collection, then use the following:

        ClearCollect(yourCollection, yourListBox.SelectedItems)

     

    You can also capture this into a variable:

       Set(yourItems, yourListBox.SelectedItems)

       -or-

       UpdateContext({yourItems: yourListBox.SelectedItems})

     

    Or you can just refer to yourListBox.SelectedItems in any place that you need to reference the currently selected items.

     

    I hope this is helpful for you.

  • jworth_tcs Profile Picture
    8 on at

    @RandyHayes - many thanks for your prompt response. I have given this a go and it still adds to the collection the number of records based on the number of selected values. However, the entries all have the first selected item in the listbox rather than adding one record per listbox selected item.

     

    Screenshots are below of the various aspects.

     

    Listbox with two selected values.PNGCollection after adding entry.PNGAdd Entry button code.PNG

    I've only just got my head round collections and Galleries. Am I misunderstanding something, hence it not working?

     

    Thanks again

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @jworth_tcs 

    I assume that you are using Timesheet_Entries as your Items property on the Gallery.  

    Your formula there is referencing the selected value of the ListBox1 - this will always be the same value.

    Change your formula to the following:

    ClearCollect(Timesheet_Entries,
     ForAll(
     ListBox1.SelectedItems,
     {
     ClaimDate: Date_WeekDay_Day & " " & Welcome_drpClaimMnth.Selected.Value & " " & Welcome_drpClaimYear.Selected.Value,
     ClaimReason: Timesheet_txtClaimReason.Text,
     ClaimStartTime: Timesheet_lblStartHrMins.Text,
     ClaimFinishTime: Timesheet_lblFinishHrMins.Text,
     ClaimHrMinDec: Timesheet_txtClaimHrMinDec.Text,
     ClaimMonth: Welcome_drpClaimMnth.Selected.Value,
     ClaimYear: Welcome_drpClaimYear.Selected.Value
     }
     )
    )

    Note on the above - ForAll returns a table, simply assign it to what you want, in this case a collection.  It is commonly misused as just a loop...it returns a value.

    Also note that you are creating a record for each ListBox1.SelectedItems record.  So, you set your ForAll to that record source and then reference the value in that record.  In this case the Date_WeekDay_Day.

     

    Also note: unless for some reason you need a collection, you can just assign this formula (minus the clear collect line and associated closing paren) to the Items property of your Gallery.

     

    That should give you what you need.

     

  • jworth_tcs Profile Picture
    8 on at

    @RandyHayes  - that's worked perfectly! Thanks very much for your help.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 320 Most Valuable Professional

#2
11manish Profile Picture

11manish 210

#3
Valantis Profile Picture

Valantis 167

Last 30 days Overall leaderboard