|
|
| Next: Ouline Numbering duplicate names? |
| Author |
Message |
kMan External

Since: Jun 01, 2006 Posts: 9
|
Posted: Thu Mar 15, 2007 4:03 pm Post subject: Opening a form from another and accessing contents Archived from groups: microsoft>public>word>vba>userforms (more info?) |
|
|
Hello all,
I have a Userform someone's built into a word template. I'd like to be able
to launch another user form when someone clicks on a button on the first
form. Being new to word VBA not so sure how I'd do this.
for eg, in Access you could do:
docmd.openform "formB"
That doesn't work in Word. I tries formB.Show; no cigars.
Related to this in access you could open a form in Dialog mode, where code
in the first form halts until the second form is closed or hidden. Could you
do the same in Word?
Finally, how could I access a textbox in the first form when I open the
second form?
Thanks for your help. |
|
| Back to top |
|
 |
Jezebel External

Since: Feb 24, 2005 Posts: 2180
|
Posted: Thu Mar 15, 2007 9:06 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Dim pForm as formB
Set pForm = new formB
pForm.Show vbModal
MyValue = pForm.Textbox1.Text
:
unload pForm
Set pForm = nothing
You can add your own properties and methods to a form module -- a form is
just a special case of class module. In particular, you often need a
property to indicate whether the user clicked OK or cancel. Within the form
(using a construction like the above) you need to hide the form rather than
unloading it when you've finished.
"kMan" <kMan.TakeThisOut@discussions.microsoft.com> wrote in message
news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
> Hello all,
>
> I have a Userform someone's built into a word template. I'd like to be
> able
> to launch another user form when someone clicks on a button on the first
> form. Being new to word VBA not so sure how I'd do this.
> for eg, in Access you could do:
> docmd.openform "formB"
>
> That doesn't work in Word. I tries formB.Show; no cigars.
>
> Related to this in access you could open a form in Dialog mode, where code
> in the first form halts until the second form is closed or hidden. Could
> you
> do the same in Word?
>
> Finally, how could I access a textbox in the first form when I open the
> second form?
>
> Thanks for your help. |
|
| Back to top |
|
 |
Greg Maxey External

Since: Jan 08, 2006 Posts: 249
|
Posted: Thu Mar 15, 2007 9:06 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Jezebel,
In a UserForm I have:
Option Explicit
Private mCancel As Boolean
Private Sub cmdCancel_Click()
mCancel = True
Me.Hide
End Sub
Private Sub cmdOK_Click()
mCancel = False
Me.Hide
End Sub
Public Property Get Cancel() As Boolean
Cancel = mCancel
End Property
In a project module I have:
Sub MagicFormDiscussion()
Dim myFrm As oUserForm
Set myFrm = New oUserForm
myFrm.Show
If myFrm.Cancel Then
MsgBox "Form was canceled"
Else
MsgBox "Form exited normally"
End If
MsgBox "Form complete"
Unload myFrm
Set myFrm = Nothing
End Sub
This is code I put togeher a year or so ago after discussing the topic with
you and Jonathan West.
Jonathan posted some informaton on pitfalls of Public declarations statement
earlier today. The form module in the code above uses a "Public" Property
Get. It won't work if it is Private. Is this a sound method or should I
look for another way to determine if a form is canceled?
Thanks
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jezebel wrote:
> Dim pForm as formB
>
> Set pForm = new formB
> pForm.Show vbModal
>
> MyValue = pForm.Textbox1.Text
>>
>
> unload pForm
> Set pForm = nothing
>
>
> You can add your own properties and methods to a form module -- a
> form is just a special case of class module. In particular, you often
> need a property to indicate whether the user clicked OK or cancel.
> Within the form (using a construction like the above) you need to
> hide the form rather than unloading it when you've finished.
>
>
>
>
>
> "kMan" <kMan.RemoveThis@discussions.microsoft.com> wrote in message
> news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
>> Hello all,
>>
>> I have a Userform someone's built into a word template. I'd like to
>> be able
>> to launch another user form when someone clicks on a button on the
>> first form. Being new to word VBA not so sure how I'd do this.
>> for eg, in Access you could do:
>> docmd.openform "formB"
>>
>> That doesn't work in Word. I tries formB.Show; no cigars.
>>
>> Related to this in access you could open a form in Dialog mode,
>> where code in the first form halts until the second form is closed
>> or hidden. Could you
>> do the same in Word?
>>
>> Finally, how could I access a textbox in the first form when I open
>> the second form?
>>
>> Thanks for your help. |
|
| Back to top |
|
 |
