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 / October 2006

Tip: Looking for answers? Try searching our database.

need help testing small part of userform

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Susan - 27 Sep 2006 20:25 GMT
i have been learning Excel VBA, now am moving onto Word....
in my first attempt @ a userform, i am stuck a little on the structure
as it's different from Excel...  i've got the form set up &
initialized, but trying this first small command isn't working.
it runs fine until i try to set the focus, then it says that page isn't
enabled (even tho i thought i told it to enable it if that specific
checkbox = true).
any corrections would be greatly appreciated!
also, is all the "addressing" really necessary?  such as
frmGenerateMortgages.MultiPage1("pgChoose").chkAHC
if there's only one "chkAHC" in the userform, why can't i just
reference the chkbox by name?  it wouldn't let me before i added all
the "address" info.
susan
**************
Private Sub cmdGenerate_Click()

Dim pgAdditional As Control

Dim chkAHC As Control

If frmGenerateMortgages.MultiPage1("pgChoose").chkAHC.Value = True Then
  frmGenerateMortgages.MultiPage1("pgAdditional").Enabled = True
  MsgBox "Please fill in additional AHC information" '+ vbInformation
  frmGenerateMortgages.MultiPage1("pgAdditional").txtSection.SetFocus

End If

End Sub
Shauna Kelly - 28 Sep 2006 12:41 GMT
Hi Susan

A few things might help here.

First, within a form (that is, for the code behind the form), the word Me
refers to the current instance of  the form.

So Me.MultiPage1 refers to the MultiPage1 control. For various reasons, it's
much better to refer to the form itself as Me than by the name of the form.

Since there is only one chkAHC on the whole form, you can refer to it with
Me.chkAHC.

However, you can't set the focus to a control on a multipage unless the
appropriate page is enabled, visible and displayed. So, to set focus to a
control on a multipage, first set the  multipage's .Value to equal the page
that holds your control, so as to display that page. Then, you can set the
focus to the individual control.

Finally, it's worth getting the hang of the With statement. Using this you
can do something like the following (not tested):

With Me
   If .chkAHC.Value = True Then
       .MultiPage1("pgAdditional").Enabled = True
       .MultiPage1.Value = 2 'or whatever value is appropriate
       .txtSection.SetFocus

       'It's a good idea to show this message *after* you display the
       'part of the form that you want the user to complete
       MsgBox "Please fill in additional AHC information", vbOKOnly +
vbInformation
   End If
End With

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

>i have been learning Excel VBA, now am moving onto Word....
> in my first attempt @ a userform, i am stuck a little on the structure
[quoted text clipped - 25 lines]
>
> End Sub
Tony Strazzeri - 28 Sep 2006 15:53 GMT
Hi Susan,

To answer your last question first, You can.

Assuming that chkAHC and txtSection are controls on the form.

You should not declare thse as they are intrinsically declared by being
a member of the form's controls container.  pgAdditional should not be
declared for the same reason.  By declaring them you are creating an
additional items which are confusing VBA, hence the need to fully
reference the items.

Neither do you need to use the form name when referencing items in the
form from within the form's code.

I am also assuming that the textbox is on the multipage page
"pgAdditional".

The following code should demonstrate what you are attempting to do.

If chkAHC = True Then
  MultiPage1.Value = MultiPage1.Item("pgAdditional").Index
  MsgBox "Please fill in additional AHC information" '+ vbInformation
  txtSection.SetFocus
End If

Notice that I am not using the enable method but simply setting the
index of the multipage to the page I want to select. You need to do
this (select the page) before you can set focus to any controls on that
page.

Hope this helps.
Cheers
TonyS.

> it runs fine until i try to set the focus, then it says that page isn't

> enabled (even tho i thought i told it to enable it if that specific
> checkbox = true).
[quoted text clipped - 20 lines]
>
> End Sub
Tony Strazzeri - 28 Sep 2006 15:54 GMT
Hi Susan,

To answer your last question first, You can.

Assuming that chkAHC and txtSection are controls on the form.

You should not declare thse as they are intrinsically declared by being
a member of the form's controls container.  pgAdditional should not be
declared for the same reason.  By declaring them you are creating an
additional items which are confusing VBA, hence the need to fully
reference the items.

Neither do you need to use the form name when referencing items in the
form from within the form's code.

I am also assuming that the textbox is on the multipage page
"pgAdditional".

The following code should demonstrate what you are attempting to do.

If chkAHC = True Then
  MultiPage1.Value = MultiPage1.Item("pgAdditional").Index
  MsgBox "Please fill in additional AHC information" '+ vbInformation
  txtSection.SetFocus
End If

Notice that I am not using the enable method but simply setting the
index of the multipage to the page I want to select. You need to do
this (select the page) before you can set focus to any controls on that
page.

Hope this helps.
Cheers
TonyS.

> it runs fine until i try to set the focus, then it says that page isn't

> enabled (even tho i thought i told it to enable it if that specific
> checkbox = true).
[quoted text clipped - 20 lines]
>
> End Sub
Susan - 29 Sep 2006 13:46 GMT
Shauna & Tony
many thanks!  that makes a lot more sense.  a few more
questions............

a) which one is "more" correct <vbg>?
MultiPage1.Value = MultiPage1.Item("pgAdditional").Index
or
.MultiPage1.Value = 2

b) you guys are saying i don't need to do this, correct?
Dim pgAdditional As Control
Dim pgData As Control

c) since i am ultimately going to be pasting data from the user form
into documents, i searched the newsgroup & found this style - is it
correct?:

Dim vName As Variable
vName = frmGenerateMortgages.MultiPage1.pgData.txtName.Text

