VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Identical sub procedures for 2 userforms

3 REPLIES 3
Reply
Message 1 of 4
Anonymous
277 Views, 3 Replies

Identical sub procedures for 2 userforms

I have two userforms in my module. Some of the required sub procedures are
needed for both userforms. So, a procedure that I've created for UserForm1
(Code) has been duplicated and placed also in UserForm2 (Code). However,
this seems wasteful, since the code is identical. Isn't there a way to make
the code accessible to both userforms without repeating it? I tried placing
the procedure in Module1 (Code), and although the program seems to know its
there (it doesn't give me an unknown procedure error) it doesn't seem to do
anything. I declared it as a public sub which, as I understand it, should
make it readable to all modules. Do I need to resign myself to duplicating
the same code within the two userforms?
3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

Randy,
Following works for me;


Form1(Code)
Private Sub Command1_Click()
Call Module1.junk
End Sub

Form2(Code)
Private Sub Command1_Click()
Call Module2.junk
End Sub

Module1(Code)
Public Sub junk()
Msgbox("From Module1")
End Sub

Module2(Code)
Public Sub junk()
Msgbox("From Module2")
End Sub

Cheers,
Kenneth Hutson
San Antonio, Texas


"Randy Richardson" wrote in message
news:977ED29D84CAAC8CADF7309969A60B91@in.WebX.maYIadrTaRb...
> I have two userforms in my module. Some of the required sub procedures
are
> needed for both userforms. So, a procedure that I've created for
UserForm1
> (Code) has been duplicated and placed also in UserForm2 (Code). However,
> this seems wasteful, since the code is identical. Isn't there a way to
make
> the code accessible to both userforms without repeating it? I tried
placing
> the procedure in Module1 (Code), and although the program seems to know
its
> there (it doesn't give me an unknown procedure error) it doesn't seem to
do
> anything. I declared it as a public sub which, as I understand it, should
> make it readable to all modules. Do I need to resign myself to
duplicating
> the same code within the two userforms?
>
>
Message 3 of 4
Anonymous
in reply to: Anonymous

Thanks, Kenneth.

It looks like my problem was the fact that pulling the procedure out of the
userform requires me to now indicate the userform on which I'm changing
captions. So, the problem is now solved. My method of calling the
functions worked just fine, it just didn't know on which userform to change
the captions.

-Randy

"Kenneth Hutson" wrote in message
news:C3D007120407A0F8F3218853EEA14DA9@in.WebX.maYIadrTaRb...
> Randy,
> Following works for me;
>
>
> Form1(Code)
> Private Sub Command1_Click()
> Call Module1.junk
> End Sub
>
> Form2(Code)
> Private Sub Command1_Click()
> Call Module2.junk
> End Sub
>
> Module1(Code)
> Public Sub junk()
> Msgbox("From Module1")
> End Sub
>
> Module2(Code)
> Public Sub junk()
> Msgbox("From Module2")
> End Sub
>
> Cheers,
> Kenneth Hutson
> San Antonio, Texas
>
>
> "Randy Richardson" wrote in message
> news:977ED29D84CAAC8CADF7309969A60B91@in.WebX.maYIadrTaRb...
> > I have two userforms in my module. Some of the required sub procedures
> are
> > needed for both userforms. So, a procedure that I've created for
> UserForm1
> > (Code) has been duplicated and placed also in UserForm2 (Code).
However,
> > this seems wasteful, since the code is identical. Isn't there a way to
> make
> > the code accessible to both userforms without repeating it? I tried
> placing
> > the procedure in Module1 (Code), and although the program seems to know
> its
> > there (it doesn't give me an unknown procedure error) it doesn't seem to
> do
> > anything. I declared it as a public sub which, as I understand it,
should
> > make it readable to all modules. Do I need to resign myself to
> duplicating
> > the same code within the two userforms?
> >
> >
>
>
Message 4 of 4
bcoward
in reply to: Anonymous

Randy,




If your procedure is needed in multiple UserForms put the code once in a class and instatiate the class object to execute your code.




Good luck,

Bob Coward


CADS Inc




www.cadsinc.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost