?
Solved

Easy Beginner VB Question Part II

Posted on 2003-03-11
8
Medium Priority
?
215 Views
Last Modified: 2010-04-07
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!!

0
Comment
Question by:nouellette
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:intelnet
ID: 8113598
You might check out this site-> www.planet-source-code.com
They have lots of different examples that might help.
0
 

Accepted Solution

by:
presbria earned 300 total points
ID: 8113684
'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
 
LVL 2

Expert Comment

by:Jacamar
ID: 8114406
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:nouellette
ID: 8114623
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
 

Expert Comment

by:DoItMyWay
ID: 8114657
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
 

Expert Comment

by:DoItMyWay
ID: 8114691
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
 

Expert Comment

by:presbria
ID: 8119195
these lines do that;

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

Next

0
 

Expert Comment

by:presbria
ID: 8119215
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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question