Screenshot of primary key being selected

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!

Screenshot of camera tool being selected in PowerApps

Empty columns in a PowerApps Collection

A Collection in PowerApps is a temporary table of data, that only exists in your app, and only while its open. When you use Collect you usually create all the data as you go. But you might need to leave some empty columns.

You can use the data in a Collection in the same way you might use an Excel spreadsheet, a SharePoint list or a SQL Server table: you can create Galleries, or Tables to show the data, or use the data to do calculations. Leaving empty columns means you can take your user back to add that data later.

Creating a Collection in PowerApps

You create a Collection using this kind of code:

Collect(CollectionName, {ColumnName1: Row1a, ColumnName2: Row1b}, {ColumnName1: Row2a, ColumnName2: Row2b})

This creates a Collection which looks like this kind of table:

ColumnName1 ColumnName2
Row1a Row1b
Row2a Row2b

Creating a Collection with empty columns

But I needed to create an app with some empty columns.

It would be a camera app, which lets you take a lot of photos quickly (by tapping on the Camera) and then displays those in a strip of small images along the bottom of the screen. You’re then taken to another screen where you can fill in some details (such as description and name) for each photo.

That second step involves creating the Name and Description columns in my Collection first, but leaving them empty.

If my Collection was to be called ‘Photos’ then the code would look like this:

Collect(Photos, {Image: Camera1.Photo, Name: "", Description: ""})

As simple as that: put all blank fields as quote marks. (‘Camera1.Photo’ is the photo that the camera takes when you tap or click it.)

Now we end up with a Collection called Photos like this:

Image Name Description
<photograph>
<photograph>

So that when I add my Gallery or Form on another page, I just make its Items attribute ‘Photos’. The app will create three fields in total, even though they have no data in them yet.