David,
First, Thisform is a relative reference to a fomr object that only has meaning in code that is "IN" the form. Your UDF is NOT IN the form so the Thisform means nothing in your UDF. When your UDF is running the filter cannot see THISFORM. Using Thisformin a filter is an invitation to the very error you report.
Instaed put the value of ther propety into the filter directly using a macro or an evaluate() as below;
lcValue = Thisform.Property
SET FILTER TO Filed = EVALUATE(lcValue)
This way the filter is not depentand on either the Thisform being relavant nor the variable lcValue being in scope.
As for the COUNT, there are a number of fox commands that don't respect the WITH stuff, LOCATE for example.
As for the modal form you refered to, well when the COUNT runs the fil;ter needs to be evaluated and the filter is being evaluated in the modal fomr so Thisform refers to that one then. See above to fix that.