cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
brentleslie
Frequent Visitor

Drop down boxes reverting to first value in SQL table

I have an Azure SQL database as my source for both form data and Dropdown box data.  There are 2 screens, one with a gallery which has a view from the SQL database as its source, which displays details (including the record ID) and another screen which contains a form that can be switched to Display/Edit/New. 

 

When a user clicks an arrow icon on the first form, it sets a variable to an ID: Set(var_sd_id,ThisItem.sd_id)

The second screens form has a DataSource: '[metadata].[source_destination]' and uses the variable var_sd_id to filter it's record. The forms Item property is: First(Filter('[metadata].[source_destination]',sd_id=var_sd_id))

 

The above seems to work fine, a record is selected on the second screen form after clicking the arrow icon on the first.

 

However, I have created a number of Dropdown Boxes on the form.  These appear to be set up correctly, they are in data cards and have the following settings for a field named "source_system_id", which is simply a FK-PK lookup to a table named "metadata.storage_system":

 

Card:

brentleslie_0-1605754145575.png

The Dropdown Box properties are:

ComboBoxProperties.PNG

The problem comes when I open the form.  The Dropdown box reverts back to the first record in the storage_system table, instead of the ID specified in the source_destination table.  I have looked for similar errors, many talk about the DefaultSelectedItems value, but I don't have that Setting to change (I assume it have been renamed to "Default"?).

 

I have also turned off the "Delayed Load" setting, which has changed the behaviour slightly but not changed this problem.

 

If I set the Value setting of the Dropdown box to system_id, the Dropdown box no longer reverts to the first item.  However I do not want the system_id to be displayed, I want the easy_name recorded in the source system table. 

 

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
brentleslie
Frequent Visitor

I fixed this myself, though I cannot seem to see anyone else solving this on this site for drop downs.  So I will post the solution here.  People should understand - this means I can use a drop down box based on SQL PK-FK relationships, just as you can in most other applications.  The drop down uses the data in the PK side and returns the PK ID which is saved in the FK field. (PK = Primary Key, FK = Foreign Key).

 

The following works for View, Edit and New form settings. For New, the Drop Down boxes appear as blank, which is good, because it picks up on default error handling when you create a form.  This means you don't need to put in custom error messages etc, you can use the default PowerApps ones when you create a form.  This also doesn't rely on creating Collections and re-storing the data in the PowerApps application.  It does require creating a few variables, but that's not abnormal or difficult.

 

My application uses a gallery on one screen used to view the data (now referred to as View screen). An arrow icon is then used in the gallery to open the View/Edit/Add Screen (now referred to as Edit screen).  The Edit screen has a form on it, with fields added as normal using the "Edit Fields" link in the properties. The Edit screen also has an Edit/Add and Save buttons.

 

When the user clicks the arrow icon from the gallery on the View screen, it does 3 things:

  1. Set the variable varEditSourceDestMode = "View" (this variable is more for easy tracking than anything else)
  2. Set the variable var_sd_id = the ID of the item in the gallery
  3. Opens the Edit screen

So far so good, the Edit screen is opened to "View" with the record selected on the form in View mode. However using Drop Down defaults, the issue I describe above occurs.  So lets modify the View Screen. Note the form on the View screen is named "frmSourceDestination":

 

  1. The screens OnVisible setting is set to the following formula:
    Switch(
    varEditSourceDestMode = "Edit",EditForm(frmSourceDestination),
    varEditSourceDestMode = "New",NewForm(frmSourceDestination),
    varEditSourceDestMode = "View",ViewForm(frmSourceDestination)
    )
  2. frmSourceDestination's DataSource is set to the FK table
  3. frmSourceDestination's Item setting is set to:
    First(Filter('[schema].[PK table]',sd_id=var_sd_id))
    This gives a warning about large datasets, which doesn't apply in my case, so I ignore
  4. The datacard containing the Drop Down has it's DataField property set to the FK table field ("source_system_id" in my case)
  5. The datacard Default is: ThisItem.FieldName
  6. The datacard Update field is set to the DropDown.Selected.PK_FieldName
    This is important as it ties the datacard value to the correct DropDown box value
  7. The datacard AllowedValues is the same as default ("DataSourceInfo(...")
  8. The Drop Down Items setting is the PK table
  9. The Drop Down Value selection is the field you want people to view when they use the Drop Down. I will call this "easy_name"
  10. The Drop Down Default value is:
    If(varEditSourceDestMode = "New", "",
    LookUp('[schema].[PK table]',PK_FieldName = ThisItem.FK_FieldName, easy_name)
    )
  11. The Drop Downs AllowEmptySelection is set to:
    If(varEditSourceDestMode="New",true,false)
    This allows the Drop Down to be blank, when you click the "New" button
  12. On the button that adds a new record, I have the following OnSelect formula:
    Set(varEditSourceDestMode,"New");NewForm(frmSourceDestination);UpdateContext({varResetDropdowns: true})
    Note - the above creates a new variable "varResetDropdowns"
  13. On the Drop Downs Reset setting, I set this to "varResetDropdowns"

 

