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
Solved

Place a totaled value into a TextBox

Posted on 1998-10-11
5
209 Views
Last Modified: 2012-05-04
I'm using VB5.  I've created a control array of 10 objects.  I want to total the value in the 10 textboxes and place the value into a "sum" textbox.  I start out putting default values into the control array (using a loop and making the value of each textbox equal i).  Now I need to total the control array and have that total display in another textbox.

Also, I want the user to be able to enter values into the control array (textboxes).  How can a make my "sum" textbox always equal the sum of the control array, i.e., not just upon form_load.

I think this'll be easy, I'm probably not asking it correctly.  If necessary, I'll e-mail my form and it should make more sense.  Thanks!
0
Comment
Question by:regent
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 70 total points
ID: 1439271
Regent:

First, add the function

Private Function fGetSum(myControl() as Textbox) as String
Dim i as Integer
Dim sum as Double

For i = myConTrol().Lbound to MyControl().UBound
   If IsNumeric(MyControl(i).Text) then
        Sum = Sum + CDbl(myControl(i).text)
       Else
        MsgBox "Invalid Entry", VBOKONLY, "Enter Numeric"
    End If
  Next i

fGetSum = cStr(Sum)
End Function

Now when you need to show the sum just add:
   TextSum = fGetSum(theTextArrayName())
You should properly replace TextSum with the name of the sum text box, , and TextArrayName with your 10 array name

To show the sum when the user is entering the number
 in the myTextArrayName_Change Event add:
If IsNumeric(myTextArrayName(Index)) then
       textSum = fGetSum (theTextArrayName())

Regards
Dalin

0
 

Author Comment

by:regent
ID: 1439272
Thanks for your help (again)!  :)

tjr
0
 

Author Comment

by:regent
ID: 1439273
Dalin,
I get a type mismatch on my control array when I call the function.  Will the function work with a control array, since VB says it's not really an array?  Can I send you my form and have you look at it?
Tom
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1439274
Send to me at Dalin_N@MailExcite.com
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1439275
Sorry, I forgot you are passing a controler array.  You need to revise a little in the function:


    Private Function fGetSum(myControl As Object) As String
    Dim i As Integer
    Dim sum As Double
   
        For i = myControl.Lbound To myControl.UBound
       
            If IsNumeric(myControl(i).Text) Then
                sum = sum + CDbl(myControl(i).Text)
            Else
                MsgBox "Invalid Entry", vbOKOnly, "Enter Numeric"
            End If
           
        Next i
   

    fGetSum = CStr(sum)
    End Function

To call it:
TxtSum = fGetSum(txtXVal)
where txtSum is the text box you want to display the sum, txtXVal is the textbox array.

let me know if there is any problems.



0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Response and request in VB6 application 11 577
Updates not working for MS Windows 7 12 181
MS Date Picker 64 bit 32 bit issue 12 60
Send outlook email from VBS Script 2 42
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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