functions in forms?

  • Thread starter Thread starter grog
  • Start date Start date
G

grog

Hi,

I am missing something. If I define a funtion within a module then it
will execute satisfactoriily from the immediate window. If I define a
funtion within a form then it does not execute satistactorily. Is this
a quirk of the immediate window? or should functions only be defined in
modules not form code?

thanks in advance.

Clive Grog
 
There is a difference between a standard module and a class module.

A function in a standard module is Public by default, and available from
anywhere.

A function in a form's module is available within that module only by
default.

You can change the default behavior by explicitly declaring it as a public
function, and referring to it through the full name of the module. So, if
you have a function called MyFunc in the module of Form1, you would declare
it as:
Public Function MyFunc()
and then call it like this:
Call Form_Form1.MyFunc()

But it would be better to put it in a standard module if you want to call it
form multiple locations.

If you do put it in a standard module, you cannot use the Me keyword
associated with the particular form. So you may need to pass in a reference
to that form, so you can use the variable in place of Me. Example:
Function MyFunc(frm As Form)
and call it form your form's module with:
Call MyFunc(Me)
 
Thanks,

I still cannot get a function written in the code for a form to run
from the IMMEDIATE window

My function is called MyFunc(). I have declared it as Public

I have tried:
?call Forms!Form1.MyFunc() error is "Expected Expression"
?call Forms_Form1.Myfunc() error is ditto
?call MyFunc() error is ditto
? MyFunc() error "Sub or function
not defined"

call Forms!Form1.MyFunc() error is "Objet Required"
call Forms_Form1.Myfunc() error is "Expected : , or ("
call MyFunc() error is "Sub or
Function not defined"

On the other hand, a function which I have defined in a module runs in
teh immdediate window with the statement
?MyFunc()

Can you tell me what I am doing wrong?

Thanks

Clive Grog


PS Is using the keyword "Call' the same as simply typing the name of
the procedure?
 
grog said:
Thanks,

I still cannot get a function written in the code for a form to run
from the IMMEDIATE window

My function is called MyFunc(). I have declared it as Public

I have tried:
?call Forms!Form1.MyFunc() error is "Expected Expression"
?call Forms_Form1.Myfunc() error is ditto
?call MyFunc() error is ditto
? MyFunc() error "Sub or function
not defined"

call Forms!Form1.MyFunc() error is "Objet Required"
call Forms_Form1.Myfunc() error is "Expected : , or ("
call MyFunc() error is "Sub or
Function not defined"

On the other hand, a function which I have defined in a module runs in
teh immdediate window with the statement
?MyFunc()

Can you tell me what I am doing wrong?

Is the Form open, are you in the Form's module, with the Immediate Window
open? Functions defined in a Form's module are available only when the Form
is Open. Oh, and Forms are in the Forms collection only when they are Open.
And, finally, what version of Access are you using -- because some were
"pickier" about how you refer to procedures in the Form's module than other
versions.

Larry Linson
Microsoft Access MVP
 
Back
Top