10 replies
by alex5
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!!!
by jade29
by jade29
by amanabala
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
'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
Private Sub Form_Load()
ch1 = 10
ch2 = 12
ch3 = 14
ch4 = 8
ch5 = 15
ch6 = 11
ch7 = 9
ch8 = 25
ch9 = 10.5
ch10 = 20
End Sub
by jade29
by sl-campbell
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
Private Sub Form_Load()
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_____________________
by sl-campbell
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
by sl-campbell
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:
Private Sub Form_Load()
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
by jade29
by jade29
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending