# Thread: Sum Values For Check Boxes

1. Registered User
Join Date
Jan 2004
Posts
164

## Unanswered: Sum Values For Check Boxes

Hi,

I have a 3 checkboxes. Each checkbox represents a value that is listed in a textbox right next to the checkbox.

I also have a summary textbox that i would like it to show the sum of all the checkboxes that where checked.

So if checkbox 1 and 2 are checks. And 1 is 50 and 2 is 100, then i would llike the summary textbox to display the totals of the two that where checked. This would mean that 150 would be displayed in this box.

Is there a way i can do this?

2. Moderator
Join Date
Jun 2005
Location
Richmond, Virginia USA
Posts
2,764
You didn't mention if you wanted to store these totals in a table. You really shouldn't! There are cases where it's justifiable to store calculated values, but this isn't one of them.You can do this same caluclation anytime you need this total, be it in queries, other forms or reports. For the purposes of this demo I've called your "summary textbox" TotalValue.

You didn't say what value you were assigning to the third checkbox, so for this example I arbitrarily used 150. You can change this, of course!

Code:
```Private Sub Form_Current()
Dim Check1Val, Check2Val, Check3Val, RunningTotal As Integer
RunningTotal = 0
If CheckBox1 Then
RunningTotal = RunningTotal + 50
End If
If CheckBox2 Then
RunningTotal = RunningTotal + 100
End If
If CheckBox3 Then
RunningTotal = RunningTotal + 150
End If

TotalValue = RunningTotal
End Sub

Private Sub CheckBox1_Click()
Dim Check1Val, Check2Val, Check3Val, RunningTotal As Integer
RunningTotal = 0
If CheckBox1 Then
RunningTotal = RunningTotal + 50
End If
If CheckBox2 Then
RunningTotal = RunningTotal + 100
End If
If CheckBox3 Then
RunningTotal = RunningTotal + 150
End If

TotalValue = RunningTotal
End Sub

Private Sub CheckBox2_Click()
Dim Check1Val, Check2Val, Check3Val, RunningTotal As Integer
RunningTotal = 0
If CheckBox1 Then
RunningTotal = RunningTotal + 50
End If
If CheckBox2 Then
RunningTotal = RunningTotal + 100
End If
If CheckBox3 Then
RunningTotal = RunningTotal + 150
End If

TotalValue = RunningTotal
End Sub

Private Sub CheckBox3_Click()
Dim Check1Val, Check2Val, Check3Val, RunningTotal As Integer
RunningTotal = 0
If CheckBox1 Then
RunningTotal = RunningTotal + 50
End If
If CheckBox2 Then
RunningTotal = RunningTotal + 100
End If
If CheckBox3 Then
RunningTotal = RunningTotal + 150
End If

TotalValue = RunningTotal
End Sub```
Good Luck!

Linq

3. Registered User
Join Date
May 2004
Location
New York State
Posts
1,178
I understood that estefex wants to sum the textboxes if the associated checkboxes were checked, and the values were the values entered in the textboxes, and was not assigning the values to the checkboxes. Therefore, a simple 4-liner should do it.

Create a global variable, call it say ChkSum As Long

In the OnCurrent of the form, assign ChkSum = 0

In the AfterUpdate() of each checkbox, simply assign

Code:
`ChkSum = Chksum + IIf(Me.CheckboxName, Me.TextboxName, Me.TextboxName * -1)`
What this does is to allow the user to uncheck a checkbox and have the textbox value subtracted from the previous total.

You could also do it differently. Make a "Confirm" command button on the form for the user to press when he's finished/sure of all entries. Then in the command button's OnClick() event, the formula is smple:

Code:
```ChkSum = Chksum + IIf(Me.CheckboxName1, Me.TextboxName1, 0)
ChkSum = Chksum + IIf(Me.CheckboxName2, Me.TextboxName2, 0)
ChkSum = Chksum + IIf(Me.CheckboxName3, Me.TextboxName3, 0)

Do something useful with ChkSum....```
Sam

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•