kMan External

Since: Jun 01, 2006 Posts: 9
|
Posted: Thu Mar 15, 2007 9:06 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Does
pForm.Show vbModal
halt execution of
MyValue = pForm....
until pForm is hidden?
"Jezebel" wrote:
> Dim pForm as formB
>
> Set pForm = new formB
> pForm.Show vbModal
>
> MyValue = pForm.Textbox1.Text
> :
>
> unload pForm
> Set pForm = nothing
>
>
> You can add your own properties and methods to a form module -- a form is
> just a special case of class module. In particular, you often need a
> property to indicate whether the user clicked OK or cancel. Within the form
> (using a construction like the above) you need to hide the form rather than
> unloading it when you've finished.
>
>
>
>
>
> "kMan" <kMan RemoveThis @discussions.microsoft.com> wrote in message
> news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
> > Hello all,
> >
> > I have a Userform someone's built into a word template. I'd like to be
> > able
> > to launch another user form when someone clicks on a button on the first
> > form. Being new to word VBA not so sure how I'd do this.
> > for eg, in Access you could do:
> > docmd.openform "formB"
> >
> > That doesn't work in Word. I tries formB.Show; no cigars.
> >
> > Related to this in access you could open a form in Dialog mode, where code
> > in the first form halts until the second form is closed or hidden. Could
> > you
> > do the same in Word?
> >
> > Finally, how could I access a textbox in the first form when I open the
> > second form?
> >
> > Thanks for your help.
>
>
> |
|
| Back to top |
|
 |
Jezebel External

Since: Feb 24, 2005 Posts: 2180
|
Posted: Thu Mar 15, 2007 11:04 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Yes, that's exactly the way to do it. I haven't seen Jonathon's post, but
presumably he was referring to the danger of using declarations like
Public XYZ as whatever
because other modules can change the value any way they like. But the code
you have creates Cancel as a read-only property, which -- in this case,
anyway -- is what you want. For completeness, you might want to set mCancel
= FALSE in the form's Activate event, in case the form gets shown again,
rather than unloaded. (A principle of object-oriented coding is that an
object is not responsible for its own destruction.)
"Greg Maxey" <gmaxey.RemoveThis@mvps.oSCARrOMEOgOLF> wrote in message
news:Opp$jQ2ZHHA.4420@TK2MSFTNGP02.phx.gbl...
> Jezebel,
>
> In a UserForm I have:
>
> Option Explicit
> Private mCancel As Boolean
> Private Sub cmdCancel_Click()
> mCancel = True
> Me.Hide
> End Sub
> Private Sub cmdOK_Click()
> mCancel = False
> Me.Hide
> End Sub
> Public Property Get Cancel() As Boolean
> Cancel = mCancel
> End Property
>
> In a project module I have:
>
> Sub MagicFormDiscussion()
> Dim myFrm As oUserForm
> Set myFrm = New oUserForm
> myFrm.Show
> If myFrm.Cancel Then
> MsgBox "Form was canceled"
> Else
> MsgBox "Form exited normally"
> End If
> MsgBox "Form complete"
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> This is code I put togeher a year or so ago after discussing the topic
> with you and Jonathan West.
>
> Jonathan posted some informaton on pitfalls of Public declarations
> statement earlier today. The form module in the code above uses a
> "Public" Property Get. It won't work if it is Private. Is this a sound
> method or should I look for another way to determine if a form is
> canceled?
>
> Thanks
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
>
> Jezebel wrote:
>> Dim pForm as formB
>>
>> Set pForm = new formB
>> pForm.Show vbModal
>>
>> MyValue = pForm.Textbox1.Text
>>>
>>
>> unload pForm
>> Set pForm = nothing
>>
>>
>> You can add your own properties and methods to a form module -- a
>> form is just a special case of class module. In particular, you often
>> need a property to indicate whether the user clicked OK or cancel.
>> Within the form (using a construction like the above) you need to
>> hide the form rather than unloading it when you've finished.
>>
>>
>>
>>
>>
>> "kMan" <kMan.RemoveThis@discussions.microsoft.com> wrote in message
>> news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
>>> Hello all,
>>>
>>> I have a Userform someone's built into a word template. I'd like to
>>> be able
>>> to launch another user form when someone clicks on a button on the
>>> first form. Being new to word VBA not so sure how I'd do this.
>>> for eg, in Access you could do:
>>> docmd.openform "formB"
>>>
>>> That doesn't work in Word. I tries formB.Show; no cigars.
>>>
>>> Related to this in access you could open a form in Dialog mode,
>>> where code in the first form halts until the second form is closed
>>> or hidden. Could you
>>> do the same in Word?
>>>
>>> Finally, how could I access a textbox in the first form when I open
>>> the second form?
>>>
>>> Thanks for your help.
>
> |
|
| Back to top |
|
 |