I can now have drop downs linked to the PK table(s) which force selections for editing and new records.  In my case this is better than the closest solution (which is still good) I found here: https://www.youtube.com/watch?v=iayXCMFZ_CE

 

 

 

View solution in original post

1 REPLY 1
brentleslie
Frequent Visitor

I fixed this myself, though I cannot seem to see anyone else solving this on this site for drop downs.  So I will post the solution here.  People should understand - this means I can use a drop down box based on SQL PK-FK relationships, just as you can in most other applications.  The drop down uses the data in the PK side and returns the PK ID which is saved in the FK field. (PK = Primary Key, FK = Foreign Key).

 

The following works for View, Edit and New form settings. For New, the Drop Down boxes appear as blank, which is good, because it picks up on default error handling when you create a form.  This means you don't need to put in custom error messages etc, you can use the default PowerApps ones when you create a form.  This also doesn't rely on creating Collections and re-storing the data in the PowerApps application.  It does require creating a few variables, but that's not abnormal or difficult.

 

My application uses a gallery on one screen used to view the data (now referred to as View screen). An arrow icon is then used in the gallery to open the View/Edit/Add Screen (now referred to as Edit screen).  The Edit screen has a form on it, with fields added as normal using the "Edit Fields" link in the properties. The Edit screen also has an Edit/Add and Save buttons.

 

When the user clicks the arrow icon from the gallery on the View screen, it does 3 things:

  1. Set the variable varEditSourceDestMode = "View" (this variable is more for easy tracking than anything else)
  2. Set the variable var_sd_id = the ID of the item in the gallery
  3. Opens the Edit screen

So far so good, the Edit screen is opened to "View" with the record selected on the form in View mode. However using Drop Down defaults, the issue I describe above occurs.  So lets modify the View Screen. Note the form on the View screen is named "frmSourceDestination":

 

  1. The screens OnVisible setting is set to the following formula:
    Switch(
    varEditSourceDestMode = "Edit",EditForm(frmSourceDestination),
    varEditSourceDestMode = "New",NewForm(frmSourceDestination),
    varEditSourceDestMode = "View",ViewForm(frmSourceDestination)
    )
  2. frmSourceDestination's DataSource is set to the FK table
  3. frmSourceDestination's Item setting is set to:
    First(Filter('[schema].[PK table]',sd_id=var_sd_id))
    This gives a warning about large datasets, which doesn't apply in my case, so I ignore
  4. The datacard containing the Drop Down has it's DataField property set to the FK table field ("source_system_id" in my case)
  5. The datacard Default is: ThisItem.FieldName
  6. The datacard Update field is set to the DropDown.Selected.PK_FieldName
    This is important as it ties the datacard value to the correct DropDown box value
  7. The datacard AllowedValues is the same as default ("DataSourceInfo(...")
  8. The Drop Down Items setting is the PK table
  9. The Drop Down Value selection is the field you want people to view when they use the Drop Down. I will call this "easy_name"
  10. The Drop Down Default value is:
    If(varEditSourceDestMode = "New", "",
    LookUp('[schema].[PK table]',PK_FieldName = ThisItem.FK_FieldName, easy_name)
    )
  11. The Drop Downs AllowEmptySelection is set to:
    If(varEditSourceDestMode="New",true,false)
    This allows the Drop Down to be blank, when you click the "New" button
  12. On the button that adds a new record, I have the following OnSelect formula:
    Set(varEditSourceDestMode,"New");NewForm(frmSourceDestination);UpdateContext({varResetDropdowns: true})
    Note - the above creates a new variable "varResetDropdowns"
  13. On the Drop Downs Reset setting, I set this to "varResetDropdowns"

 

I can now have drop downs linked to the PK table(s) which force selections for editing and new records.  In my case this is better than the closest solution (which is still good) I found here: https://www.youtube.com/watch?v=iayXCMFZ_CE

 

 

 

View solution in original post

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Apps Community Call

Monthly Power Apps Community Call

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

secondImage

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV STudio

Power Platform ISV Studio

ISV Studio is the go-to Power Platform destination for ISV’s to monitor & manage applications post-AppSource publish.

Top Solution Authors
Top Kudoed Authors
Users online (71,595)