Easy Beginner VB Question Part II

I posted a question last week and have yet another...I'm new to VB programming and am not real familiar with the code, but I am familiar with the concepts.

I have one simple form...a very small form with just a few items.  All I'm going to need to do is assign values to check boxes, make sure at least ONE check box is checkmarked and then when the user clicks CALCULATE it gives them a running total...or if they click END it shuts down.  Simple!

I'm working with two parts of my little sample application, the first one has three menu choices in the form of checkboxes.  Each check box has a monetary value assigned to it. I'm not sure what the code is to declare these values...do I declare them at the beginning as an array??  For instance I have chkBox1, chkBox2, chkBox3.  I'm puzzled by the difference between assigning these boxes their own value or assigning them string values in an array DIM statement?  I basically need a dollar value to each box and a validity check to make sure the user checks at least ONE of these check boxes when it's time to caluclate.  

At the bottom of thise form I have about six more check boxes.  Each one of these is unique and the user can check as many as they want...they all have the same value assigned...let's say $100.00.  For each one of these checkmarked, it adds to the total.  So when the user clicks on CALCULATE...it takes the total of all checked boxes and displayes them in a text box called txtTotal.  

I'm a little bit unsure on where or how I should declare these actually.  Should all the code be under the CALCULATE button or does some of it get loaded under the Form itself?  Thanks!!

LVL 3
nouelletteAsked:
Who is Participating?
 
presbriaConnect With a Mentor Commented:
'This code works.
'You'll need to set up a control array for the checkboxes with the same value

'Ask me if you'd like anything explained

Private Sub cmdCalculate_Click()
    Dim iCount, iTotal As Integer
    If AtleastOneCheckboxIsChecked() Then
        iTotal = 0
        'loop through control array of check boxes with same value
        For iCount = 0 To 2
            If checkboxsamevalues(iCount).Value = vbChecked Then
                iTotal = iTotal + 100
            End If
        Next
   
        'check each box with different values
        If checkboxdifvalues1.Value = vbChecked Then
            iTotal = iTotal + 125 'whatever amount
        End If
        If checkboxdifvalues2.Value = vbChecked Then
            iTotal = iTotal + 198 'whatever amount
        End If
        If checkboxdifvalues3.Value = vbChecked Then
            iTotal = iTotal + 111 'whatever amount
        End If
   
        Call MsgBox("The total is " & iTotal & ".", vbOKOnly, "Total")
    Else
        Call MsgBox("You need to select atleast one option.", vbOKOnly, "Error")
    End If
End Sub

Private Sub cmdExit_Click()
    End
End Sub

Private Function AtleastOneCheckboxIsChecked() As Boolean
    AtleastOneCheckboxIsChecked = False
    Dim iCount As Integer

    For iCount = 0 To 2
        If checkboxsamevalues(iCount).Value = vbChecked Then
            AtleastOneCheckboxIsChecked = True
            Exit Function
        End If
    Next
    If checkboxdifvalues1.Value = vbChecked Then
        AtleastOneCheckboxIsChecked = True
        Exit Function
    End If
    If checkboxdifvalues2.Value = vbChecked Then
        AtleastOneCheckboxIsChecked = True
        Exit Function
    End If
    If checkboxdifvalues3.Value = vbChecked Then
        AtleastOneCheckboxIsChecked = True
        Exit Function
    End If
End Function
0
 
intelnetCommented:
You might check out this site-> www.planet-source-code.com
They have lots of different examples that might help.
0
 
JacamarCommented:
in the checkbox code code, and begin your program with command1 not enabled.

if checkbox1.value = vbchecked then
   Command1.enabled = True
   Else
   if checkbox2.value = vbchecked then
     Command1.enabled = True
     Else
     If checkbox3.value = vbchecked then
       Command1.enabled = True
       Else
       Command1.enabled = False
     End if
   End if
End if

Now create a control array for your form checkboxes and use this code.  Call your control array "Checkboxes"

Private sub Checkboxes_Click(Index as integer)
For inI = 0 to 4
 If Checkboxes(inI).value = vbchecked then
  sgTotal = sgTotal + 100
  Picture1.cls
  Picture1.Print sgTotal
 end if
Next inI
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nouelletteAuthor Commented:
Presbia,

This is good except it doesnt calculate my group of values that are the same.  For some reason I get a total...and if I check some more same value boxes that should increment the price by 100...the price stays the same.  
0
 
DoItMyWayCommented:
Probably the most direct method of handling several controls on a form is to create a control array for them. i.e.:

-Name all the controls [checkboxes] you want to group together with the same name. Then set their index in a 0 based sequential order.

-When its time to loop the array of controls [checkbobes] use something like this:

   with controlname
.lbound
0
 
DoItMyWayCommented:
dim booValid as boolean

With ControlName
   for i = .lbound to .ubound
      if .checked=true then
        boovalid = true
      end if
   next i
 
   If booValid=false
   'alert user
   msgbox "Please select at least one CheckBox.",vbexclamation, "Invalid Selection:"
   else
   'Calculate total(s).
   end if
End With
0
 
presbriaCommented:
these lines do that;

For iCount = 0 To 2
   If checkboxsamevalues(iCount).Value = vbChecked Then
      iTotal = iTotal + 100

Next

0
 
presbriaCommented:
these lines do that;

For iCount = 0 To 2
     If checkboxsamevalues(iCount).Value = vbChecked Then
           iTotal = iTotal + 100
     End If
Next


The count is from 0 to 2 as I'v accounted for a control array of 3 controls.  When you try to name a control the same as a control that already exists, vb will ask if you want to create a control array.  If you do, and you've named something a for example, one will be called a(0) and the next one a(1), etc.  That's useful for looping, as shown above.

If you want the value isolated for the group that has the same value, then make another variable.

If you want me to fix up your forms email me at brianpressman@iko.com
0
All Courses

From novice to tech pro — start learning today.