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!

Assign a value in a text box on a Word 2000 form 1

Status
Not open for further replies.

sqleptical

Programmer
Sep 26, 2001
112
US
I'm new to forms in Word & am trying to assign a value in a text box named Deduction depending on the value of the Combo Box named s1. The VBA code below does not work.

If s1 = "Yes" Then
Deduction = "B"
Else
Deduction = "C"
End If

Thanks!
 
Which kind of form ? An UserForm ?
What is the meaning of does not work ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It's a Word 2000 document that has Text Fields, Check Boxes, and Drop-Down Form Fields added to it for data capture purposes.

I put the If statement into a VBA Sub and assigned it to be ran when the S1 field is exited. No error is thrown, but neither "B" or "C" are entered into the Deduction Text Field.
 
Tip: use the Option Explicit instruction to discover that VBA have no meaning of what are s1 and Deduction.

Have a look at the FormFields collection.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes, FormFields led me to the answer! Thank You!!
Below works:

If ActiveDocument.FormFields("S1").Result = "Yes" Then
ActiveDocument.FormFields("Deduction").Result = "B"
Else
ActiveDocument.FormFields("Deduction").Result = "C"
End If

I love this forum!
 
Just a suggestion. If you are using a lot of formfields, you may find it handy to make more use of objects. You can declare the whole collection of formfields as one object, then point to individual formfields from that object. Like this:
Code:
Dim DocFF As FormFields
Set DocFF = ActiveDocument.FormFields

If DocFF("S1").Result = "Yes" Then
     DocFF("Deduction").Result = "B"
Else
    DocFF("Deduction").Result = "C"
End If

Notice the plural in:

Dim DocFF As FormFields

The plural means the object (DocFF) contains the entire collection of formfields in the document. You can also make singular objects, that is an object that is ONE formfield. This can be handy if you point to that object a lot.
Code:
Dim DocFF As FormFields
Dim S1_FF As FormField
Dim DeductFF As FormField
[COLOR=red] ' notice the singular, not plural[/color red]

Set DocFF = ActiveDocument.FormFields
Set S1_FF = DocFF("S1")
Set DeductFF = DocFF("Deduction")

If S1_FF.Result = "Yes" Then
     DeductFF.Result = "B"
Else
    DeductFF.Result = "C"
End If

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top