You can find the meaning of all error numbers in the manual under "Error messages". Error 040 just means a wrong number of optimize calls. You can not sometimes optimize and sometimes not - either you call optimize in every run, or not at all.

http://manual.zorro-trader.com/optimize.htm

If you can't find the problem, please post your script here and I'll look over it.