Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Posts
    15

    Question Unanswered: Adding Values in a Combo Box and displaying a total in a text box

    Hi there ,
    How do you add values in a combo box and show the total in a text box.

    E.g Suppose I had 3 combo boxes

    A B C say i chose
    1 2 2 then txtanswer should show 5?

  2. #2
    Join Date
    Sep 2003
    Location
    Birmingham, UK
    Posts
    121
    Use the 'AfterUpdate' function on the combo C and in the Event Procedure put something like:

    txtAnswer = A + B + C

  3. #3
    Join Date
    Jul 2004
    Location
    Inverurie
    Posts
    628
    try this:
    Code:
    Sub A AfterUpdate()
    
    Dim AVal As Integer
    
    AVal = Me[A].Value
    
    End Sub
    
    Sub B AfterUpdate()
    
    Dim BVal As Integer
    
    BVal = Me[B].Value
    
    End Sub
    
    Sub C AfterUpdate()
    
    Dim CVal As Integer
    
    CVal = Me[C].Value
    
    End Sub
    
    Sub CalcAnswer_Click()
    
    Dim txtAnswer As Integer
    
    txtAnswer = AVal + BVal + CVal
    
    CalcAnswer.Value = txtAnswer
    
    End Sub

  4. #4
    Join Date
    Mar 2005
    Posts
    93
    Quote Originally Posted by SexualChocolate
    Use the 'AfterUpdate' function on the combo C and in the Event Procedure put something like:

    txtAnswer = A + B + C
    I concur with SexaulChocolate, but you need to have the code in all 3 combo boxes' AfterUpdate events. You should use an IF statement to check for Null values as well.

  5. #5
    Join Date
    Dec 2003
    Posts
    268
    For sake of management I might generate a recursive event that calls itself for three iterations and just add that value to the respective text box on the after update event for any of the combo boxes.

    This implementation accounts for null values in the event if any combo box is not selected yet.

    It responds pretty quickly, but if you wanted to make it go faster you could declare the variable BoxNames on the main for the form, then load the array with values on the form_Load event. In this instance it builds and fills the array with every afterupdate event. Not the most efficient, but I think yo get the gist.

    MW

    Private Sub AddComboBoxes()
    Dim BoxNames(3) As String
    Dim x As Integer
    Dim sum As Integer

    'fill the values of the array with the managed controls
    BoxNames(0) = "A": BoxNames(1) = "B": BoxNames(2) = "C"

    For x = 0 To UBound(BoxNames) - 1
    If Not IsNull(Controls("cbo" & BoxNames(x))) Then sum = sum + Controls("cbo" & BoxNames(x)).Value
    Next x
    txtSum = sum
    End Sub

    Private Sub cboB_AfterUpdate()
    AddComboBoxes
    End Sub

    Private Sub cboC_AfterUpdate()
    AddComboBoxes
    End Sub
    Private Sub cboA_AfterUpdate()
    AddComboBoxes
    End Sub

Posting Permissions

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