Setting a value to a option button & then calculating the total

D

dworst

I am using MS Word 2003 to create a Word document form.

I'm using tables to insert my form fields.

In one table I have option buttons where the user must select one of
the option buttons in that group. For example:

My first Option button group would be named "Adaptability". Within the
"Adaptability" Option button Group I have 4 option buttons:
Ineffective, Capable, Superior and N/A.

I have 10 Groups with each group having 4 options, Ineffective,
Capable, Superior and N/A.

Ineffective should have a checked value of 1, Capable should have a
checked value of 2, Superior should have a checked value of 3 and N/A
should have a checked value of -1.

I want to write something in code to check the value of each groups
option buttons and then present a total that will be divided by 10.

I hope I've given you the info you need to help me out, but if not,
please let me know and I'll try to explain further. Sometimes it is
difficult to convey what you want in writing.

I'm pretty much of a novice at this so can anyone help me out? I think
that I need some kind of CASE statement, but not sure. Thanks very
much in advance.
 
G

Greg Maxey

Something like this should work:

Sub Eval()
Dim a, b, c, d, e, f, g, h, i, j As Long
Dim pScore As Long
With ThisDocument
Select Case True 'First group
Case .OptionButton1.Value
a = 3
Case .OptionButton2.Value
a = 2
Case .OptionButton3.Value
a = 1
Case .OptionButton4.Value
a = -1
Case Else
MsgBox "No selection in Group 1"
End Select
Select Case True 'First group
Case .OptionButton5.Value
b = 3
Case .OptionButton6.Value
b = 2
Case .OptionButton7.Value
b = 1
Case .OptionButton8.Value
b = -1
Case Else
MsgBox "No selection in Group 2"
End Select
'Repeat for groups 3 through 10
pScore = (a + b) / 2 'Test formula. Delete when you have create the
other groups
'pScore = (a + b + c + d + e + f + g + h + i + j) / 10 'Real formula.
MsgBox "Average score = " & pScore
End With
End Sub
 
D

dworst

Greg:
Thank you for the quick response. If I wanted pScore to show up in a
form field called "Total Score" how would I do that:?
 
D

dworst

Greg:

Also, I put your code in as shown below, but it doesn't seem to be
calculating the total correctly. If "Ineffective" is = 1 and "Capable"
is equal to 2 then the total would be 3 / 2 which would be 1.5 and I
think it is rounding up. I don't want it to do that.

Thanks, again!
 
G

Greg Maxey

Yes sorry.

Change the declaration of a, b, c, etc. and pScore to Double vice Long
 
G

Greg Maxey

You will need to unprotect the form, insert the value, reprotect the
form. Something like this incorporated at the end to the other macro
should do:

With ActiveDocument
.Unprotect
.FormFields("Total_Score").Result = pScore
.Protect wdAllowOnlyFormFields, True
End With
 
D

dworst

Greg:
Thank you for the quick response. If I wanted pScore to show up in a
form field called "Total Score" how would I do that:?
 
G

Greg Maxey

Asked and answered. See earlier post in this string.

BTW, you can't have a formfield named "Total Score" as bookmark names
cannot contain spaces. You could use "Total_Score" as in the example I
provided.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top