Solved

Place a totaled value into a TextBox

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…

803 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