You can try using something like the expression below as a default value for your
Sample Number field in QGIS (see docs for info on default values)
But do not enable 'Apply default value on update' as you don't want your sample numbers to change if you go back and modify some other feature in the record.
I assume here you want to retain the text format with zero-padding of the numbers.
CASE WHEN "sample_taken" = 'Y'
However you may not see this work properly in QField as there seems to be a limitation on default values being created on insert when they rely on a value from another field in the feature being digitised (see issue #914 on GithHub)
If you encounter the above issue, one workaround for your case could be to collect your sample records on another layer.
Either create a separate file altogether, or duplicate the original layer reference in the layer panel (i.e., two references to the same file), with the original filtered to
"sample_taken" = 'N' (Non-sample layer) and the other filtered to
"sample_taken" = 'Y' (Sample layer).
Then set up the default value for
Sample Number, but only for the Sample layer, using just the third line in the above expression (so there is no reference to
When you are digitising in the field and you know you'll collect a sample, you need to switch to editing the Sample layer and then input your data; the
Sample Number should then autoincrement only within its own layer, without looking at records from the Non-sample layer (i.e. records where
"sample_taken" = 'N')