Solved

Place a totaled value into a TextBox

Posted on 1998-10-11
5
204 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now