Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2007

Tip: Looking for answers? Try searching our database.

Opening a form from another and accessing contents

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kMan - 16 Mar 2007 00:03 GMT
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.
Jezebel - 16 Mar 2007 01:22 GMT
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.

> Hello all,
>
[quoted text clipped - 16 lines]
>
> Thanks for your help.
Greg Maxey - 16 Mar 2007 01:43 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Dim pForm as formB
>
[quoted text clipped - 32 lines]
>>
>> Thanks for your help.
Jezebel - 16 Mar 2007 03:06 GMT
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.)

> Jezebel,
>
[quoted text clipped - 77 lines]
>>>
>>> Thanks for your help.
Greg Maxey - 16 Mar 2007 03:30 GMT
Ok thanks.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> 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
[quoted text clipped - 96 lines]
>>>>
>>>> Thanks for your help.
Jonathan West - 16 Mar 2007 13:17 GMT
> Jezebel,
>
[quoted text clipped - 38 lines]
> 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.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

Greg Maxey - 16 Mar 2007 21:57 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>> Jezebel,
>>
[quoted text clipped - 58 lines]
> putting a Public variable into an ordinary Module. They behave rather
> differently from Public variables in classes or forms.
Jean-Guy Marcil - 16 Mar 2007 15:22 GMT
Greg Maxey was telling us:
Greg Maxey nous racontait que :

> Jezebel,
>
[quoted text clipped - 38 lines]
> 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?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Jezebel - 16 Mar 2007 19:06 GMT
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.

> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
[quoted text clipped - 98 lines]
>
> But why would we?
Jean-Guy Marcil - 16 Mar 2007 19:46 GMT
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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

kMan - 16 Mar 2007 02:31 GMT
Does
pForm.Show vbModal

halt execution of
MyValue = pForm....

until pForm is hidden?

> Dim pForm as formB
>
[quoted text clipped - 32 lines]
> >
> > Thanks for your help.
Jezebel - 16 Mar 2007 03:08 GMT
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.

> Does
> pForm.Show vbModal
[quoted text clipped - 45 lines]
>> >
>> > Thanks for your help.
kMan - 16 Mar 2007 03:16 GMT
thanks a million...

I'll try it tonight.

> 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
[quoted text clipped - 52 lines]
> >> >
> >> > Thanks for your help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.