Screenshot of primary key being selected
Set you primary key by right clicking on the column to the left

The first argument of ‘Patch’ should be a collection

There’s an incredibly common – and infuriating – error message that I keep running into, so I’m making a note here so I don’t forget! If you see “The first argument of ‘Patch’ should be a collection”, and you’re stuck, I have a quick fix for you!

In PowerApps, a Patch command sends information to your database. So, for example, if your app is connected to a table called ‘[dbo].[FriendsList]’ and you want to change Monica’s phone number to 555-12345 (whether it’s blank at the moment or has an old number in there), you could add this code to the OnSelect function of a button:

Patch(‘[dbo].[FriendsList]’, First(Filter(‘[dbo].[FriendsList]’, Name = ‘Monica’)), {PhoneNo: “555-12345”})

In order, these commands:

  • Determine the ‘collection’ that a record is going into (in this case ‘[dbo].[FriendsList]’). A collection can be a database table, or a local collection built with the Collect() command, for example.
  • Find the First (assuming the only, but more of that another time) item in theĀ ‘[dbo].[FriendsList]’ database table where the ‘Name’ column has ‘Monica’ in it
  • Fill in the ‘PhoneNo’ column of the table with “555-12345”

The first argument of ‘Patch’ should be a collection

So we know that the first argument of this Patch is a collection. So why the error?!

If you swear you have this correct (and I’ve sworn a lot over this when making PowerApps), but you’re still getting the message “The first argument of ‘Patch’ should be a collection” head to your database. For example, if it’s a SQL Server database, you can open SQL Server Management Studio (SSMS) and find your table. Right click on the table name in the Object Explorer and select ‘Design’.

You need to make sure your table has an ID column with a primary key. Right click on the column where the little black arrow is, and select ‘Set Primary Key’.

Screenshot of primary key being selected
Set your primary key by right clicking on the column to the left

Next, make the column the ‘Identity’ column. Do this by going to the bottom of the screen and finding ‘Identity Specification’ and clicking the little black arrow next to it. Then to the right of ”(Is Identity)’ click on ‘No’, to bring down the drop-down menu. Now you can click on ‘Yes’. This means your ID number will increase by one automatically each time you add a new record.

Screenshot of Identity Column being created
Turn the Identity Specification of your column to ‘Yes’

These two measures make sure each of your records can be distinguished from the next one, and your error message will be no more! Why that has anything to do with collections being missing is, for now, beyond me.

Damn Microsoft for its idiotic error messages!

Leave a Reply

Your email address will not be published.