|
|
|
date: 12 May 2006 06:30:47 -0700,
group: microsoft.public.word.vba.userforms
back
Re: Standard Conventions for UserForms
I tend to look for using as little code as possible. On that basis, I use
the following method
Project code:
Sub CallUF()
Dim myFrm As UserForm1
Set myFrm = New UserForm1
myFrm.Show
End Sub
UserForm code:
Private Sub CmdBtnOK_Click()
ActiveDocument.Bookmarks("bkName").Range.Text = Me.txtName
Unload Me
End Sub
However, if there is more processing code, then you might not want to
include it all in the CmdBtnOK_Click event. If some or all of the code might
need to be called from other occasions, then you need to get that code out
to separate routines.
I normally use Unload me rather than Me.Hide in an event routine to close
the form, so that when you move on to the next line of the calling routine
the form has not only been hidden but also unloaded. That way, you don't
need explicit code in the calling routine to unload the form and set the
object variable to Nothing.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
"Greg Maxey" wrote in message
news:1147440647.497512.220470@j73g2000cwa.googlegroups.com...
> Yesterday I was in a discussion (buried deep in another string) with
> Jean-Guy Marcil about the proper conventions for developing userform
> code. In particular, where the bulk of the code should reside. Here
> is a very simple example where a userform provides one text input to
> supply a bookmark in the document. I am using this to illustrate at
> least four different approaches for processing the form. Jean-Guy
> advocates method 1. I personally like the concept of method 3, but it
> carries the baggage (at least I think it is baggage from my limited
> understanding of the topic) of having to use public declarations (or
> worse Get and Let property statements which brings on nightmares).
>
> Was hoping to expand the discussion and get opinions from other on best
> methods/practices.
>
> Thanks.
>
> 1. Process form in the calling macro:
>
> Project code:
>
> Sub CallUF()
> Dim myFrm As UserForm1
> Set myFrm = New UserForm1
> myFrm.Show
> ActiveDocument.Bookmarks("bkName").Range.Text = myFrm.txtName
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> UserForm code:
>
> Private Sub CmdBtnOK_Click()
> Me.Hide
> End Sub
>
> 2. Process form with a control in the form (e.g., a command button
> event)
>
> Project code:
>
> Sub CallUF()
> Dim myFrm As UserForm1
> Set myFrm = New UserForm1
> myFrm.Show
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> UserForm Code
>
> Private Sub CmdBtnOK_Click()
> Me.Hide
> ActiveDocument.Bookmarks("bkName").Range.Text = Me.txtName
> End Sub
>
> 3. Process form using a separate routine called from the calling
> macro. Note for simplicity I just made a public declaration of the
> variable pName in the project module:
>
> Project code:
>
> Option Explicit
> Public pName As String
> Sub CallUF()
> Dim myFrm As UserForm1
> Set myFrm = New UserForm1
> myFrm.Show
> ProcessForm
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> Sub ProcessForm()
> ActiveDocument.Bookmarks("bkName").Range.Text = pName
> End Sub
>
> UserForm code:
>
> Private Sub CmdBtnOK_Click()
> Me.Hide
> pName = Me.txtName
> End Sub
>
> 4. Process form using a separate routine contained in the user form
>
> Project code:
>
> Sub CallUF()
> Dim myFrm As UserForm1
> Set myFrm = New UserForm1
> myFrm.Show
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> UserForm code:
>
> Private Sub CmdBtnOK_Click()
> Me.Hide
> ProcessForm
> End Sub
>
> Sub ProcessForm()
> ActiveDocument.Bookmarks("bkName").Range.Text = Me.txtName
> End Sub
>
date: Fri, 12 May 2006 14:51:53 +0100
author: Jonathan West
Re: Standard Conventions for UserForms
"Greg Maxey" wrote in message
news:1147443387.588370.241750@i39g2000cwa.googlegroups.com...
> Thanks Jonathan,
>
> I have read lots of discussions where you support economy in code ;-)
>
> <If some or all of the code might need to be called from other
> occasions, then you need to get that code out to separate routines.
>
> Where would you put this code?
>
It depends.
If the code will only need to be called by other events within the same
form, you might as well keep it in the form. That way, you can keep the form
as self-contained as possible if you decide to transfer it to another
template. In addition, if you use the Me keyword in the code to refer to the
current instance of the form, then the code won't need to be modified if you
bring it out to a different routine.
If the code may need to be called from other forms or other macros, then you
would probably want to put it into a separate module. If the code refers to
controls in the form, you will need to pass the instance of the form as a
parameter to the routine, because the Me keyword won't work in code in
routines in ordinary modules, even if called from a form or class module.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
date: Fri, 12 May 2006 15:52:21 +0100
author: Jonathan West
Re: Standard Conventions for UserForms
"Greg Maxey" wrote in message
news:1147458607.804170.129110@y43g2000cwc.googlegroups.com...
> Ok, I see sort of.
>
> So by using:
>
> Sub ProcessForm(oForm as UserForm1)
>
> I could then process one or a hundred control values in a procedure
> outside the UserForm. This would be sort of like my original method 3,
> but I wouldn't need to declare Public variables to pass the data from
> the form back to the module. Personally I like the code separated out
> this way.
I would prefer to avoid using Public variables where there is no particular
need. Therefore, to my mind, this option is better than your option 3.
However, I see no great benefit in putting the routine in a separate module
if it will only ever be called for a single Userform. In addition to
minimising the amount of code, I like to simplify things as much as possible
by keeping closely related code together in a single module. Therefore,
unless the external routine is going to be called from multiple forms (hence
passing the userform As Object in my example), your option 4 is preferable
in my view.
In essence, Option 1 or Option 4 are both perfectly fine for code that is
only used by a single UserForm, and the choice between them is largely one
of convenience - with more processing you may want to split the code up into
separate routines.
>
> However, it sounds like using a method like this would make me odd man
> out wrt coding conventions.
I've seen *much* worse! Including from Microsoft. If you want to see a truly
dire example of spaghetti code, open up the VBA project for the Batch
Conversion Wizard that is included within Microsoft Office.
Ultimately, the main thing is to remember that source code serves two quite
separate purposes.
1. It is a set of instruction to be compiled and executed by the computer.
2. It is a message to yourself (or to another programmer who views your
code) about what you are trying to achieve, so that code can more easily be
debugged, maintained and extended in future.
All coding conventions have this second point as their ultimate aim.
Provided that you have a coding convention that works for you, you needn't
worry much if your code doesn't conform to other people's conventions. Just
concentrate on making it clear by ensuring that whatever convention you
choose, you apply it as consistently as reasonably possible.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
date: Sat, 13 May 2006 18:59:15 +0100
author: Jonathan West
Re: Standard Conventions for UserForms
Thanks for the detailed reply and explanations.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jonathan West wrote:
> "Greg Maxey" wrote in message
> news:1147458607.804170.129110@y43g2000cwc.googlegroups.com...
>> Ok, I see sort of.
>>
>> So by using:
>>
>> Sub ProcessForm(oForm as UserForm1)
>>
>> I could then process one or a hundred control values in a procedure
>> outside the UserForm. This would be sort of like my original method
>> 3, but I wouldn't need to declare Public variables to pass the data
>> from the form back to the module. Personally I like the code
>> separated out this way.
>
> I would prefer to avoid using Public variables where there is no
> particular need. Therefore, to my mind, this option is better than
> your option 3. However, I see no great benefit in putting the routine
> in a separate module if it will only ever be called for a single
> Userform. In addition to minimising the amount of code, I like to
> simplify things as much as possible by keeping closely related code
> together in a single module. Therefore, unless the external routine
> is going to be called from multiple forms (hence passing the userform
> As Object in my example), your option 4 is preferable in my view.
>
> In essence, Option 1 or Option 4 are both perfectly fine for code
> that is only used by a single UserForm, and the choice between them
> is largely one of convenience - with more processing you may want to
> split the code up into separate routines.
>
>>
>> However, it sounds like using a method like this would make me odd
>> man out wrt coding conventions.
>
> I've seen *much* worse! Including from Microsoft. If you want to see
> a truly dire example of spaghetti code, open up the VBA project for
> the Batch Conversion Wizard that is included within Microsoft Office.
>
> Ultimately, the main thing is to remember that source code serves two
> quite separate purposes.
>
> 1. It is a set of instruction to be compiled and executed by the
> computer.
> 2. It is a message to yourself (or to another programmer who views
> your code) about what you are trying to achieve, so that code can
> more easily be debugged, maintained and extended in future.
>
> All coding conventions have this second point as their ultimate aim.
> Provided that you have a coding convention that works for you, you
> needn't worry much if your code doesn't conform to other people's
> conventions. Just concentrate on making it clear by ensuring that
> whatever convention you choose, you apply it as consistently as
> reasonably possible.
date: Sat, 13 May 2006 16:07:50 -0400
author: Greg Maxey RrOMEOgOLF
Re: Standard Conventions for UserForms
Jonathan,
One more thing.
<I normally use Unload me rather than Me.Hide in an event routine to close
<the form, so that when you move on to the next line of the calling routine
<the form has not only been hidden but also unloaded. That way, you don't
<need explicit code in the calling routine to unload the form and set the
<object variable to Nothing.
So if Unload myFrm *is* used in the calling routine vice Unload Me in the
userform as you propose, is Set myFrm = Nothing then redundant in the
calling routine?
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jonathan West wrote:
> "Greg Maxey" wrote in message
> news:1147458607.804170.129110@y43g2000cwc.googlegroups.com...
>> Ok, I see sort of.
>>
>> So by using:
>>
>> Sub ProcessForm(oForm as UserForm1)
>>
>> I could then process one or a hundred control values in a procedure
>> outside the UserForm. This would be sort of like my original method
>> 3, but I wouldn't need to declare Public variables to pass the data
>> from the form back to the module. Personally I like the code
>> separated out this way.
>
> I would prefer to avoid using Public variables where there is no
> particular need. Therefore, to my mind, this option is better than
> your option 3. However, I see no great benefit in putting the routine
> in a separate module if it will only ever be called for a single
> Userform. In addition to minimising the amount of code, I like to
> simplify things as much as possible by keeping closely related code
> together in a single module. Therefore, unless the external routine
> is going to be called from multiple forms (hence passing the userform
> As Object in my example), your option 4 is preferable in my view.
>
> In essence, Option 1 or Option 4 are both perfectly fine for code
> that is only used by a single UserForm, and the choice between them
> is largely one of convenience - with more processing you may want to
> split the code up into separate routines.
>
>>
>> However, it sounds like using a method like this would make me odd
>> man out wrt coding conventions.
>
> I've seen *much* worse! Including from Microsoft. If you want to see
> a truly dire example of spaghetti code, open up the VBA project for
> the Batch Conversion Wizard that is included within Microsoft Office.
>
> Ultimately, the main thing is to remember that source code serves two
> quite separate purposes.
>
> 1. It is a set of instruction to be compiled and executed by the
> computer.
> 2. It is a message to yourself (or to another programmer who views
> your code) about what you are trying to achieve, so that code can
> more easily be debugged, maintained and extended in future.
>
> All coding conventions have this second point as their ultimate aim.
> Provided that you have a coding convention that works for you, you
> needn't worry much if your code doesn't conform to other people's
> conventions. Just concentrate on making it clear by ensuring that
> whatever convention you choose, you apply it as consistently as
> reasonably possible.
date: Sat, 13 May 2006 17:38:51 -0400
author: Greg Maxey RrOMEOgOLF
|
|