Radio buttons -- assigning variables, adding totals?

K

Kamran

Is it possible to use radio buttons to assign a value to a variable for
generating totals? In my case, I have a table where the user clicks a radio
button in a row. All items in that row are mutually exclusive, i.e., you can
only click one button in that row. I would like it to generate a value
depending on what button you click, so if you click on the third column,
let's say a "total" cell on the far right would be assigned a value of 3, if
you click on the fifth column, the "total" cell would be assigned a value of
5. Then I would to calculate an overall total at the end of the table. Is this
feasible?

I'm real good with Word and pretty familiar with macros, but I'm not a
developer so I may need some detailed instructions.
Thanks.
 
K

Kamran

I should probably add I'm using Word 2003, and I'm using FORMTEXT fields and
others, in a protected form. The buttons I created using the Control Toolbox.
 
G

Greg Maxey

I should probably add I'm using Word 2003, and I'm using FORMTEXT fields and
others, in a protected form.  The buttons I created using the Control Toolbox.

I suppose you would use the optionbutton click event. Something like
this:

Private Sub OptionButton1_Click()
ActiveDocument.Tables(1).Cell(1, 5).Range.Text = 1
End Sub
Private Sub OptionButton2_Click()
ActiveDocument.Tables(1).Cell(1, 5).Range.Text = 2
End Sub
Private Sub OptionButton3_Click()
ActiveDocument.Tables(1).Cell(1, 5).Range.Text = 3
End Sub

Where .Cell(1,5) is the first row, fifth column.
 
K

Kamran

Okay, thanks for that. Now I have a few questions:
How and when does the code get executed?
How can I find out the exact cell reference, cause my tables have a lot of
joined cells.
Can I name my tables, or does it go by a number?
Thanks again.
 
K

Kamran

Alternatively, is there a way to simply assign a value to variable that I
could just use a bookmark for in the result cell?
 
C

Cindy M.

Hi Kamran,
How and when does the code get executed?
When the radio button is clicked.
How can I find out the exact cell reference, cause my tables have a lot of
joined cells.
Can you explain exactly what information it is that you want to "get" from
the cell?

Greg's code has hard-coded values. And since you have to create a separate
"Click" procedure for each button, you may as well hard-code the values?
Can I name my tables, or does it go by a number?
Internally, Word has no particular way of identifying tables in a document.
You can select a table, then apply a bookmark to it.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
K

Kamran

Thanks for responding, Cindy.
This doc is a medical calculation chart. So let's say we have a row called
"Temperature" and there are four columns in that row with different ranges of
temperatures. If the user clicks in column 1, then I want to create a
"Temperature" variable that contains the value "1". If the user clicks in
column 2, then I want to assign the "Temperature" variable a value of "2".
Similarly, I want to do the same with heart rate, blood pressure, etc. Then
I want to display each of these values in a column on the right and then add
all these values together for a total score. I hope that makes sense.
 
C

Cindy M.

Hi Kamran,
This doc is a medical calculation chart. So let's say we have a row called
"Temperature" and there are four columns in that row with different ranges of
temperatures. If the user clicks in column 1, then I want to create a
"Temperature" variable that contains the value "1". If the user clicks in
column 2, then I want to assign the "Temperature" variable a value of "2".
Similarly, I want to do the same with heart rate, blood pressure, etc. Then
I want to display each of these values in a column on the right and then add
all these values together for a total score. I hope that makes sense.
Yes, it makes sense.

I don't understand why you aren't able to use the code Greg gave you...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
K

Kamran

Apparently it won't update the screen, because the form is locked. I was
trying to assign the value to a bookmark or variable, and then reference it
that way.
 
K

Kamran

I'm trying to do something like this, but it doesn't work:

Private Sub SOFA_Resp0_Click()
Set SOFA_Resp = 0
End Sub

I'm not a programmer (only did a little BASIC 20 years ago), so I may need
extra help with this.
 
K

Kamran

I've also done this, as in Greg's code, but it only works when I click the
Run button in the VB editor. It does nothing when the form is locked and I
click the buttons.

Private Sub SOFA_Resp0_Click()
ActiveDocument.Tables(1).Cell(4, 7).Range.Text = 0
End Sub
 
G

Gordon Bentley-Mix

The problem is the locked form. The code writes text into a Range, but Word
can't do this if the document is locked. You'll need to unlock it first and
then lock it again afterwards. See the VBA help topics on Protect and
Unprotect methods for more information.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
K

Kamran

Thanks for the reply, Gordon. I finally worked around it this way, which
allows me to just assign the value to a variable, then I can just use a
formula elsewhere to generate a total score:

Private Sub Button1_Click()
ActiveDocument.Variables("var1").Value = 0
ActiveDocument.Bookmarks("TOTAL").Range.Fields.Update
End Sub
 

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