cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Automate2create
Helper II
Helper II

Multiple variables OnChange property

I have a combo box called cboType.  The 5 choices available are Cars, Lorries, Motorbikes, Plane, Helicopters. 

 

The code I have available on the OnChange property is: 

If(cboType.Selected.'Type' = "Cars", Set(vTransportType, 1), 

    cboType.Selected.'Type' = "Motorbikes", Set(vTransportType, 2),  

    cboType.Selected.'Type' = "Lorries", Set(vTransportType, 3), 

cboType.Selected.'Type' = "Planes", Set(vTransportType, 4), 

cboType.Selected.'Type' = "Helicopters", Set(vTransportType, 5)); 

 

Then there are some combo boxes, text field etc below that I want to show dependant on the type of transport selected. 

Each box has code on the Visible property like this: 

If(vTransportType = 1, true, false) //Cars 

Or in some cases multiple transport types will reveal the same combox box 

If(vTransportType = 1, true, false, //Cars  

    vTransportType = 2, true, false) //Motorbikes 

 

There are no errors in my code but it is not revealing the boxes per transport type as I would like. 

The global variables also show vTransportType but show it as empty.  What am I doing wrong? 

4 REPLIES 4
zmorek
Super User
Super User

Try not using a variable, in the past I've had hard times getting a combo box to use OnChange successfully. Can you instead, on the label's Visible property, try something like:

cboType.Selected.'Type'="Cars" || cboType.Selected.'Type'="Motorbikes"

Unless you absolutely need the variable for use elsewhere I don't think you need it just for this case.

RandyHayes
Super User
Super User

@Automate2create 

Another option to consider is to let your Combobox data drive your app.  If you absolutely need to do some conversion from the Text to a number type, then that can all be part of the Combobox.

 

I don't know what your Items property is on the Combobox, but an example would be this in the Items:

Table(
    {Type: "Cars", TransportType: 1}, 
    {Type: "Motorbikes", TransportType: 2},  
    {Type: "Lorries", TransportType: 3}, 
    {Type: "Planes", TransportType: 4}, 
    {Type: "Helicopters", TransportType: 5}
)

Then your Visible property on other controls would be like this:

cboType.Selected.TransportType = 1 || cboType.Selected.TransportType =2

 

Advantage of this is only that if the Names in the Type will change, then you don't have to change the text name in all the formulas in the app.

 

Note, variables are NOT needed on controls like this unless you need a SnapShot of it (non-changing).  The controls are all global in PowerApps, so, from any place in your app, you can always reference cboType.Selected and get the value there.  Setting a variable is just an extra step you don't need.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
Automate2create
Helper II
Helper II

@RandyHayes Thank you so much!  This is exactly what I needed and I have applied it. There is one minor issue.  Sometimes, when the fields appear, if fields are not visible, there are gaps left between the fields.

 

For example, when Cars is selected on the TransportType drop down, lots of fields appear apart from three fields that relate to Planes which are: Wing, Flap, Jet Engine.  When I select Cars, lots of fields appear but there is a gap of three fields where Wing, Flap and Jet Engine would be. 

 

So it works and is the best result so far but the result is a bit 'patchy'.  Have you got any ideas?  I would love to be able to set it up so that when Cars is selected, it can prioritise the other fields over the hidden Planes fields.   

RandyHayes
Super User
Super User

@Automate2create 

This is more related then to the layout you have.  Is this all freeform or are you using an EditForm?

Perhaps a picture of what you are seeing would help!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (2,480)