Solved

Place a totaled value into a TextBox

Posted on 1998-10-11
5
220 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
[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
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month6 days, 14 hours left to enroll

623 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