Calculating Options Buttons in Word 2000

J

jgeorgen

I am building an evaluation form for work. There are seven
categories. In each category there are five ratings (1-5) and users
select a rating by clicking an option button. I'd like to calculate
the numeric total of all the selected option buttons at the bottom of
the form and I've tried to use the following code to do so:

Dim a, b, c, d, e, f, g As Long
Dim pScore As Long
With ThisDocument
Select Case True 'First group
Case .OptionButton1.Value
a = 5
Case .OptionButton2.Value
a = 4
Case .OptionButton3.Value
a = 3
Case .OptionButton4.Value
a = 2
Case .OptionButton5.Value
a = 1
Case Else
MsgBox "No selection in Group 1"
End Select
'this would be repeated for the other categories.
End With
End Sub

When finally running the code, however, I get the following error
msg... "Compile Error: Method or Data Member not found" and the
program highlights the words "OptionButton1" in the code. Can someone
help me understand what this means and how to fix it? Thanks very
much.
 
D

Doug Robbins - Word MVP

Put each group of 5 option buttons in a frame, give each frame a name
(framea, frameb, framec, framed, etc), then use the following code:

Dim acontrol as Control
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, g
As Long
a = 0
For i = 1 To 5
Set acontrol = Framea.Controls(i - 1)
If acontrol.Value = True Then
a = i
End If
Next i
If a = 0 then
msgBox "No Selection for Group 1"
End If
b = 0
For i = 1 To 5
Set acontrol = Frameb.Controls(i - 1)
If acontrol.Value = True Then
b = i
End If
Next i

If b = 0 then
msgBox "No Selection for Group 2"
End If
c = 0
For i = 1 To 5
Set acontrol = Framec.Controls(i - 1)
If acontrol.Value = True Then
c = i
End If
Next i

If c = 0 then
msgBox "No Selection for Group 3"
End If
d = 0
For i = 1 To 5
Set acontrol = Framed.Controls(i - 1)
If acontrol.Value = True Then
d = i
End If
Next i

If d = 0 then
msgBox "No Selection for Group 4"
End If
e = 0
For i = 1 To 5
Set acontrol = Framee.Controls(i - 1)
If acontrol.Value = True Then
e = i
End If
Next i

If e = 0 then
msgBox "No Selection for Group 5"
End If
f = 0
For i = 1 To 5
Set acontrol = Framef.Controls(i - 1)
If acontrol.Value = True Then
f = i
End If
Next i

If f = 0 then
msgBox "No Selection for Group 8"
End If
g = 0
For i = 1 To 5
Set acontrol = Frameg.Controls(i - 1)
If acontrol.Value = True Then
g = i
End If
Next i

If g = 0 then
msgBox "No Selection for Group 7"
End If

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg Maxey

Doug,

Thanks for posting. Your method got me to thinking about looping
through the groups using an array. What do you think of this method?


Private Sub CommandButton1_Click()
Dim acontrol As Control
Dim catArray() As String
Dim resultsArray(6) As Long 'One less than number of groups
catArray = Split("a|b|c|d|e|f|g", "|")
Dim lngGroup As Long
Dim i As Long
'Loop throug each group. Note: controls are indexed starting with 0
For lngGroup = 0 To 6
'Loop through each frame control
For i = 0 To 4
Set acontrol = Controls("Frame" & catArray(lngGroup)).Controls(i)
If acontrol.Value = True Then
resultsArray(lngGroup) = i + 1
Exit For
ElseIf i = 4 Then
MsgBox "No selection was made for Group " & lngGroup + 1
Exit Sub
End If
Next i
Next lngGroup
lngGroup = 0
For i = 0 To UBound(resultsArray)
lngGroup = lngGroup + resultsArray(i)
Next i
MsgBox Format(Val(lngGroup) / 7, "#.00")
Unload Me
End Sub
 
D

Doug Robbins - Word MVP

You can get to the finishline by crawling, walking or running.

Need to learn how to crawl, then walk, before trying to run.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

jgeorgen

You can get to the finishline by crawling, walking or running.

Need to learn how to crawl, then walk, before trying to run.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








- Show quoted text -

Doug,

I think I'm crawling.:) Thanks to both you and Greg for your help.
Here's another question. Your code...Do I need to be incorporating a
UserForm to employ the code? If so, how do I make a document that
uses Option Buttons set in a UserForm? Any good MVP articles that I
can study? I am a novice at UserForms.

Thanks so much again,
Josh
 
G

Greg Maxey

Doug,

I think I'm crawling.:) Thanks to both you and Greg for your help.
Here's another question. Your code...Do I need to be incorporating a
UserForm to employ the code? If so, how do I make a document that
uses Option Buttons set in a UserForm? Any good MVP articles that I
can study? I am a novice at UserForms.

Thanks so much again,
Josh- Hide quoted text -

- Show quoted text -

Josh,

Yes the code Doug provided and the sprinter version I proposed ;-)
requires a userform.

The optionbuttons are in the userform. In the document you could use
bookmarked symbols to represent the options selected in the user from.

Based on your form design of 7 groups (a - g) and each group having 5
options, you would need to create 35 bookmarks in the document named
Groupa1 through Groupa5
Groupb1 through Groupb5 etc.

For my test document. I inserted a WindDings2 Font character 154 to
represent a the not selected option buttons. I created 7 groups of 5
of these symbols and bookmarked them as described above.

Then I ran this code as the Command_Button click event in the
UserForm.
Private Sub CommandButton1_Click()
Dim catArray() As String
Dim lngGroup As Long
Dim bSelection As Boolean
Dim i As Long
Dim acontrol As Control
Dim resultsArray(6) As Long 'One less than number of groups
Dim oRng As Word.Range
catArray = Split("a|b|c|d|e|f|g", "|")
'Loop throug each group. Note: controls are indexed starting with 0
Application.ScreenUpdating = False
For lngGroup = 0 To 6
bSelection = False
'Loop through each frame control
For i = 0 To 4
Set acontrol = Controls("Frame" & catArray(lngGroup)).Controls(i)
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup) &
i + 1).Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3942, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i + 1,
oRng
If acontrol.Value = True Then
resultsArray(lngGroup) = i + 1
bSelection = True
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup)
& i + 1).Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3938, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i +
1, oRng
End If
Next i
If Not bSelection Then
MsgBox "No selection was made for Group " & lngGroup + 1
Exit Sub
End If
Next lngGroup
lngGroup = 0
For i = 0 To UBound(resultsArray)
lngGroup = lngGroup + resultsArray(i)
Next i
MsgBox Format(Val(lngGroup) / 7, "#.00")
Unload Me
Application.ScreenUpdating = True
Application.ScreenRefresh
End Sub

The code is a little clunky as I need to figure out (if I can) a
better way of defining the bookmark text.
 
D

Doug Robbins - Word MVP

Greg's off running again and unless I missed it, he did not point you in the
direction of creating a userform.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

jgeorgen

Greg's off running again and unless I missed it, he did not point you in the
direction of creating a userform.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP







- Show quoted text -

Greg,

Thanks so much! I will try that out and let you know how it goes.

Doug,

Thanks for the reading suggestion. That's a great idea, and I
actually did read it before I started this project. Even so, I'll
review it again to make sure I didn't miss anything. My biggest
question was simply getting the nicely arranged UserForm to populate
onto a template. In the reading material, the code for the
commandbutton is given to you, and so when I branch out from that, I'm
lost because I barely know any code.

I'll give both of your suggestions a whirl and report back. Thanks
again for your continued help.

Josh
 

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