Greg Maxey External

Since: Jan 08, 2006 Posts: 249
|
Posted: Thu Mar 15, 2007 11:04 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Ok thanks.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jezebel wrote:
> Yes, that's exactly the way to do it. I haven't seen Jonathon's post,
> but presumably he was referring to the danger of using declarations
> like
> Public XYZ as whatever
>
> because other modules can change the value any way they like. But the
> code you have creates Cancel as a read-only property, which -- in
> this case, anyway -- is what you want. For completeness, you might
> want to set mCancel = FALSE in the form's Activate event, in case the
> form gets shown again, rather than unloaded. (A principle of
> object-oriented coding is that an object is not responsible for its
> own destruction.)
>
>
> "Greg Maxey" <gmaxey.RemoveThis@mvps.oSCARrOMEOgOLF> wrote in message
> news:Opp$jQ2ZHHA.4420@TK2MSFTNGP02.phx.gbl...
>> Jezebel,
>>
>> In a UserForm I have:
>>
>> Option Explicit
>> Private mCancel As Boolean
>> Private Sub cmdCancel_Click()
>> mCancel = True
>> Me.Hide
>> End Sub
>> Private Sub cmdOK_Click()
>> mCancel = False
>> Me.Hide
>> End Sub
>> Public Property Get Cancel() As Boolean
>> Cancel = mCancel
>> End Property
>>
>> In a project module I have:
>>
>> Sub MagicFormDiscussion()
>> Dim myFrm As oUserForm
>> Set myFrm = New oUserForm
>> myFrm.Show
>> If myFrm.Cancel Then
>> MsgBox "Form was canceled"
>> Else
>> MsgBox "Form exited normally"
>> End If
>> MsgBox "Form complete"
>> Unload myFrm
>> Set myFrm = Nothing
>> End Sub
>>
>> This is code I put togeher a year or so ago after discussing the
>> topic with you and Jonathan West.
>>
>> Jonathan posted some informaton on pitfalls of Public declarations
>> statement earlier today. The form module in the code above uses a
>> "Public" Property Get. It won't work if it is Private. Is this a
>> sound method or should I look for another way to determine if a form
>> is canceled?
>>
>> Thanks
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
>>
>>
>> Jezebel wrote:
>>> Dim pForm as formB
>>>
>>> Set pForm = new formB
>>> pForm.Show vbModal
>>>
>>> MyValue = pForm.Textbox1.Text
>>>>
>>>
>>> unload pForm
>>> Set pForm = nothing
>>>
>>>
>>> You can add your own properties and methods to a form module -- a
>>> form is just a special case of class module. In particular, you
>>> often need a property to indicate whether the user clicked OK or
>>> cancel. Within the form (using a construction like the above) you
>>> need to hide the form rather than unloading it when you've finished.
>>>
>>>
>>>
>>>
>>>
>>> "kMan" <kMan.RemoveThis@discussions.microsoft.com> wrote in message
>>> news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
>>>> Hello all,
>>>>
>>>> I have a Userform someone's built into a word template. I'd like to
>>>> be able
>>>> to launch another user form when someone clicks on a button on the
>>>> first form. Being new to word VBA not so sure how I'd do this.
>>>> for eg, in Access you could do:
>>>> docmd.openform "formB"
>>>>
>>>> That doesn't work in Word. I tries formB.Show; no cigars.
>>>>
>>>> Related to this in access you could open a form in Dialog mode,
>>>> where code in the first form halts until the second form is closed
>>>> or hidden. Could you
>>>> do the same in Word?
>>>>
>>>> Finally, how could I access a textbox in the first form when I open
>>>> the second form?
>>>>
>>>> Thanks for your help. |
|
| Back to top |
|
 |
