How do you call the same Sub in 2 different forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a Private Sub cmdViewROEDetails_Click() that I have a lot of code in.
I want to take advantage of that lengthy code on another form. Yeah, I can
copy paste it to a Private Sub on the new form, but I am thinking ahead to
when I may need to change a piece of it. I wouldn't want to have to remember
to go to all the forms' code and make each small change and remember to make
it everywhere.

How would I call the piece of code? Is that what a Public sub is for? If
so, do I make Public Sub cmdViewROEDetails_Click and put my code in it. Then
how do I call it from another form?
 
Hello worksfire1.
I have a Private Sub cmdViewROEDetails_Click() that I have a lot of
code in. I want to take advantage of that lengthy code on another
form. Yeah, I can copy paste it to a Private Sub on the new form,
but I am thinking ahead to when I may need to change a piece of it.
I wouldn't want to have to remember to go to all the forms' code
and make each small change and remember to make it everywhere.

How would I call the piece of code?

Call a Public Sub procedure stored in a standard module where the
"lot of code" is in. If you intended to use "Me" in your code, you
can use CodeContextObject instead.
Is that what a Public sub is for?

A Public Sub can be called from anywhere in your project, a Private
Sub (like the event procedures) can only be called from within the
module where it resides.
If so, do I make Public Sub cmdViewROEDetails_Click and put my
code in it.

No. I suggest not to make event procedures public. A Public Sub in
a form becomes a method of the form (like Requery) and would be
called as Forms!NameOfTheForm.cmdViewROEDetails_Click.
Then how do I call it from another form?

Store the code in a public sub in a standard (not class) module
and call that sub from the event procedures in your forms.
 
Back
Top