# Web Development

## General discussion

Locked

### Simple VB Calculation

By ·
I'm new to VB and I'm completely stumped on a calculation assignment for my class. I've built a form with 10 checkboxes, and each have a different dollar amount assigned to them (prices for veterinary services). I can't figure out what code I needto assign to the command button to display the sum of only the checked boxes. How do I single out only the selected boxes to be calculated so that their sum will be displayed? Thanks for any help!

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

You have 10 checkboxes, if the names of these checkboxes are chk1 through chk10, the code for the button could be:

if chk1.value = 1 then sum = sum + (amount for checkbox #1)
if chk2.value = 1 then sum = sum + (amount for checkbox #2)
if chk3.value = 1 then sum = sum + (amount for checkbox #3)

and so on for each of the checkboxes

Hope this helped!!!

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

Hi Alex, thanks for responding! The code you gave me will display the price for each service individually, but will not total the items. For example, it will give the price for an office visit of \$20, but when office visit and grooming are both checked, the total value is that of whichever one that was chosen last. Hmmmm...

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

I am pretty much repeating the code from Alex:
This code assumes a form(form1), 10 check boxes (Check1 to Check10), and text box(text1).
Paste this code in appropriate places and run the program. Select one or more of the check boxes and click thebutton; You should see the total in the text box. I can send the VB project and the form, if you need.

Good luck!

Ravi<ananth_manabala@yahoo.com>
'Form General Code
Dim ch1 As Double
Dim ch2 As Double
Dim ch3 As Double
Dim ch4 As Double
Dim ch5 As Double
Dim ch6 As Double
Dim ch7 As Double
Dim ch8 As Double
Dim ch9 As Double
Dim ch10 As Double
Dim sumch As Double

Private Sub Command1_Click()
sumch = 0
If Check1.Value = 1 Then
sumch = sumch + ch1
End If
If Check2.Value = 1 Then
sumch = sumch + ch2
End If
If Check3.Value = 1 Then
sumch = sumch + ch3
End If
If Check4.Value = 1 Then
sumch = sumch + ch4
End If
If Check5.Value = 1 Then
sumch = sumch + ch5
End If
If Check6.Value = 1 Then sumch = sumch + ch6
End If
If Check7.Value = 1 Then
sumch = sumch + ch7
End If
If Check8.Value = 1 Then
sumch = sumch + ch8
End If
If Check9.Value = 1 Then
sumch = sumch + ch9
End If
If Check10.Value = 1 Then
sumch = sumch + ch10
End If
Text1.Text = sumch
End Sub

ch1 = 10
ch2 = 12
ch3 = 14
ch4 = 8
ch5 = 15
ch6 = 11
ch7 = 9
ch8 = 25
ch9 = 10.5
ch10 = 20
End Sub

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

There are several ways to do this and still keep the code short and simple - here is one:

( Requires changing the check box names)
First change all check boxes to the same names, (such as chkbxPrices), each with a different index. (Click on the first check box, change its' name in the properties window and then look for the index property and assign it 0. Then move to the next check box and change its' name to the same as in the first check box but assign its' index property to 1. Do the remaining check boxes increasing the indexes each time by one).

Then write the followingcode:

'CODE BLOCK START_____________________

'Declarations
'Values corresponds to the chkbxPrices indexes
Private Const GROOM% = 0
Private Const WASH%= 1
Private Const NAILS% = 2
'Etc

Private m_Prices() As Double

SetlPrices
End Sub

Private Sub SetlPrices()
ReDim m_Prices(chkbxPrices.Count - 1)
m_Prices(GROOM) = 10.45
m_Prices(WASH) = 9.50
m_Prices(NAILS) = 5.10
'Etc

End Sub

Private Function GetTotalPrice() As Double
Dim chkbx As CheckBox
Dim dTotal As Double

For Each chkbx In chkbxPrices

If chkbx.Value Then dTotal = dTotal + Prices(chkbx.Index)

Next chkbx
GetTotalPrice = dTotal

End Function

Then call the function from the check box, (or a command button), Click event:

Private Sub chkbxPrices_Click( Index As Integer)
'Show the sum in a label, (or create a Form level variable to store the value)
lblPrice.Caption = GetTotalPrice()
End Sub

'CODE BLOCK END_____________________

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

There is a typo in the function:

Prices in the line

If chkbx.Value Then dTotal = dTotal + Prices(chkbx.Index)

needs to be changed to m_Prices

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

IF you do not want to change the check box names as suggested then I would add the price to each check box TAG property and call the function in each check box click event or from the command button click event:

SetlPrices
End Sub

Private Sub SetlPrices()

Check1.Tag = "PRICE:10.45"
Check2.Tag = "PRICE:9.5"
Check3.Tag = "PRICE:5.1"

End Sub

Private Sub Check1_Click()
'Show the sum in a label
lblPrice.Caption = GetTotalPrice
End Sub

Private Sub Check2_Click()
'Show the sum in a label
lblPrice.Caption = GetTotalPrice
End Sub

Private Sub Check3_Click()
'Show the sum in a label
lblPrice.Caption = GetTotalPrice
End Sub

Private Function GetTotalPrice() As Double
Dim c As Control
Dim sTag As String
Dim dTotal As Double

For Each c In Me.Controls
If TypeOf c Is CheckBox Then
sTag = c.Tag
'Just in case there are other check boxes that have nothing to
'do with the prices, check if the current check box is a price check box
If InStr(sTag, "PRICE") Then
'It is a price check box - extract the price
sTag = Replace(sTag, "PRICE:", vbNullString)

'calc running total
If c.Value Then dTotal = dTotal + Val(sTag)
End If
End If
Next c

GetTotalPrice = dTotal

End Function

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

Thanks so much for all the suggestions...I finally figured it out and realized that I have to assign some value to all of the choices! I'm going to try to give all three of you tech points for taking the time to answer me...hope it works.

Collapse -

### Simple VB Calculation

by In reply to Simple VB Calculation

This question was closed by the author

## Related Discussions

• 20

• 10

• 0

#### Is it possible to place 2 links to same URL at one page?

Olive Route ·

• 0