Jezebel External

Since: Feb 24, 2005 Posts: 2180
|
Posted: Thu Mar 15, 2007 11:04 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
I should have suggested that you read help on 'vbModal'. If you show the
form with Modal = true, execution in the calling function stops and waits
while the form is active (ie until it is hidden or unloaded); if modal is
false, execution continues immediately. Which you obviously don't want in
your case, because you'd be trying to read the form properties before the
user has had a chance to set them.
"kMan" <kMan RemoveThis @discussions.microsoft.com> wrote in message
news:987F481C-293D-4245-AB9D-CC27B190BE26@microsoft.com...
> Does
> pForm.Show vbModal
>
> halt execution of
> MyValue = pForm....
>
> until pForm is hidden?
>
>
>
> "Jezebel" wrote:
>
>> Dim pForm as formB
>>
>> Set pForm = new formB
>> pForm.Show vbModal
>>
>> MyValue = pForm.Textbox1.Text
>> :
>>
>> unload pForm
>> Set pForm = nothing
>>
>>
>> You can add your own properties and methods to a form module -- a form is
>> just a special case of class module. In particular, you often need a
>> property to indicate whether the user clicked OK or cancel. Within the
>> form
>> (using a construction like the above) you need to hide the form rather
>> than
>> unloading it when you've finished.
>>
>>
>>
>>
>>
>> "kMan" <kMan RemoveThis @discussions.microsoft.com> wrote in message
>> news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
>> > Hello all,
>> >
>> > I have a Userform someone's built into a word template. I'd like to be
>> > able
>> > to launch another user form when someone clicks on a button on the
>> > first
>> > form. Being new to word VBA not so sure how I'd do this.
>> > for eg, in Access you could do:
>> > docmd.openform "formB"
>> >
>> > That doesn't work in Word. I tries formB.Show; no cigars.
>> >
>> > Related to this in access you could open a form in Dialog mode, where
>> > code
>> > in the first form halts until the second form is closed or hidden.
>> > Could
>> > you
>> > do the same in Word?
>> >
>> > Finally, how could I access a textbox in the first form when I open the
>> > second form?
>> >
>> > Thanks for your help.
>>
>>
>> |
|
| Back to top |
|
 |
kMan External

Since: Jun 01, 2006 Posts: 9
|
Posted: Thu Mar 15, 2007 11:04 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
thanks a million...
I'll try it tonight.
"Jezebel" wrote:
> I should have suggested that you read help on 'vbModal'. If you show the
> form with Modal = true, execution in the calling function stops and waits
> while the form is active (ie until it is hidden or unloaded); if modal is
> false, execution continues immediately. Which you obviously don't want in
> your case, because you'd be trying to read the form properties before the
> user has had a chance to set them.
>
>
>
>
>
> "kMan" <kMan.TakeThisOut@discussions.microsoft.com> wrote in message
> news:987F481C-293D-4245-AB9D-CC27B190BE26@microsoft.com...
> > Does
> > pForm.Show vbModal
> >
> > halt execution of
> > MyValue = pForm....
> >
> > until pForm is hidden?
> >
> >
> >
> > "Jezebel" wrote:
> >
> >> Dim pForm as formB
> >>
> >> Set pForm = new formB
> >> pForm.Show vbModal
> >>
> >> MyValue = pForm.Textbox1.Text
> >> :
> >>
> >> unload pForm
> >> Set pForm = nothing
> >>
> >>
> >> You can add your own properties and methods to a form module -- a form is
> >> just a special case of class module. In particular, you often need a
> >> property to indicate whether the user clicked OK or cancel. Within the
> >> form
> >> (using a construction like the above) you need to hide the form rather
> >> than
> >> unloading it when you've finished.
> >>
> >>
> >>
> >>
> >>
> >> "kMan" <kMan.TakeThisOut@discussions.microsoft.com> wrote in message
> >> news:D9F05EF3-CE57-4166-9E9F-3D404B447D14@microsoft.com...
> >> > Hello all,
> >> >
> >> > I have a Userform someone's built into a word template. I'd like to be
> >> > able
> >> > to launch another user form when someone clicks on a button on the
> >> > first
> >> > form. Being new to word VBA not so sure how I'd do this.
> >> > for eg, in Access you could do:
> >> > docmd.openform "formB"
> >> >
> >> > That doesn't work in Word. I tries formB.Show; no cigars.
> >> >
> >> > Related to this in access you could open a form in Dialog mode, where
> >> > code
> >> > in the first form halts until the second form is closed or hidden.
> >> > Could
> >> > you
> >> > do the same in Word?
> >> >
> >> > Finally, how could I access a textbox in the first form when I open the
> >> > second form?
> >> >
> >> > Thanks for your help.
> >>
> >>
> >>
>
>
> |
|
| Back to top |
|
 |
