Hi Mikeg,
As u have pointed out,even if the rule fails,the custom code still runs.Thats how it is.
So in addition to ur rules,why dont u put a condition in ur code that lets the custom code to run only if the query field is not blank.
something like...
if(QueryField's value is not blank)
{
custom code comes here
}
or you could avoid using rules and accomplish everything using code itself.
Regards,
Abhishek