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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Assign object to varia...
Power Apps
Answered

Assign object to variable?

(0) ShareShare
ReportReport
Posted on by 386

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.

Categories:
I have the same question (0)
  • Verified answer
    Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @EdHansberry 

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

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

  • eka24 Profile Picture
    20,923 on at

    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.

  • EdHansberry Profile Picture
    386 on at

    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
     )
    )

     

     

     

  • EdHansberry Profile Picture
    386 on at

    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) )

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard