Public Sub

K

Kim

I have a Public Sub that is one form, but I am trying to call it from a
different form, but I can't. I am getting "Sub or Function not defined". I
thought if a sub routine was declared public you could call it from anywhere.

Thanks,

Kim
 
A

Andi Mayer

I have a Public Sub that is one form, but I am trying to call it from a
different form, but I can't. I am getting "Sub or Function not defined". I
thought if a sub routine was declared public you could call it from anywhere.

Thanks,

Kim

a form is a class-module and therefore it has to be in memory (the
form have to be open) to see this Sub.

either open the form or move the sub to a standard module
 
A

Arvin Meyer

Kim said:
I have a Public Sub that is one form, but I am trying to call it from a
different form, but I can't. I am getting "Sub or Function not defined". I
thought if a sub routine was declared public you could call it from
anywhere.

You can but you must refer to it like this:

Form_FormName.SubName

So, for instance, if you have a form named frmMessage with a public sub like
this:

Public Sub Hello()
MsgBox "Hello World"
End Sub

The syntax would be:

Form_frmMessage.Hello
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access
 
D

Dirk Goldgar

Arvin Meyer said:
You can but you must refer to it like this:

Form_FormName.SubName

So, for instance, if you have a form named frmMessage with a public
sub like this:

Public Sub Hello()
MsgBox "Hello World"
End Sub

The syntax would be:

Form_frmMessage.Hello

Or else

Forms!FormName.Hello

or

Forms("FormName").Hello

I don't like the class-module name syntax Arvin suggests, because (a) if
you have more than one instance of the form open, it's not clear which
one's Sub will be called, and (b) if the form is not open when you call
the method, it will be opened invisibly and remain open until you close
it. So I prefer to qualify the call with an explicit reference to an
open instance of the form.
 
Top