Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003: dropdown form field to auto complete another field 1

Status
Not open for further replies.

pendle666

Technical User
Jan 30, 2003
295
GB
Hello

I have a form in Word 2003 which is normally protected so the user only completes the various fields and dropdowns.

What I would like to happen is if a user selects a particular item from the dropdown menu for it to populate other fields.

For example: The "benefit band" dropdown - if "6" is selected, then I want the "holiday" field to be "25" and "notice period" to be "1 month".

How can I do this? I've not done VBA with form fields before.

Pendle
 
Not to be critical, but how much searching have you done regarding this? It is a subject that comes up regularly.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Hello

I've actually done quite a bit of searching on this subject. Whilst there are macros for dealing with bookmarks and userforms to fill in forms, I need to be able do as described in my original message. And that is what I've been unable to find out.


Pendle
 
Hi Pendle,

As Gerry says, this sort of this comes up fairly often, and the replies are pretty standard:

You can use fields coded as-
{IF{REF benefit_band}= 6 25}
and
{IF{REF benefit_band}= 6 "1 month"}

Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste them from this message.


Cheers
[MS MVP - Word]
 
Or you can use macros (VBA). There is an admitedly complicated example in my Formfield FAQ on this site. Note to self: got to update that thing. However, there are lots and lots of examples of what you are asking about, if you look hard enough.

"For example: The "benefit band" dropdown - if "6" is selected, then I want the "holiday" field to be "25" and "notice period" to be "1 month".

You do not state if the "holiday" formfield is a text formfield - and please, use proper terms when posting as much as possible. Fields and Formfields are different, and when you talk about one (or the other) is helpful to state one...or the other.

Is "notice period" a text formfield? You do not state if it is, so I am assuming that it is. BTW: it must be really named something else, because you can not have spaces in formfield names.

Code:
Dim DocFF As ActiveDocument.FormFields

Set DocFF = ActiveDocument.FormFields

Select Case DocFF("benefit_band").Result
   Case  2  ' or whatever
       ' do something
   Case  "yadda"    ' or whatever
       ' do something else
   Case 6
      DocFF("holiday").Result = "25"
      DocFF("notice_period").Result = "1 month"
   Case 13456
       ' do something else
   Case Else
       ' whatever
End Select
If you have a lot of formfields, it is better to use a FormFields - notice the plural - object. That way, you can use:
Code:
   DocFF("holiday").Result = "25"
   DocFF("notice_period").Result = "1 month"
instead of:
Code:
   ActiveDocument.Formfields("holiday").Result = "25"
   ActiveDocument.Formfields("notice_period").Result = "1 month"
Just less writing. It also allows faster looping through all the formfields (if this is ever required).

Note: if you are using conditional logic to replace the items in a dropdown formfield, make SURE you clear the entries first! I posted a fairly decent example at VBAExpress ages ago. It comes up if you google "Word formfields conditional logic".

Also, if you google that, there are also multiple examples of using Fields (like the sample post above), that macropod has posted all over the place.

macropod is the leading expert of using Fields in Word, and as we have both mentioned, this is a question that is asked repeatedly. Me, I tend to go the VBA route as I find it clearer to think through - all the possible nested IF statements makes me batty - but there are certainly advantages with using Fields. Not having to deal with possible Security issues for one.

Whether you go the Fields route, or VBA, depends on a clear determination of your requirements.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
I'm very sorry for not using the technical terms - my status might be technical user, but I'm certainly not one, but there isn't a lower status than that.

I object to the implication that I have not looked hard enough, but then when you don't know the exact terms of what you're looking for, it does make it rather hard to find an answer. If I had known that I was meant to google "word formfields conditional logic", then I wouldn't have needed to come here.

Thank you though for providing an answer to my query.

Pendle
 
I was not trying to be critical. What DID you use as a search?

The key is "logic" and/or "conditional"...which is what you are, in fact, trying to do.

"Formfields" + "logic" gets a number of hits as well.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Hello

As I said, I wasn't really sure what to look for. I can't remember the exact terms but it was more along the lines of using VB with word form fields.

Each item to be completed in the form is either a text field or a dropdown. They have been renamed as all one word or with a _ between words.

I've tried using these as suggested

{IF{REF benefit_band}= 6 25}
{IF{REF benefit_band}= 6 "1 month}

but unfortunately they've not worked for me - asuming that they were meant to go into the holidays and notice_period text fields. The bookmark is definitely called "benefit_band"


Pendle
 
Hi Pendle,

Those aren't "meant to go into the holidays and notice_period text fields" - and if you followed the instructions I gave you'd have soon found it impossible to do so. What I posted are fields in their own right. Simply choose where you want the results to appear in the document and input the fields as described. Also, make sure the 'benefit_band' field's properties have the 'calculate on exit' attribute checked.


Cheers
[MS MVP - Word]
 
Hi

This is what you said in your post:

"You can use fields coded as-
{IF{REF benefit_band}= 6 25}
and
{IF{REF benefit_band}= 6 "1 month"}

Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste them from this message."

Here's what I did:

Table Cell B5 is where the number of days holidays goes. I did CTRL + F9 to create a pair of braces and typed in IF then did another pair of braces and typed REF benefit_band in that 2nd pair and within the first pair = 6 25 exactly as you put in your post.

The benefit_band dropdown has "calculate on exit" checked.
I then locked the document, changed the benefit_band dropdown from "choose.." to "6", tabbed onto the next text field and the following message appears instead of 25:

Error: Unknown op code for conditional. When I closed and reopened my saved doc, the code has changed to {IF choose... = 6 25}, the reference has to be entered again before I can try again.


regards

Pendle
 
Hi Pendle,

The "Error: Unknown op code for conditional" message suggests you haven't input the field code correctly - you need to pay particular attention to the spacing between the expressions. I've tested both field codes on my own system, so I know they work.


Cheers
[MS MVP - Word]
 
Hello

I've double checked the spacing, copying exactly as your example and it's worked. But unfortunately when I close (without saving) and then reopen to start again, the unknown op code error message appears straight away, probably because "choose..." is the default option, not 6.

thanks for your help anyway.

Pendle
 
Hi Pendle,

Whether the default is the same as what the IF test is looking for makes no difference to the field's performance. You will, however, get the "Error! Unknown op code for conditional." result if there's no space between the '=' and the '6'.


Cheers
[MS MVP - Word]
 
Hi
I've redone the document from scratch this time, and it's worked at last.

Thank you very much for your support.

Pendle

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top