Jonathan West External

Since: Dec 18, 2003 Posts: 112
|
Posted: Fri Mar 16, 2007 9:06 am Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
"Greg Maxey" <gmaxey.TakeThisOut@mvps.oSCARrOMEOgOLF> wrote in message
news:Opp$jQ2ZHHA.4420@TK2MSFTNGP02.phx.gbl...
> Jezebel,
>
> In a UserForm I have:
>
> Option Explicit
> Private mCancel As Boolean
> Private Sub cmdCancel_Click()
> mCancel = True
> Me.Hide
> End Sub
> Private Sub cmdOK_Click()
> mCancel = False
> Me.Hide
> End Sub
> Public Property Get Cancel() As Boolean
> Cancel = mCancel
> End Property
>
> In a project module I have:
>
> Sub MagicFormDiscussion()
> Dim myFrm As oUserForm
> Set myFrm = New oUserForm
> myFrm.Show
> If myFrm.Cancel Then
> MsgBox "Form was canceled"
> Else
> MsgBox "Form exited normally"
> End If
> MsgBox "Form complete"
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> This is code I put togeher a year or so ago after discussing the topic
> with you and Jonathan West.
>
> Jonathan posted some informaton on pitfalls of Public declarations
> statement earlier today. The form module in the code above uses a
> "Public" Property Get. It won't work if it is Private. Is this a sound
> method or should I look for another way to determine if a form is
> canceled?
>
Public properties in UserForms or Class modules are fine. They (along with
public methods) are the means by which the form or class communicated with
the rest of your project.
If you declare a variable with the Public keyword on a UserForm or Class, it
acts as a read-write public property of that form or class. If you later
decide that you want to add some processing when the variable is set or read
from outside, it is easy enough to convert it into a Property Let/Get pair,
and the interface of the form or class to the outside world appears entirely
unchanged.
There are those who argue that it is better to start with a Property Let/Get
pair from the outset. I'm inclined that way in my own coding style, but I
accept that it is a matter of style, and need for this is dependent on the
complexity of the project you work on.
I was discussing in the General group yesterday the pitfalls of putting a
Public variable into an ordinary Module. They behave rather differently from
Public variables in classes or forms.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup |
|
| Back to top |
|
 |
Jean-Guy Marcil External

