The above works perfectly on the form.
As for the SQL formatted query in VBA editor: I narrowed the error down to when the QOH is calculated.
First, I had to replace the Nz function with IsNull.. i.e. IsNull([SumOfQuantity_goodsout],0) AS StockOut.
I also isolated the [SumOfQuantity_goodsout]...