perhaps it would better be defined as
vName = Me.MultiPage1.Value("2").txtName.Text  ?

(if not, i'm DEFINITELY shortening that form name!!!!  :D  )
many thanks for your patience & help.
susan
Shauna Kelly - 30 Sep 2006 10:07 GMT
Hi Susan

> a) which one is "more" correct <vbg>?
> MultiPage1.Value = MultiPage1.Item("pgAdditional").Index
> or
> .MultiPage1.Value = 2

Six of one and half a dozen of the other.  If the page in your multipage
named "pgAdditional" is the 3rd page (ie has an .Index of 2) then
   MultiPage1.Item("pgAdditional").Index
will resolve to 2.

And so
   MultiPage1.Value = MultiPage1.Item("pgAdditional").Index
will have the same effect as
   MultiPage1.Value = 2

In some cases, the first method would be marginally 'safer' because, if you
change the order of your pages, then the code will still work and will still
display the page named "pgAdditional" even if it's now, say, page 7. On the
other hand, if you use the MultiPage1.Item("pgAdditional") construct, and
you change the name of your page, then you'll have to remember to change
this line of code, too.

I don't like using the names of controls without explicitly telling VBA what
control it is. So I would do either:
    Me.MultiPage1.Value = 2   OR   Me.MultiPage1.Item("pgAdditional").Index
or
   With Me
       .MultiPage1.Value = 2   OR   .MultiPage1.Item("pgAdditional").Index
   End With

> b) you guys are saying i don't need to do this, correct?
> Dim pgAdditional As Control
> Dim pgData As Control

Without going through your code in detail, it's hard to say whether you need
these or not. However, make sure that you put
   Option Explicit
at the top of every module, form and class. You can get the VBE to do this
automatically for you if you do Tools > Options and on the Editor tab, tick
"Require variable declaration".

> c) since i am ultimately going to be pasting data from the user form
> into documents, i searched the newsgroup & found this style - is it
[quoted text clipped - 5 lines]
> perhaps it would better be defined as
> vName = Me.MultiPage1.Value("2").txtName.Text  ?

Neither of those lines of code will work, for various reasons. But let's
ignore that and get on with what you actually need to do.

I'm assuming that frmGenerateMortgages is the name of your userform. That
is, this is the name that appears in the Project Explorer. As a general
rule, do *not* use that name anywhere in the code of the form itself.

I'm assuming that txtName is a text box.

Then the code above seems to be trying to store the text in that text box in
a Word Variable. That may be what you want, but it won't insert the text
into your document. But if you do want a Word Variable, and if txtName is a
textbox, then you need something like:
   Dim vName As Word.Variable

   Set vName = ActiveDocument.Variables.Add(Name:="Name of my variable")
   vName.Value = Me.txtName.Text

If you then want the value of the Variable to appear in the document, do
ctrl-F9 and within the braces that Word gives you, type DOCVARIABLE  "Name
of my variable".

Hope this helps.

Shauna Kelly.  Microsoft MVP.
http://www.shaunakelly.com/word

> Shauna & Tony
> many thanks!  that makes a lot more sense.  a few more
[quoted text clipped - 22 lines]
> many thanks for your patience & help.
> susan
Tony Strazzeri - 01 Oct 2006 17:24 GMT
I only have two comments to add.

> Hi Susan
>
> > a) which one is "more" correct <vbg>?
> > MultiPage1.Value = MultiPage1.Item("pgAdditional").Index

I prefer to use the page name as I find it easier to recognise and
remember as distinct from a number which as Shauna says can change. If
you change the page name you will get a runtime error.

> > c) since i am ultimately going to be pasting data from the user form
> > into documents, i searched the newsgroup & found this style - is it
> > correct?:
> >
> > Dim vName As Variable
> > vName = frmGenerateMortgages.MultiPage1.pgData.txtName.Text

Don't confuse Word Document variables with program variables.

Dim vName -defines a program variable as a varient data type (because
the actal dtat type has not been specified).
Dim vName As String - defines a variable that will contain text data.

Cheers
TonyS.
Susan - 01 Oct 2006 17:39 GMT
i appreciate all your help, you two!  i'm not @ work today (sunday) so
i can't look @ my code directly.  what i am specifically trying to do:

the user form will let users input the specific information that goes
into the blank mortgages.  the mortgages are already set up with text
form fields and REF fields to repopulate the data (at the moment i just
use them as tabbed forms, but each project can have 2-4 mortgages, &
i'm tired of retyping the same info over & over).  i have named the
document form fields the same names as the user form textboxes......
so, txtName in the user form (ultimately is supposed to) correspond
with txtName bookmark in the document.

once i get it to work with one mortgage, then the 10 or so check boxes
(after extensive coding & testing) will allow the program to pull up
each individual mortgage document, insert the data where necessary,
save it with a specific name (probably using the txtName field), print,
close that document, and open the next one.

i realize this is a VERY ambitious project but i feel you don't learn
anything if you don't try!  :D  i've got the userform up & running, &
thanks to your help i've got that AHC multipage working (that one
mortgage has some additional information required that the others don't
use).

i really don't want to post the whole code & have you guys "fix" it for
me; i'd rather try to do each part on my own & just ask for help when
i'm stuck.  at this point i'm trying to figure out how to dim each user
form textbox; as a variable, as a string?  one is a date, so that's
easy, and the numbers are obviously integers.  when i get to work
tomorrow i'll look @ your suggestions & see how i can implement them.
thanks again & hope you're enjoying your weekend!!
susan
Susan - 01 Oct 2006 17:55 GMT
also, actually @ this point this is in the wrong newsgroup....... i
should repost to word.vba.
:D
susan
 
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.