Since: Nov 30, 2006 Posts: 68
|
Posted: Fri Mar 16, 2007 10:22 am Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Greg Maxey was telling us:
Greg Maxey nous racontait que :
> Jezebel,
>
> In a UserForm I have:
>
> Option Explicit
> Private mCancel As Boolean
> Private Sub cmdCancel_Click()
> mCancel = True
> Me.Hide
> End Sub
> Private Sub cmdOK_Click()
> mCancel = False
> Me.Hide
> End Sub
> Public Property Get Cancel() As Boolean
> Cancel = mCancel
> End Property
>
> In a project module I have:
>
> Sub MagicFormDiscussion()
> Dim myFrm As oUserForm
> Set myFrm = New oUserForm
> myFrm.Show
> If myFrm.Cancel Then
> MsgBox "Form was canceled"
> Else
> MsgBox "Form exited normally"
> End If
> MsgBox "Form complete"
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> This is code I put togeher a year or so ago after discussing the
> topic with you and Jonathan West.
>
> Jonathan posted some informaton on pitfalls of Public declarations
> statement earlier today. The form module in the code above uses a
> "Public" Property Get. It won't work if it is Private. Is this a
> sound method or should I look for another way to determine if a form
> is canceled?
I understand the principle of the Get/Let pair for creating read-only or
Read/Write properties.
But, in such simple cases (i.e. Was the form's Cancel button used or not?),
is it "dangerous" to use simply:
Option Explicit
Public mCancel As Boolean
Private Sub cmdCancel_Click()
mCancel = True
Me.Hide
End Sub
Private Sub cmdOK_Click()
mCancel = False
Me.Hide
End Sub
Sub MagicFormDiscussion()
Dim myFrm As oUserForm
Set myFrm = New oUserForm
myFrm.Show
If myFrm.mCancel Then
MsgBox "Form was canceled"
Else
MsgBox "Form exited normally"
End If
MsgBox "Form complete"
Unload myFrm
Set myFrm = Nothing
End Sub
The calling Sub will not change the value of mCancel (although it could)
just because it makes no sense to do that.
Or, for the sake of completeness and total security, is it recommended to
always use a Get statement instead?
I mean, I know we could write something like:
Sub MagicFormDiscussion()
Dim myFrm As oUserForm
Set myFrm = New oUserForm
myFrm.Show
myFrm.mCancel = True
If myFrm.mCancel Then
MsgBox "Form was canceled"
Else
MsgBox "Form exited normally"
End If
MsgBox "Form complete"
Unload myFrm
Set myFrm = Nothing
End Sub
But why would we?
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE.RemoveThis@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org |
|
| Back to top |
|
 |
Jezebel External

Since: Feb 24, 2005 Posts: 2180
|
Posted: Fri Mar 16, 2007 3:08 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
In this case it's not very dangerous, no. Obviously in practice there's no
harm in using a read/write property but only reading it; provided this is a
discipline you can maintain across your project (which you usually can if
it's reasonably small and you're the only person who touches the code). But
it's a poor practice none the less. If you're writing code that has to stay
in use for a while, or that will get maintained by others, the total effort
expended over the life of the project is minimised if you follow good coding
standards.
One part of which is to keep everything as modular as possible and to write
'defensive' code, that precludes the possibility of the error rather than
relying on memory and discipline to prevent it.
"Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote in message
news:%23pOTca9ZHHA.808@TK2MSFTNGP04.phx.gbl...
> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
>
>> Jezebel,
>>
>> In a UserForm I have:
>>
>> Option Explicit
>> Private mCancel As Boolean
>> Private Sub cmdCancel_Click()
>> mCancel = True
>> Me.Hide
>> End Sub
>> Private Sub cmdOK_Click()
>> mCancel = False
>> Me.Hide
>> End Sub
>> Public Property Get Cancel() As Boolean
>> Cancel = mCancel
>> End Property
>>
>> In a project module I have:
>>
>> Sub MagicFormDiscussion()
>> Dim myFrm As oUserForm
>> Set myFrm = New oUserForm
>> myFrm.Show
>> If myFrm.Cancel Then
>> MsgBox "Form was canceled"
>> Else
>> MsgBox "Form exited normally"
>> End If
>> MsgBox "Form complete"
>> Unload myFrm
>> Set myFrm = Nothing
>> End Sub
>>
>> This is code I put togeher a year or so ago after discussing the
>> topic with you and Jonathan West.
>>
>> Jonathan posted some informaton on pitfalls of Public declarations
>> statement earlier today. The form module in the code above uses a
>> "Public" Property Get. It won't work if it is Private. Is this a
>> sound method or should I look for another way to determine if a form
>> is canceled?
>
> I understand the principle of the Get/Let pair for creating read-only or
> Read/Write properties.
>
> But, in such simple cases (i.e. Was the form's Cancel button used or
> not?), is it "dangerous" to use simply:
>
> Option Explicit
> Public mCancel As Boolean
> Private Sub cmdCancel_Click()
> mCancel = True
> Me.Hide
> End Sub
> Private Sub cmdOK_Click()
> mCancel = False
> Me.Hide
> End Sub
>
> Sub MagicFormDiscussion()
> Dim myFrm As oUserForm
> Set myFrm = New oUserForm
> myFrm.Show
> If myFrm.mCancel Then
> MsgBox "Form was canceled"
> Else
> MsgBox "Form exited normally"
> End If
> MsgBox "Form complete"
> Unload myFrm
> Set myFrm = Nothing
> End Sub
>
> The calling Sub will not change the value of mCancel (although it could)
> just because it makes no sense to do that.
> Or, for the sake of completeness and total security, is it recommended to
> always use a Get statement instead?
> I mean, I know we could write something like:
>
> Sub MagicFormDiscussion()
> Dim myFrm As oUserForm
> Set myFrm = New oUserForm
> myFrm.Show
>
> myFrm.mCancel = True
>
> If myFrm.mCancel Then
> MsgBox "Form was canceled"
> Else
> MsgBox "Form exited normally"
> End If
> MsgBox "Form complete"
> Unload myFrm
> Set myFrm = Nothing
>
> End Sub
>
> But why would we?
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREMOVE RemoveThis @CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
> |
|
| Back to top |
|
 |
Jean-Guy Marcil External

Since: Nov 30, 2006 Posts: 68
|
Posted: Fri Mar 16, 2007 3:09 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Jezebel was telling us:
Jezebel nous racontait que :
> One part of which is to keep everything as modular as possible and to
> write 'defensive' code, that precludes the possibility of the error
> rather than relying on memory and discipline to prevent it.
Very true, especially if the project is later handled by some else.
Thanks for your thoughts.
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE RemoveThis @CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org |
|
| Back to top |
|
 |
Greg Maxey External

Since: Jan 08, 2006 Posts: 249
|
Posted: Fri Mar 16, 2007 4:57 pm Post subject: Re: Opening a form from another and accessing contents [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Thanks Jonathan. Sometimes I get it, sometimes I don't. As the mere
mention of Class, Get, and Let can usually bring on a outbreak of hives,
this time I am not sure .
I'll keep plugging away and one day the "blink" of complete comprehension
will finally occur.
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jonathan West wrote:
> "Greg Maxey" <gmaxey RemoveThis @mvps.oSCARrOMEOgOLF> wrote in message
> news:Opp$jQ2ZHHA.4420@TK2MSFTNGP02.phx.gbl...
>> Jezebel,
>>
>> In a UserForm I have:
>>
>> Option Explicit
>> Private mCancel As Boolean
>> Private Sub cmdCancel_Click()
>> mCancel = True
>> Me.Hide
>> End Sub
>> Private Sub cmdOK_Click()
>> mCancel = False
>> Me.Hide
>> End Sub
>> Public Property Get Cancel() As Boolean
>> Cancel = mCancel
>> End Property
>>
>> In a project module I have:
>>
>> Sub MagicFormDiscussion()
>> Dim myFrm As oUserForm
>> Set myFrm = New oUserForm
>> myFrm.Show
>> If myFrm.Cancel Then
>> MsgBox "Form was canceled"
>> Else
>> MsgBox "Form exited normally"
>> End If
>> MsgBox "Form complete"
>> Unload myFrm
>> Set myFrm = Nothing
>> End Sub
>>
>> This is code I put togeher a year or so ago after discussing the
>> topic with you and Jonathan West.
>>
>> Jonathan posted some informaton on pitfalls of Public declarations
>> statement earlier today. The form module in the code above uses a
>> "Public" Property Get. It won't work if it is Private. Is this a
>> sound method or should I look for another way to determine if a form
>> is canceled?
>>
>
> Public properties in UserForms or Class modules are fine. They (along
> with public methods) are the means by which the form or class
> communicated with the rest of your project.
>
> If you declare a variable with the Public keyword on a UserForm or
> Class, it acts as a read-write public property of that form or class.
> If you later decide that you want to add some processing when the
> variable is set or read from outside, it is easy enough to convert it
> into a Property Let/Get pair, and the interface of the form or class
> to the outside world appears entirely unchanged.
>
> There are those who argue that it is better to start with a Property
> Let/Get pair from the outset. I'm inclined that way in my own coding
> style, but I accept that it is a matter of style, and need for this
> is dependent on the complexity of the project you work on.
>
> I was discussing in the General group yesterday the pitfalls of
> putting a Public variable into an ordinary Module. They behave rather
> differently from Public variables in classes or forms. |
|
| Back to top |
|
 |
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|