Click to See Complete Forum and Search --> : why cannot work?


gambit
March 27th, 2001, 05:12 AM
hi

hope some one can help.
After running the code below i always can this run-time error '91', Object Variable or with block variable not set
why is that so?


private function myfn() as integer
dim i as integer

i = 1
if i = 1 then
fn = 0
msgbox ("0",)
else if i = 3 then
fn = 2
end if
end function

private sub mysub ()
dim abc as integer

abc = myfn

if abc = 1 then
msgbox "error:", , "debug"
elseif abd = 2 then
exit sub
end if
end sub




after 0 was printed out , the error message came out.

thank you!

Clearcode
March 27th, 2001, 05:30 AM
You should always include

option Explicit



as the first line in any module, form or class.
This forces you to declare variables before using them and the compiler would pick up the fact that you have decl;ared your function using the name "myfn" but tried to use "fn" to set it.
The corrected version is:

private function myfn() as integer
dim i as integer

i = 1
if i = 1 then
myfn = 0
msgbox ("0",)
else if i = 3 then
myfn = 2
end if

end function




HTH,
Duncan


-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com