I haven't read this whole thread, so forgive me if this is irrelevant or already covered.
If you need the hWnd of an Access control there is a method here on The Access Web:
https://theaccessweb.com/api/api0027.htm
Do you really need to do the loop to get the line number?
What happens if you try:
Function GetLineNumber(F As Form, KeyName As String, KeyValue)
' ...
' Loop backward, counting the lines.
' Do Until RS.BOF
' CountLines = CountLines + 1
' RS.MovePrevious
' Loop...
-- ...
Totals AS
(
SELECT
ml.GENERATOR_ID,
SUM(ISNULL(ml.QUANTITY_HAZ_KG, 0)) AS decTotalKg,
-- ...
Do you need the ISNULL() here or is it another extra unnecessary function call?
I *think* SUM(ml.QUANTITY_HAZ_KG) AS decTotalKg should yield the same result.
Your description gives the feeling that you have a flaw in your tables design.
If all products are available to all warehouses then you should have a single products table and a separate table for warehouses, and then a junction table for WarehouseProducts.
Then, when you get a new product you...
You're welcome! (y)
(I'm guessing that you took the above 'Returns' code from here and hand-edited it to resemble what is output by the function because you still have double quotes around (New) !)
Just in case it isn't obvious from the code, the trick is to add the delimiter you use to separate your values in the string before and after the value you seek.
That way you avoid matching on partials.
However, you also have to avoid the gotcha where the string being searched does not have a...