If two options are "True" then...

N

Newforms

Hi, I am trying to put togther a form that populates a Word document,
the idea is this:

Bread: $50
Cheese: $20
Milk: $10

Total: $80

I want to have options that can be selected so that the total changes
as required, so that if only bread and cheese are selected the total is
$70.

The code I have so far is this:

Dim mytext As String
If OptionButton1.Value = True Then
bread = "Bread - $50"
ElseIf OptionButton2.Value = True Then
cheese = "Cheese - $20"
ElseIf OptionButton3.Value = True Then
milk = "Milk - $10"
End If

I am trying to include code to determine what options are true and
produce the correct total. Any ideas?

Thanks, Jonathan

PS. I know these are very expensive groceries, its just to illustrate
the point! ;)
 
T

Tony Strazzeri

The problem with the code you are using is that it will fall out of the
test as soon as ANY of the the tests are true.

Try defining a variable to hold the total and add to it each of the
amount for the true options.

BTW I am not sure what you are actually trying to do with the variables
called bread, cheese, milk since thay appear to be labels and not
values.
ig
dim lTot as long

lTot=0
dim sTape as string

If OptionButton1.Value = True
This can be simplified as "If OptionButton1 Then" since true is the
default value.

try this.
If OptionButton1 Then
iTot=iTot+50 'Bread
sTape=sTape & vbcr & "Bread - $50"
endif

If OptionButton2 then
iTot=iTot+20
sTape=sTape & vbcr & "Cheese - $20"
endif

If OptionButton3 then
iTot=iTot+10
sTape=sTape & vbcr & "Milk - $10"
endif

msgbox sTape & vbcr & "========" & vbcr & "Total " & iTot


Of course this can be further simplified by using descriptive names for
the option buttons (eg "obBread") and/or arrays to hold the label and
amount for each option button. Post your code back for suggestions
once you have the process working.


Cheers
TonyS.
 

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