Calling class module event sub from Form.

  • Thread starter Thread starter P
  • Start date Start date
P

P

Hi. I created a form (Form) and a separate class module (ClassModule). When
I click on cmdButton on Form, the code cmdButton_Click in ClassModule
executes (sinking events). I would like to call cmdButton_Click from the
Form module without having to click on the command button or making
cmdButton_Click public? How can I do so? I tried to create an empty
cmdButton_Click in the form itself and see if it would sink down to the
class module. It does not seem to work. Thank you for your help. P
 
Making the event Public would solve your problem. You say you don't
want to do that, but you are in fact wanting to use it as if it were
Public, so I'm not sure what your concern is.

An alternate method would be to create another Public subroutine
("cmdButton_Click_Public") and then have that routine call the Private
event.

(Side note 1: I don't follow the terms "sink" and "sinking" in this
context.)

(Side note 2: If your form is actually named "Form", that sounds
like a bad idea.)

-Matt
 
Thank you Matt for your feedback. In the class module, I created:

Public Sub cmdSave_Click_Public()
cmdSave_Click
End Sub

In the form module, I call
cmdSave_Click_Public

When I compile, I get Sub or Function not defined. Any thought?
When I click the Save button in the main form, cmdSave_Click in class module
executes. Can I evoke the click event in the form without calling
cmdSave_Click() and let Access do the rest as if the user had actually
clicked the button? Thank you for your help. P
 
Hi,
You certainly can call the form's event from within the form.
cmdSave_Click_Public can't be the name of the event though.
It looks like your trying to call your class sub, not the click
event of the button.
 
It sounds like you want to call the same code in two
different contexts, when the user clicks save, and when
you want to do it through code. In this situtation I
usually define a sub that is called by cmdSave_click
event, and is scoped such that I call it from other
places as need be. For example, if you have a save
routine called by the cmdSave_Click event, and you want
to call it in response to a dialoge presented in the
Unload event make it private to the form and call from
either place. If you need to call it from other classes
and forms then you may be forced to make it public or
duplicate the code, you have to consider the trade offs
(bad duplicate code, bad, bad duplicate code, no treats).
HTH
-----Original Message-----
Thank you Matt for your feedback. In the class module, I created:

Public Sub cmdSave_Click_Public()
cmdSave_Click
End Sub

In the form module, I call
cmdSave_Click_Public

When I compile, I get Sub or Function not defined. Any thought?
When I click the Save button in the main form, cmdSave_Click in class module
executes. Can I evoke the click event in the form without calling
cmdSave_Click() and let Access do the rest as if the user had actually
clicked the button? Thank you for your help. P


Matthew Sullivan said:
Making the event Public would solve your problem. You say you don't
want to do that, but you are in fact wanting to use it as if it were
Public, so I'm not sure what your concern is.

An alternate method would be to create another Public subroutine
("cmdButton_Click_Public") and then have that routine call the Private
event.

(Side note 1: I don't follow the terms "sink" and "sinking" in this
context.)

(Side note 2: If your form is actually named "Form", that sounds
like a bad idea.)

-Matt
 
Back
Top