I think I cracked it.
Your bracketing formula helped a lot.
When a record is clicked on the other screen in the data table, I set a variable for the ID called DataTableID.
In the main screen I then check to see if this has a value - if it does then I do the lookup with that value. If not then I use the last inserted ID. The record is then updated and patched using either of those ID's. The reason it was creating a second record was because I was not resetting it to blank from the main menu if the new record option is selected. There are probably a million ways to write this more elegantly than I have, but I am a beginner who is learning. Thanks for your help and pointing me in the right direction.
Set(myItem,
LookUp(MyData,
ID = If(!IsBlank(DataTableID),
DataTableID,
LastAddedItem.ID
)
)
)
;
If(!IsBlank(myItem),
UpdateContext({msg:"Updating"});
UpdateContext({patchResult:
Patch(MyData,
{ID: If(!IsBlank(DataTableID),
DataTableID,
LastAddedItem.ID)},
{
Title:txtClientName.Text,
WriteOffTotal:Value(WriteOffAmt.Text),
AccountReference :txtAcctRef.Text
}
)
});
UpdateContext({msg:If(Not(IsBlank(patchResult)) And IsEmpty(Errors(MyData)),"Update Patch succeeded","Update Patch failed")});
Refresh(MyData);
,
UpdateContext({msg:"Adding Rec"});
UpdateContext({patchResult:
Patch(
MyData,
Defaults( MyData ),
{
Title:txtClientName.Text,
WriteOffTotal:Value(WriteOffAmt.Text),
AccountReference :txtAcctRef.Text
}
)
});
UpdateContext({msg:If(Not(IsBlank(patchResult)) And IsEmpty(Errors(MyData)),"Insert Patch succeeded","Insert Patch failed")});
Refresh(MyData);
Set(LastAddedItem,First(Sort(MyData,ID,SortOrder.Descending)))
)