call sub from other module

  • Thread starter Thread starter Jesper
  • Start date Start date
J

Jesper

If I have a private sub in form1:

Private Sub cmdOK_click()
End sub

And I want to run this from within form2's module - is that possible?
Or must I make it public as:

Public Sub cmdOK_click()
End sub

Or should I just put the content of it in a public sub and run that from
where ever I need it?

Public sub myOKcode()
End sub


Thanks,
Jesper
 
While you can make it public, and then refer to the form when calling the
routine:

Call Form_Form1.cmdOK_Click()

you're probably better off putting it in a module, rather than associating
it with a form. Remember that Form1 would have to be open in order for you
to be able to call its code.
 
You can do it by referencing the form the sub is in:
Call Forms!form1.cmdOK_click()

However, I would suggest you put the code in a standard module and make it
public. Then, as you noted, you can call it from anywhere.
 
While you can make it public, and then refer to the form when calling the
routine:

Call Form_Form1.cmdOK_Click()

you're probably better off putting it in a module, rather than associating
it with a form. Remember that Form1 would have to be open in order for you
to be able to call its code.

Great, thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top