where to put userform event procedures

Ö

Örjan Skoglösa

I have an userform with some command buttons.

Where should I put the procedures triggered by the buttons?

I have tried both to insert them directly under the event procedure in
the userform code but also to call a procedure in a attached macro
module. I can´t figure out any difference.

Are there any rules, principles or conventions regarding this?

TIA

Örjan Skoglösa
 
G

George Seese

Try this, to get you started:
New document.
Insert a userform, which is named UserForm1 by default.
On the userform, insert a CommandButton.
Right click the Command Button on the userform.
Select View Code.
VBA displays the screen where you enter code for this userform.
On first time, it inserts starter code for the Click event (default event
for this control):
---
Private Sub CommandButton1_Click()
(blank line)
End Sub
---
You enter your code starting at the blank line.
For example, you may want to display another userform:
Userform2.show
or whatever.

George
 
Ö

Örjan Skoglösa

Thank you George.

I try to clarify what I wanted to ask (english is not my favourite
language ;-)

The event procedures for my controls do work. No problem.

I just wondered where to put those procedures. As far as I can
realize, they work both ways, no matter if I directly put them in the
userform code or if I only call them from there.

Is there any reason why I should avoid to put all my procedures, even
longish ones, inside the userform module? Will things like flow
control, error handling routines, hiding and showing the form, opening
Msgboxes, manipulating the document, etc. also work well then?

TIA
Örjan Skoglösa
 
C

Cindy Meister -WordMVP-

Hi Örjan,
I just wondered where to put those procedures. As far as I can
realize, they work both ways, no matter if I directly put them in the
userform code or if I only call them from there.

Is there any reason why I should avoid to put all my procedures, even
longish ones, inside the userform module?
regular module vs. userform module

A userform module is actually a class module. This means things aren't
as "publicly" available to other VBA things.

Code you may want to re-use or share with other procedures is therefore
better off in a regular module.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
Ö

Örjan Skoglösa

I understand that there are instances when code has to be put in a
regular module, e.g. when code is called for from an opening document
or code that has to available to other procedures (modules?).

On the other hand I could avoid coding those prefix containers and
have almost all code in one place if I keep it in the userform module.

Most important seems to be that there is no code or procedure that
does not function from inside an userform module.

Thanks George, Jeffrey and Cindy.

Örjan
 

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

Top