cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Advocate V
Advocate V

Assign object to variable?

Is there a way to assign an object to a variable to make code easier to change and read? For example:

If(
    IsBlank(txtItemSetupWidth.Text),
    White,
    If(
        Or(
            Value(txtItemSetupWidth.Text) < 0,
            Not(IsNumeric(txtItemSetupWidth.Text))
        ),
        ColorFade(
            Red,
            50%
        ),
        White
    )
)

 

in VBA I could do something like this:

With txtItemSetupWidth
If(
    IsBlank(.Text),
    White,
    If(
        Or(
            Value(.Text) < 0,
            Not(IsNumeric(.Text))
        ),
        ColorFade(
            Red,
            50%
        ),
        White
    )
)
End With

 

Excel is also rolling this functionality in formulas with the new LET() function, but LET doesn't exist in PowerApps, and WITH() seems to do something totally different.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User II
Super User II

Hi @EdHansberry 

Have you seen Shane Young's @Shanescows  video about the PowerApps With() function? 

https://www.youtube.com/watch?v=AYD2oWue7fw  

View solution in original post

4 REPLIES 4
Super User II
Super User II

Hi @EdHansberry 

Have you seen Shane Young's @Shanescows  video about the PowerApps With() function? 

https://www.youtube.com/watch?v=AYD2oWue7fw  

View solution in original post

Super User III
Super User III

OnStart or OnVisible or OnSelect of a button Set a variable:

Set(Mycolor,txtItemSetupWidth.Text)

 

Then use the variable Mycolor to replace txtItemSetupWidth

------------

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

Thanks @Drrickryp . Definitely helpful. I'm still not getting it 100%. For example:
This works. If the text box isn't blank and has a positive and numeric value, it returns white, otherwise red.

 

 

With(
    {TextBox: txtItemSellingUnitWidth.Text},
    Switch(
        true,
        IsError(Value(TextBox)),
        ColorFade(
            Red,
            50%
        ),
        Value(TextBox) < 0,
        ColorFade(
            Red,
            50%
        ),
        Not(IsNumeric(TextBox)),
        ColorFade(
            Red,
            50%
        ),
        White
    )
)

 

 

 

This next one though doesn't work. It is returning red, but it is a valid formula in the sense that PowerApps is evaluating it and returning red. I just need to dig into which of those is causing it, is it the value(TextBox.Text) isn't working or Not(IsNumeric(TextBox.Text)) isn't working.

 

In this example it doesn't matter, but it would if I wanted to evaluate different properties of the txtItemSellingUnitDepth object, not just the .Text property.

 

 

 

With(
    {TextBox: txtItemSellingUnitDepth},
    Switch(
        true,
        IsError(Value(TextBox.Text)),
        ColorFade(
            Red,
            50%
        ),
        Value(TextBox.Text) < 0,
        ColorFade(
            Red,
            50%
        ),
        Not(IsNumeric(TextBox.Text)),
        ColorFade(
            Red,
            50%
        ),
        White
    )
)

 

 

 

It is this guy:

With(
    {TextBox: txtItemSellingUnitDepth},
         Value(TextBox.Text) )

 If there is a numeric value in the text box, it still returns 0.  Hrm... actually this returns false, so it isn't recognizing the numbers in the text box as numeric...

With(
    {TextBox: txtItemSellingUnitDepth},
         IsNumeric(TextBox.Text) )

Helpful resources

Announcements
New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

Top Solution Authors
Top Kudoed Authors
Users online (99,847)